[GRASS-user] How to manipulate PostGIS attributes from GRASS

Jonah Duckles jonahd at gmail.com
Mon Apr 19 12:13:20 EDT 2010


I often do something similar taking a random point-sample of a given
raster using v.out.ascii and r.what.  r.what is nice because you can
string together multiple rasters that you want to sample from.  Say
multiple time-steps in a timeseries of rasters etc.

What I tend to do is something like:
tablename=myschema.mytable
v.random n=1000 out=random_points
v.out.ascii random_points fs=" " | r.what myraster | sed 's/|/,/g' |
sed "s/^/insert into ${tablename} values (/g;s/$/)/g" | psql -h
localhost dbname

You could probably streamline for your needs by reading with
v.external, then updating the table with update statements piped to
psql client.  You'll want to check and makes sure the v.out.ascii's id
column is the same as the ogc_fid in your postgis table.
v.external dsn="PG:..." layer=orig_table out=orig_table
# build insert or update statements with SED
v.out.ascii orig_table fs=" " | r.what elevation | sed 's/|/,/g' | sed
"s/^/insert into tablename values (/g;s/$/)/g" | psql -h localhost
dbname

You could change this to an update statement and have it update the
table directly via the psql client.

Jonah

2010/4/18 Micha Silver <micha at arava.co.il>
>
> I have a PostGIS point layer. I've added an elevation column and I want to insert values for elevation using v.what.rast . What's the best way to do this?
>
> I tried:
> 1- v.external, but that's a read only connection.
> 2- v.in.ogr -t (to avoid creating a duplicate table), then v.db.connect to reconnect to the original PostGIS table. But that's *wrong* since there's no way that I can see to connect the cat values to the data table id column.
> Finally I did:
> 3- db.connect (to the PostGIS database)
> v.in.ogr dsn="PG:..." layer=orig_table out=new_table
> to create a new attrib table in Postgresql (geometry in GRASS)
> v.what.rast on the new vector to get elevation values into the new attrib table,
> then using a postgres update statement I copied the elevation attributes from the new table to the original. This does work OK.
>
> Is there smoother way?
> Thanks,
> Micha
>
>
> --
> Micha Silver
> http://www.surfaces.co.il/
> Arava Development Co.  +972-52-3665918
>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>


More information about the grass-user mailing list