[GRASS-user] Python Script not working now

Anna Petrášová kratochanna at gmail.com
Mon Aug 7 12:58:04 PDT 2023


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 <phillipjallen830 at gmail.com>
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 <module>
>     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: <D:/DEM_12_5/grassdata> 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 at 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 at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20230807/1ad96d0f/attachment.htm>


More information about the grass-user mailing list