Re: [GRASS-user] Python Script not working now

2023-08-07 Thread Anna Petrášová
Hi,

I think the gsetup.init parameters changed, see:
https://grass.osgeo.org/grass83/manuals/libpython/script.html#script.setup.init

Best,
Anna

On Mon, Aug 7, 2023 at 3:03 PM Phillip Allen 
wrote:

> Hi all,
>
> I wrote a python script ~6 years ago to create sample catchment polygons
> for stream sediment samples and all worked well. I created 10,000s of
> catchements successfully!
>
> Today I reconfigured the script for some new data and I am now getting
> this error:
>
> (Mon Aug  7 12:57:09 2023)
>
> D:\DEM_12_5\mcc_area\basin_code04_mcc.py
>
> Traceback (most recent call last):
>   File "D:\DEM_12_5\mcc_area\basin_code04_mcc.py", line 43,
> in 
> gsetup.init(gisbase, gisdb, location, mapset)
>   File "C:\OSGeo4W\apps\grass\grass83\etc\python\grass\scrip
> t\setup.py", line 329, in init
> raise ValueError(
> ValueError: Mapset D:\DEM_12_5\grassdata\dem12_5_a is not
> valid:  is not a valid GRASS Location
> because PERMANENT Mapset is missing
> (Mon Aug  7 12:57:10 2023) Command ended with non-zero return code 1 (0
> sec)
>
> I know the mapset PERMANENT exists and is valid since I just opened it in
> GRASS.
>
> Why is this python code below not running properly now?
> Has something in GRASS changed 'recently' ?
>
> regards,
>
> Phil
> Consulting Geochemist
> (someday I really need to become a better python coder!)
>
> #
> #  basin python code
> #! C:\OSGeo4W64\bin\python.exe
>
> # IMPORT CSV & LOOP THROUGH COORDINATES FOR r.water.outlet
> import os
> import sys
> import psycopg2
>
>
> #set up GRASS environment variables
> sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
> import grass.script as g
> import grass.script.setup as gsetup
> gisbase = os.environ['GISBASE']
>
> gisdb = 'D:/DEM_12_5/grassdata'
> location = 'dem12_5_a'
> mapset = 'PERMANENT'
> rast_draindir = 'mcc_d1_draindir150'
>
>
> # first join the new snap table's cat column to the original attribute
> table
> # v.out.ascii input=th_ss_aem_snap_r5@PERMANENT type=point
> output=D:/DEM_12_5/mcc_area/mcc_ss_aga_pt_temp.csv columns=sample_number
> format=point separator=comma precision=20
> txtfile = 'D:/DEM_12_5/mcc_area/mcc_ss_aga_pt_temp.csv'
> txtfileWKT = 'D:/DEM_12_5/mcc_area/mcc_ss_WKT.txt'
> pgsrid = '32618'
>
> pg_user = 'postgres'
> pg_user_pwd = 'notmyrealpassword'
> pg_schema = 'basin'
> pg_tbl_name = 'mcc_samp_basin'
> pg_host = 'localhost'
> pg_dbname = 'ss_basin_test'
>
> txt_pg_tbl_create = 'CREATE TABLE ' + pg_schema + "." + pg_tbl_name + ' (x
> double precision, y double precision, samp_id character varying(254), geom
> geometry(POLYGON, ' + pgsrid + ') );'
> txt_pg_db =  "host='" + pg_host + "' dbname='" + pg_dbname + "' user='" +
> pg_user + "' password='" + pg_user_pwd + "'"
>
> txt_gpg_db = "PG:dbname=" + pg_dbname + " host='" + pg_host + "' user='" +
> pg_user + "' password='" + pg_user_pwd + "'"
>
>
> #print "Connecting to database\n ->%s" % (txt_pg_db)
> #print "Grass PG Connection String\n ->%s" % (txt_gpg_db)
>
> gsetup.init(gisbase, gisdb, location, mapset)
> g.run_command('g.region', flags='a', rast = rast_draindir)
>
>
> # Create PostGIS Polygon Table
> conn = psycopg2.connect(txt_pg_db)
> cur = conn.cursor()
> cur.execute(txt_pg_tbl_create)
> conn.commit()
>
> #csv file reading and importation
> import csv
> rows = list(open(txtfile))
> totalrows = len(rows) - 1
>
> #loop through the csv(coordinates) file in r.water.outlet module
> f = open(txtfile, 'r')
> element = list(csv.reader(f))
> i = 0
> j = 0
> while True:
> if i <= totalrows:
> g.run_command('r.water.outlet', overwrite = True , input = rast_draindir,
> output = 'b' , coordinates = element[i][j] + ',' + element[i][j + 1] )
> g.run_command('r.null' , map='b', setnull = 0 )
> g.run_command('r.to.vect' , overwrite = True , input='b', output = 'b_rc',
> type = 'area' )
> g.run_command('v.out.ascii', overwrite = True, input='b_rc',
> type='boundary', output=txtfileWKT, format='wkt')
> g.run_command('v.out.postgis', overwrite = True, input='b_rc',
> type='boundary', output=txt_gpg_db , output_layer = pg_schema + ".b_rc" )
> sql_txt = "INSERT INTO " + pg_schema + "." + pg_tbl_name + " SELECT " +
> element[i][j] + " AS x, " + element[i][j + 1] + " AS y, '" + element[i][j +
> 3] + "' AS samp_id, b_rc.geom AS geom FROM " + pg_schema + ".b_rc;"
> cur.execute(sql_txt)
> conn.commit()
> i = i + 1
> else:
> break
>
> cur.close()
> conn.close()
>
>
> ___
> grass-user mailing list
> grass-user@lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user
>
___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python Script not working now

2023-08-07 Thread Phillip Allen
Hi all,

I wrote a python script ~6 years ago to create sample catchment polygons
for stream sediment samples and all worked well. I created 10,000s of
catchements successfully!

Today I reconfigured the script for some new data and I am now getting this
error:

(Mon Aug  7 12:57:09 2023)

D:\DEM_12_5\mcc_area\basin_code04_mcc.py

Traceback (most recent call last):
  File "D:\DEM_12_5\mcc_area\basin_code04_mcc.py", line 43,
in 
gsetup.init(gisbase, gisdb, location, mapset)
  File "C:\OSGeo4W\apps\grass\grass83\etc\python\grass\scrip
t\setup.py", line 329, in init
raise ValueError(
ValueError: Mapset D:\DEM_12_5\grassdata\dem12_5_a is not
valid:  is not a valid GRASS Location
because PERMANENT Mapset is missing
(Mon Aug  7 12:57:10 2023) Command ended with non-zero return code 1 (0
sec)

I know the mapset PERMANENT exists and is valid since I just opened it in
GRASS.

Why is this python code below not running properly now?
Has something in GRASS changed 'recently' ?

regards,

Phil
Consulting Geochemist
(someday I really need to become a better python coder!)

#
#  basin python code
#! C:\OSGeo4W64\bin\python.exe

# IMPORT CSV & LOOP THROUGH COORDINATES FOR r.water.outlet
import os
import sys
import psycopg2


#set up GRASS environment variables
sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
import grass.script as g
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']

gisdb = 'D:/DEM_12_5/grassdata'
location = 'dem12_5_a'
mapset = 'PERMANENT'
rast_draindir = 'mcc_d1_draindir150'


# first join the new snap table's cat column to the original attribute
table
# v.out.ascii input=th_ss_aem_snap_r5@PERMANENT type=point
output=D:/DEM_12_5/mcc_area/mcc_ss_aga_pt_temp.csv columns=sample_number
format=point separator=comma precision=20
txtfile = 'D:/DEM_12_5/mcc_area/mcc_ss_aga_pt_temp.csv'
txtfileWKT = 'D:/DEM_12_5/mcc_area/mcc_ss_WKT.txt'
pgsrid = '32618'

pg_user = 'postgres'
pg_user_pwd = 'notmyrealpassword'
pg_schema = 'basin'
pg_tbl_name = 'mcc_samp_basin'
pg_host = 'localhost'
pg_dbname = 'ss_basin_test'

txt_pg_tbl_create = 'CREATE TABLE ' + pg_schema + "." + pg_tbl_name + ' (x
double precision, y double precision, samp_id character varying(254), geom
geometry(POLYGON, ' + pgsrid + ') );'
txt_pg_db =  "host='" + pg_host + "' dbname='" + pg_dbname + "' user='" +
pg_user + "' password='" + pg_user_pwd + "'"

txt_gpg_db = "PG:dbname=" + pg_dbname + " host='" + pg_host + "' user='" +
pg_user + "' password='" + pg_user_pwd + "'"


#print "Connecting to database\n ->%s" % (txt_pg_db)
#print "Grass PG Connection String\n ->%s" % (txt_gpg_db)

gsetup.init(gisbase, gisdb, location, mapset)
g.run_command('g.region', flags='a', rast = rast_draindir)


# Create PostGIS Polygon Table
conn = psycopg2.connect(txt_pg_db)
cur = conn.cursor()
cur.execute(txt_pg_tbl_create)
conn.commit()

#csv file reading and importation
import csv
rows = list(open(txtfile))
totalrows = len(rows) - 1

#loop through the csv(coordinates) file in r.water.outlet module
f = open(txtfile, 'r')
element = list(csv.reader(f))
i = 0
j = 0
while True:
if i <= totalrows:
g.run_command('r.water.outlet', overwrite = True , input = rast_draindir,
output = 'b' , coordinates = element[i][j] + ',' + element[i][j + 1] )
g.run_command('r.null' , map='b', setnull = 0 )
g.run_command('r.to.vect' , overwrite = True , input='b', output = 'b_rc',
type = 'area' )
g.run_command('v.out.ascii', overwrite = True, input='b_rc',
type='boundary', output=txtfileWKT, format='wkt')
g.run_command('v.out.postgis', overwrite = True, input='b_rc',
type='boundary', output=txt_gpg_db , output_layer = pg_schema + ".b_rc" )
sql_txt = "INSERT INTO " + pg_schema + "." + pg_tbl_name + " SELECT " +
element[i][j] + " AS x, " + element[i][j + 1] + " AS y, '" + element[i][j +
3] + "' AS samp_id, b_rc.geom AS geom FROM " + pg_schema + ".b_rc;"
cur.execute(sql_txt)
conn.commit()
i = i + 1
else:
break

cur.close()
conn.close()
___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python script for template creation -i.atcorr module-reg

2018-10-22 Thread Markus Metz
On Mon, Oct 22, 2018 at 1:32 PM Moritz Lennert 
wrote:
>
> Hi,
>
> Please keep discussions on the mailing list.
>
> On 22/10/18 13:07, kameswari devi wrote:
> > yes i have given a csv file but x_new is above the interpolation range
> > message is appeared.
> >> wav(nm),B10,B11
> >> 477,0.3744
> >> 478,0.6864
> >> 480,1
> >> 481,0.8624
> >> 482,0.6018
> >> 483,0.4434
> >> 484,
> >> 485,
> >> 486,,0.377
> >> 487,,0.667
> >> 488,,0.968
> >> 489,,1
> >> 490,,0.760
> >> 491,,0.590
> >> 492,,0.455
>
> AFAIK, you always need the same number of fields on every line, but the
> first 8 lines only have two fields while the rest has 3 fields.

Which GRASS version are you using? Please use a recent GRASS 7.4 version,
or better GRASS 7.6. Can you also post the full output of create_iwave.py?
That could help to find out what goes wrong.

Markus M

>
> Moritz
>
> > On Mon, Oct 22, 2018 at 1:32 PM Moritz Lennert
> >  wrote:
> >>
> >> On 22/10/18 06:02, kameswari devi wrote:
> >>> Hello all,
> >>> I m trying to use Create_iwave.py for template creation in grass for
> >>> atmopsheric correction.But due to the spectral response values errors
> >>> are thrown. here i am sending the rsr i used and error i got .please
> >>> suggest me. thakn you
> >>>
> >>> wav(nm)  B10
> >>> 477 0.3744
> >>> 478 0.6864
> >>> 480 1
> >>> 481 0.8624
> >>> 482 0.6018
> >>> 483 0.4434
> >>> 484
> >>> 485
> >>> 486 0.377
> >>> 487 0.667
> >>> 488 0.968
> >>> 489 1
> >>> 490 0.760
> >>> 491 0.590
> >>> 492 0.455
> >>
> >>
> >> AFAIR, you need to separate fields by a comma, not a space.
> >>
> >> Moritz
> >
> >
> >
>
>
> ___
> grass-user mailing list
> grass-user@lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user
___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Re: [GRASS-user] Python script for template creation -i.atcorr module-reg

2018-10-22 Thread Moritz Lennert

Hi,

Please keep discussions on the mailing list.

On 22/10/18 13:07, kameswari devi wrote:

yes i have given a csv file but x_new is above the interpolation range
message is appeared.

wav(nm),B10,B11
477,0.3744
478,0.6864
480,1
481,0.8624
482,0.6018
483,0.4434
484,
485,
486,,0.377
487,,0.667
488,,0.968
489,,1
490,,0.760
491,,0.590
492,,0.455


AFAIK, you always need the same number of fields on every line, but the 
first 8 lines only have two fields while the rest has 3 fields.


Moritz


On Mon, Oct 22, 2018 at 1:32 PM Moritz Lennert
 wrote:


On 22/10/18 06:02, kameswari devi wrote:

Hello all,
I m trying to use Create_iwave.py for template creation in grass for
atmopsheric correction.But due to the spectral response values errors
are thrown. here i am sending the rsr i used and error i got .please
suggest me. thakn you

wav(nm)  B10
477 0.3744
478 0.6864
480 1
481 0.8624
482 0.6018
483 0.4434
484
485
486 0.377
487 0.667
488 0.968
489 1
490 0.760
491 0.590
492 0.455



AFAIR, you need to separate fields by a comma, not a space.

Moritz







___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Re: [GRASS-user] Python script for template creation -i.atcorr module-reg

2018-10-22 Thread Moritz Lennert

On 22/10/18 06:02, kameswari devi wrote:

Hello all,
I m trying to use Create_iwave.py for template creation in grass for
atmopsheric correction.But due to the spectral response values errors
are thrown. here i am sending the rsr i used and error i got .please
suggest me. thakn you

wav(nm)  B10
477 0.3744
478 0.6864
480 1
481 0.8624
482 0.6018
483 0.4434
484
485
486 0.377
487 0.667
488 0.968
489 1
490 0.760
491 0.590
492 0.455



AFAIR, you need to separate fields by a comma, not a space.

Moritz
___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

[GRASS-user] Python script for template creation -i.atcorr module-reg

2018-10-21 Thread kameswari devi
Hello all,
I m trying to use Create_iwave.py for template creation in grass for
atmopsheric correction.But due to the spectral response values errors
are thrown. here i am sending the rsr i used and error i got .please
suggest me. thakn you

wav(nm)  B10
477 0.3744
478 0.6864
480 1
481 0.8624
482 0.6018
483 0.4434
484
485
486 0.377
487 0.667
488 0.968
489 1
490 0.760
491 0.590
492 0.455

ERROR:
python 2.7.13
y=self._evaluate(x)
File"C"\*\interpolate.py",line 618,in_check_bounds
raise valueError("A value in x_new is above the interpolation"
ValueError: A value in x_new is above the interpolation range.

I have given rsr of hyperspectral sensor with narrow wavelength ranges
initially but same error is coming.I tried truncate bounds but of no
use.
Actually i am new to python. so what function it internally using for
generation of interpolated template is not understood.Values should be
of 10nm or lesser or also can be used?
please provide me the solution.


-- 
Regards
kameswari devi P
___
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

[GRASS-user] Python script works with Qgis but not in Grass

2014-01-27 Thread Davide Sponchiado

Hello to everyone,

i got Windows 8 with QGIS Dofour 2.0.1 e Grass 6.4.3 on my PC.

I got no problem when i run my script on Qgis but i need to fix some 
problems inside it and Qgis doesn't show you any log errors so i need to 
use Grass.

When i launch it some errors happens:

Launching script 'C:\Program Files\QGIS 
Dufour\apps\grass\grass-6.4.3\etc\gui\scripts\v.grass2hec_rev.py'...

Exception in thread Thread-75:
Traceback (most recent call last):
  File
C:\PROGRA~1\QGISDU~1\apps\Python27\lib\threading.py, line
808, in __bootstrap_inner
self.run()
  File C:\PROGRA~1\QGISDU~1\apps\grass\grass-6.4.3\etc\wxpy
thon\gui_core\goutput.py, line 126, in run
self.resultQ.put((requestId, self.requestCmd.run()))
  File C:\PROGRA~1\QGISDU~1\apps\grass\grass-6.4.3\etc\wxpy
thon\core\gcmd.py, line 553, in run
env = env)
  File C:\PROGRA~1\QGISDU~1\apps\grass\grass-6.4.3\etc\wxpy
thon\core\gcmd.py, line 164, in __init__
subprocess.Popen.__init__(self, args, **kwargs)
  File
C:\PROGRA~1\QGISDU~1\apps\Python27\lib\subprocess.py, line
711, in __init__
errread, errwrite)
  File
C:\PROGRA~1\QGISDU~1\apps\Python27\lib\subprocess.py, line
948, in _execute_child
startupinfo)
TypeError: environment can only contain strings

First of all I uninstall and reinstall Qgis and Grass many times trying 
to understand if the problem was in the x86 version or in some registry 
forgot in the previous versions (i control the regedit deleting manually 
all the files i thought related to Qgis and Grass).
Then, i remembered that I installed Python one month ago (not in the 
same folder), then i uninstall it.
I fear to forgot some other registry but nothing is shown directly in 
the registry. All the Python27 are deleted.


Now i reinstall QGIS and Grass (version i told before) but still it 
doesn't work.


Really need a big help.
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script works with Qgis but not in Grass

2014-01-27 Thread Enrico Gallo
Dear list,

this problem has already reported for grass 7 [0] (still without a solution?)

I am not a Python expert, but accordind to [1] the problem seems due
to Unicode string management.

Davide, I suppose the script path used with Grass is:something like this:

C:\[...]\QGIS Dufour\apps\grass\grass-6.4.3\etc\gui\scripts

but probabliy this is not the same path where you put the same script
in order to use QGIS scripting interface, right?

List, any hint to check startupinfo values before launching the script ?
Could we avoid Unicode strings or force it to right values as in [2] ?

HTH

Enrico



[0] http://lists.osgeo.org/pipermail//grass-user/2013-October/069019.html
[1] https://mail.python.org/pipermail/python-win32/2010-April/010400.html
[2] https://github.com/SeleniumHQ/selenium/pull/54/files
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script

2013-07-02 Thread Pedro Camargo
Hi,
I'm trying to use r.stats via the python shell inside GRASS but I'm
getting an error that I do not know how to solve.  The code I'm using is
this:

grass.run_command('r.stats',input=inp,output='D:\\CropScape\\2012_cdls\\AZ.c
sv',flags = 'ani')

I already tried the following variables for input:

inp=['AZ','COUNTIES']
inp=['AZ@PERMANENT ','COUNTIES@PERMANENT']
inp=['D:\\CropScape\\2012_cdls\\AZ.tif',
'D:\\CropScape\\Counties\\COUNTIES.tif']


Every time I get the same error:  GRASS_INFO_ERROR(1444,1): Raster map
D:\CropScape\2012_cdls\AZ.tif not found

Any idea of what could be?


Thanks,
Pedro

___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python script

2013-07-02 Thread Glynn Clements

Pedro Camargo wrote:

   I'm trying to use r.stats via the python shell inside GRASS but I'm
 getting an error that I do not know how to solve.  The code I'm using is
 this:
 
 grass.run_command('r.stats',input=inp,output='D:\\CropScape\\2012_cdls\\AZ.c
 sv',flags = 'ani')
 
 I already tried the following variables for input:
 
 inp=['AZ','COUNTIES']
 inp=['AZ@PERMANENT ','COUNTIES@PERMANENT']

Either of these should work, provided that you actually have maps with
those names in the PERMANENT mapset.

 inp=['D:\\CropScape\\2012_cdls\\AZ.tif',
 'D:\\CropScape\\Counties\\COUNTIES.tif']

The inputs must be map names, not filenames.

Have you actually created the maps? You cannot use an image file as a
map without first either importing it (with e.g. r.in.gdal) or linking
it (with r.external).

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script: focal filter based on r.cost

2012-05-29 Thread Johannes Radinger
Hi,

I try to create a tiny script that allows to create a focal filter based on 
r.cost. This means that rather using a moving window I use r.cost to create a 
buffer around a cell which is then used for getting certain statistics of the 
neighborhood of that cell (e.g. mean, max, sum) and create a new raster based 
on these statistical values. I'd like to use r.cost as it also allows to create 
distance buffers along rasterized lines (the main purpose for me). One example 
can be: Get the maximum raster value which is up- and downstream  from a cell 
in a river distance of 500 m...

So far I think I have a plan how it can work and I tried to write the script in 
python. Anyway there are some problems where I am not sure how I can solve 
them...

1) I don't really know how I can loop over the points (raster cells) and update 
the points with the statistical value (e.g. mean). I try following:

grass.write_command(db.execute, stdin = 'UPDATE point_tmp%s SET univar=%d 
WHERE cat = %d' % (str(os.getpid()),stat,cat))

but I get a time out (WARNING: Busy SQLITE db, already waiting for 10 
seconds, 20 sec...). Probably it is related to the fact the two processes are 
using the same database table (loop over the points and update)... 

2) I don't know what is the best way to get a variable based on the input, 
which is like defining a global variable in a if-loop, but I am not sure if 
that is a good way?

Best is to look at the script which is attached. Theoretically it should be 
possible to be used on every raster. But as it loops over each cell it takes 
really long time to process large maps.

Maybe anyone wants to help me further developing this tiny python script. Any 
help is very much appreciated. Thank you!

Cheers
/johannes
-- 
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!  

Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
#!/usr/bin/env python
#

#
# SCRIPT:   Creating focal statistical raster based on r.cost distance
#
# AUTHOR(S):Johannes Radinger

# DATE: 2012-05-03
#
#
#%Module
#% description: Calculating focal raster based on a r.cost distance of a raster cell
#% keywords: raster
#%End
#%option
#% key: input
#% type: string
#% gisprompt: old,cell,raster
#% description: input raster
#% required: yes
#%end
#%option
#% key: stat
#% type: string
#% multiple:no
#% options:mean,max,sum
#% description: Desired focal statistic for output raster
#% required: yes
#%end
#%option
#% key: distance
#% type: double
#% required: yes
#% multiple: no
#% description: Distance for filter/buffer
#%End



import sys
import os
import atexit
import sqlite3


import grass.script as grass


tmp_map_rast = None
tmp_map_vect = None

def cleanup():
if (tmp_map_rast or tmp_map_vect):
   grass.run_command(g.remove, 
   rast = tmp_map_rast,
   vect = tmp_map_vect,
   quiet = True)




def main():  
#defining temporary maps
tmp_map_rast = [MASK,tmp+str(os.getpid()),distance_mask+str(os.getpid())]
tmp_map_vect = [point_tmp+str(os.getpid())]

#database-connection
env = grass.gisenv()
gisdbase = env['GISDBASE']
location = env['LOCATION_NAME']
mapset = env['MAPSET']
grass.run_command(db.connect,
  driver = sqlite,
  database = os.path.join(gisdbase, location, mapset,'sqlite.db'))  
database = sqlite3.connect(os.path.join(gisdbase, location, mapset, 'sqlite.db'))
db = database.cursor()
# Getting resolution of raster map
res = int(grass.read_command(g.region,
  flags = m).strip().split('\n')[4].split('=')[1])  

 
# Defining variables
input = [options['input']]
distance = options['distance']

# Remove any mask before processing
grass.run_command(g.remove,
rast = MASK)


for i in input:
grass.mapcalc($tmp = if($raster,$res,null()),  # creating temporary map
tmp = tmp+str(os.getpid()),
res = res,
raster = i)
# make points from raster
grass.run_command(r.to.vect,
overwrite = True,
input = tmp+str(os.getpid()),
output = point_tmp+str(os.getpid()),
feature = point)

# Get coordinates for each point
grass.run_command(v.db.addcol,
map = point_tmp+str(os.getpid()),
columns = X DOUBLE, Y DOUBLE, univar DOUBLE)  
grass.run_command(v.to.db,
map = point_tmp+str(os.getpid()),
type = point,
option = coor,
columns = 

[GRASS-user] Python Script is not able to find a file in a different Drive

2010-10-14 Thread Jenny Turner
Greetings

I have a python script that needs to get a file (import a raster). I have
tried in Ubuntu and also in Windows and it works but, When I have that file
in a different hard drive (D: instead of C: where WinGRASS is installed)
os.path.isfile(input) is false... Why? is it some limitation of GRASS?

Thanjs~
Jenny
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python Script- Check if a raster exists

2010-09-27 Thread Helena Herrera
Hi

I'm doing a python script and, in a process, I need to check if a raster map
exists. My idea is, if this map exists I don't need to create otherwise, I
need to create it (v.to.rast). Is there a way to check if a map exists in
the current mapset?
Thanks
Helena
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script- Check if a raster exists

2010-09-27 Thread Christian Schwartze
Try e.g g.findfile element=vector file=testmap

Christian


From: Helena Herrera 
Sent: Monday, September 27, 2010 12:25 PM
To: grass-user@lists.osgeo.org 
Subject: [GRASS-user] Python Script- Check if a raster exists


Hi 


I'm doing a python script and, in a process, I need to check if a raster map 
exists. My idea is, if this map exists I don't need to create otherwise, I need 
to create it (v.to.rast). Is there a way to check if a map exists in the 
current mapset?
Thanks
Helena





___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script- Check if a raster exists

2010-09-27 Thread Helena Herrera
Uhm. I have tried that one with
g.findfile element=raster file=L (I have a raster map named L in my mapset)
and I get:
name=
mapset=
fullname=
file=
Which would mean that it doesn't exist.

is there any other way to do this for GRASS-pythons scripts?

On Mon, Sep 27, 2010 at 11:39 AM, Christian Schwartze 
christian.schwar...@uni-jena.de wrote:

  Try e.g g.findfile element=vector file=testmap

 Christian

  *From:* Helena Herrera helenaherrera1...@gmail.com
 *Sent:* Monday, September 27, 2010 12:25 PM
 *To:* grass-user@lists.osgeo.org
 *Subject:* [GRASS-user] Python Script- Check if a raster exists

 Hi

 I'm doing a python script and, in a process, I need to check if a raster
 map exists. My idea is, if this map exists I don't need to create otherwise,
 I need to create it (v.to.rast). Is there a way to check if a map exists in
 the current mapset?
 Thanks
 Helena

 --

 ___
 grass-user mailing list
 grass-user@lists.osgeo.org
 http://lists.osgeo.org/mailman/listinfo/grass-user


___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script- Check if a raster exists

2010-09-27 Thread Christian Schwartze
See the notes and output sections of the g.findfile manpage. There you can find 
additional information how to use the comand in scripts.
http://grass.fbk.eu/gdp/html_grass64/g.findfile.html

Christian.


From: Helena Herrera 
Sent: Monday, September 27, 2010 12:47 PM
To: Christian Schwartze ; grass-user@lists.osgeo.org 
Subject: Re: [GRASS-user] Python Script- Check if a raster exists


Uhm. I have tried that one with  
g.findfile element=raster file=L (I have a raster map named L in my mapset) and 
I get:
name=
mapset=
fullname=
file=
Which would mean that it doesn't exist.


is there any other way to do this for GRASS-pythons scripts?


On Mon, Sep 27, 2010 at 11:39 AM, Christian Schwartze 
christian.schwar...@uni-jena.de wrote:

  Try e.g g.findfile element=vector file=testmap

  Christian


  From: Helena Herrera 
  Sent: Monday, September 27, 2010 12:25 PM
  To: grass-user@lists.osgeo.org 
  Subject: [GRASS-user] Python Script- Check if a raster exists


  Hi 


  I'm doing a python script and, in a process, I need to check if a raster map 
exists. My idea is, if this map exists I don't need to create otherwise, I need 
to create it (v.to.rast). Is there a way to check if a map exists in the 
current mapset?
  Thanks
  Helena


--


  ___
  grass-user mailing list
  grass-user@lists.osgeo.org
  http://lists.osgeo.org/mailman/listinfo/grass-user



___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script- grass.debug

2010-09-27 Thread Glynn Clements

Luisa Peña wrote:

 I'm doing a Python script, based on r.in.aster, to import a set of images
 but, I'm not sure for that is used the following:
 grass.debug(gdalwarp -t_srs %s %s %s % (proj, srcfile, tempfile))
 WHat is the point of using grass.debug

grass.debug prints a debugging message if DEBUG is set.

It's just an interface to g.message -d ...:

def debug(msg, debug = 1):
run_command(g.message, flags = 'd', message = msg, debug = debug)

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python Script- grass.debug

2010-09-23 Thread Luisa Peña
Hi there

I'm doing a Python script, based on r.in.aster, to import a set of images
but, I'm not sure for that is used the following:
grass.debug(gdalwarp -t_srs %s %s %s % (proj, srcfile, tempfile))
WHat is the point of using grass.debug if I do the following after this:
if platform.system() == Darwin:
cmd = [arch, -i386, gdalwarp, -t_srs, proj, srcfile,
tempfile ]
else:
cmd = [gdalwarp, -t_srs, proj, srcfile, tempfile ]
p = grass.call(cmd)
if p != 0:
#check to see if gdalwarp executed properly
return


Thanks

Luisa
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python Script for changing raster values

2010-09-07 Thread Luisa Peña
Greetings

I have a classification raster (with values that ranges from 1-20) and I
need to reclassify them. I mean, to change (as an example):
1-  255
2-  100
3 - 150

and I need to implement this on a Python script. What do you suggest to do?
I mean, that is the most robust and easy way to implement this?
Thanks
Luisa
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script for changing raster values

2010-09-07 Thread Christian Schwartze
Following code could be one approach:

os.system(cat %s | r.recode input=map_a output=map_recl % path_to_rules)

where path_to_rules is a previously created (temp) file containg the 
reclassifiying rules as for example:

1:1:1.1:1.1
2:2:7.5:7.5
3:3:0.4:0.4

Christian.




From: Luisa Peña 
Sent: Tuesday, September 07, 2010 12:37 PM
To: GRASS user list 
Subject: [GRASS-user] Python Script for changing raster values


Greetings 


I have a classification raster (with values that ranges from 1-20) and I need 
to reclassify them. I mean, to change (as an example):
1-  255
2-  100
3 - 150


and I need to implement this on a Python script. What do you suggest to do? I 
mean, that is the most robust and easy way to implement this?
Thanks
Luisa





___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python Script for changing raster values

2010-09-07 Thread Glynn Clements

Christian Schwartze wrote:

 Following code could be one approach:
 
 os.system(cat %s | r.recode input=map_a output=map_recl % path_to_rules)
 
 where path_to_rules is a previously created (temp) file containg the
 reclassifiying rules as for example:

os.system() shouldn't be used. E.g. the above will fail if
path_to_rules contains spaces (as is often the case on Windows).

The preferred approach for the above command is:

grass.run_command(r.recode, input=map_a,
output=map_recl, rules = path_to_rules)

If you have a command which requires input via stdin (rather from a
named file), use e.g.:

rules_f = open(path_to_rules, r)
try:
grass.run_command(r.recode, input=map_a,
output=map_recl, stdin = rules_f)
finally:
rules_f.close()

Or, if you can rely upon Python 2.5 or later, use:

from __future__ import with_statement
with open(path_to_rules, r) as rules_f:
grass.run_command(r.recode, input=map_a,
output=map_recl, stdin = rules_f)

Also, even in shell scripts cat file | command is suboptimal; use
command  file instead.

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script- drop-down menu list of multiple groups

2010-08-16 Thread António Rocha

Greetings

I'm implementing a Python Script and I need to know one thing related 
with Modules Interfaces: can I have a drop-down menu list of multiple 
groups?  Instead of just selecting one group (e.g. i.maxlik) select more 
than one (multiple)?


Thanks
Best regards
Antonio


__ Information from ESET NOD32 Antivirus, version of virus signature 
database 5371 (20100816) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script for Reclass from values

2010-07-23 Thread Luisa Peña
Greetings I have a list of values stored in a text file like this
20.4 24.5 45.6 4.56
20.5 34.5. 34.5 43.4
1.2 5.3 43.4 3.3
...
Each column corresponds to an image and each line to a classe (e.g. pixels
in image 1 with value 20.4 and 24.5 in image 2 and 45.6 in image 3 and 4.56
in image4 corresponds to class1). What I want to do is to obtain a single
classification map but based on these values for all 4 maps. Is it possible?

Thanks
Luisa
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script for Reclass from values

2010-07-20 Thread Luisa Peña
Greetings I have a list of values stored in a text file like this
20.4 24.5 45.6 4.56
20.5 34.5. 34.5 43.4
1.2 5.3 43.4 3.3
...
Each column corresponds to an image and each line to a classe (e.g. pixels
in image 1 with value 20.4 and 24.5 in image 2 and 45.6 in image 3 and 4.56
in image4 corresponds to class1). What I want to do is to obtain a single
classification map but based on these values for all 4 maps. Is it possible?

Thanks
Luisa
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python Script to retrieve Location's GCS

2010-06-18 Thread Kim Besson
Greetings
Is there a Pythons Functionality to retrieve Locations Geographic
Coordinates System? But in this case can these coordinates be used in
GDALWARPs as as an Geographic Coordinates input ?

Thanks

Best regards,
Kim
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] Python script in grass

2010-02-06 Thread Margherita Di Leo

Hi List,
please a little help with my python script. I don't know how to say in 
python:


echo 589541.859564|4473239.49338| | v.in.ascii output=outlet

Thank you
Cheers

--
Eng. Margherita Di Leo
Ph.D. Candidate
Methods and Technologies for Environmental Monitoring
Department of Environmental Engineering and Physics (DIFA)

University of Basilicata 
Campus Macchia Romana
85100 - Potenza 
Italy


Office: +39-0971205363
Fax: +39-0971205160





___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python script in grass

2010-02-06 Thread Christian Kaiser
Ciao,

What you can do, is write your point into a temporary file and then read in 
again this file using v.in.ascii command. For example something like this:

import grass.script as grass
import os

# For temporary files
import tempfile as pytempfile

# For deleting temp directory
import shutil

# Create temporary directory
tmpdir = pytempfile.mkdtemp()
tmp_vec = tmpdir + '/vec.ascii'

fp = open(tmp_points, 'w')
fp.write('589541.859564|4473239.49338|')
fp.close()

# Read in your point
grass.run_command(v.in.ascii, input=tmp_vec, output=outmap, fs='|', skip=0, 
x=1, y=2, cat=3, columns='x double precision, y double precision, cat int')

# Remove the temporary directory and all the files in it
shutil.rmtree(tmpdir)


Of course, some error checking would be nice...

HTH
Christian



On 6 févr. 2010, at 10:55, Margherita Di Leo wrote:

 Hi List,
 please a little help with my python script. I don't know how to say in python:
 
 echo 589541.859564|4473239.49338| | v.in.ascii output=outlet
 
 Thank you
 Cheers
 
 -- 
 Eng. Margherita Di Leo
 Ph.D. Candidate
 Methods and Technologies for Environmental Monitoring
 Department of Environmental Engineering and Physics (DIFA)
 
 University of Basilicata Campus Macchia Romana
 85100 - Potenza Italy
 
 Office: +39-0971205363
 Fax: +39-0971205160
 
 
 
 
 
 ___
 grass-user mailing list
 grass-user@lists.osgeo.org
 http://lists.osgeo.org/mailman/listinfo/grass-user


___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Python script in grass

2010-02-06 Thread Glynn Clements

Margherita Di Leo wrote:

 please a little help with my python script. I don't know how to say in 
 python:
 
 echo 589541.859564|4473239.49338| | v.in.ascii output=outlet

To do it using the grass.script module:

grass.write_command('v.in.ascii', output = 'outlet',
stdin = 589541.859564|4473239.49338|)

or (more suitable when you want to feed a lot of data):

p = grass.feed_command('v.in.ascii', output = 'outlet')
p.stdin.write(589541.859564|4473239.49338|)
p.stdin.close()
p.wait()

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


[GRASS-user] python script: grass.list_grouped for other mapset

2009-08-12 Thread Milton Cezar Ribeiro
Dear all,

I am building some codes on python that read grass informations.
If I start msys - grass - select my location  mapset, I am able
to both get the list of rasters or read a raster map.

To get the list of raster maps I use something like:
   landscape_list=grass.list_grouped('rast')['PERMANENT']

This works fine. But my application will need to read information
for more than one mapset at same time, and the are not stored
on PERMANENT.

As I am working with landscape simulations,
for each landscape (I have about 30,000 landscapes with 512x512 pixels)
I generated about 5 new maps, and for convenience  (including backup)
echo set of maps are stored on different mapsets.

I really need to stay conected on one mapset and get the list of rasters
as well as the raster maps from other mapsets, and this need to be done
on the fly of my script.

Any hint are welcome.

cheers

milton
brazil=toronto
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user