GRASS 5.7 Usage Examples - basics |
Convert from/to GRASS 5.0 vector maps |
You can run 5.7 (at the same time) on a 5.0 location without problems.
To convert 5.0 vector data to 5.7, run:
v.convert in=vectormap_from_50 out=vectormap v.db.connect -p vectormap g.region -p vect=vectormap d.vect vectormap d.m
Convert 5.7 vector map back to GRASS 5.0
v.out.ascii -o newmap.clean out=newmap.clean
Geometry storage in various formats |
Attribute storage in (external) databases |
Example: Import 3D DXF/DWG vector data and NVIZ 3D vector visualization |
Since GRASS 5.7 supports 3D vector data, we can easily import DXF and DWG files and display them with NVIZ:
v.in.dwg in=WATRTOWR.DXF out=watertowerXY
v.info watertowerXY
g.region vect=watertowerXY -p r.mapcalc "dem_flat=-1874" nviz el=dem_flat vect=watertowerXY
Transformation to UTM coordinates:
Most DXF/DWG drawings are done within XY coordinates. To transform them to
a national grid, we can use 'v.transform' with a 4 point transformation.
We select UTM coordinates where to place the water tower and query the
elevation (d.what.rast elevation.dem). We decide that the xy-extension of
the water tower be 20m and it's height 100m (defined later).
The required transformation points file for 'v.transform' may look like this
(L: left, R: right, U: upper, L: lower, N, S, W, E):
# XY UTM #------- # UL NW # UR NE # LR SW # LL SE -584 585 598000 4920770 580 585 598020 4920770 580 -600 598020 4920750 -584 -600 598000 4920750
v.transform -t in=watertowerXY out=watertowerUTM points=wt.points zscale=0.04 zshift=1320 g.region rast=elevation.dem nviz el=elevation.dem vect=watertowerUTM
Example: Extract vector data from map to new map with SQL statements |
#note: strings must be quoted: v.extract markveggy.shp output=markveggy.1 where="VEGTYPE = 'PS'" #selection with OR: # Take care to use parenthesis on both sides of or ( otherwise the result m # be bogus). Single clause of one comparison should also be enclosed in # brackets standing on one of the sides of 'or' like in: # select * from tab where (c1 < 5) or (c2 >1) v.extract markveggy.shp out=markveggy.1 new=1 where="(VEGTYPE = 'Wi') or (VEGTYPE = 'PS') or (PRIME_TYPE='Wi')" #to use the new map, you should link attributes to it. This can #be the table from the parent map. Edit the DB file and add a row for #the new map (here: markveggy.1) #Then check connection: v.db.connect -p markveggy.1 #Query new map: d.what.vect #Or you can display with labels attached to the vector: First we check # from which column labels to display: db.columns tab=markveggy d.vect markveggy.1 att=vegtype display=attr lcolor=red
Example: Vector map import data using OGR |
v.in.ogr d=wdb2_unep.mif out=wdb2 lay=wdb2_unep
If the map contains areas, we must clean off collinear boundary vectors as
well as multiple centroids with 'v.clean'. This is needed, if you see for
example the message
WARNING: 2. centroid found in area 2
v.clean in=wdb2 out=wdb2_clean tool=rmdac
v.clean in=wdb2_clean out=wdb2_clean2 tool=rmdupl d.vect wdb2_clean2
Example: Vector map export data using OGR |
v.out.ogr input=multi typ=line dsn=/tmp layer=testogr
#Export to GML format (generates /tmp/testogr..gml file with layer 'testogr'):
v.out.ogr input=multi typ=line dsn=/tmp/testogr.gml layer=testogr format=GML
Example: SQL queries |
#Select from table 'markveggy' (in database 'grass57test') all rows where 'VEGTYPE = "IFA"': #NOTE: string attributes must be quoted: echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc #NOTE: for areas such selections only work when area centroids are present! d.erase d.vect markveggy.vegtype where="VEGTYPE = 'IFA'" d.what.vect -a
echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc #NOTE: for areas such selections only work when area centroids are present! d.erase d.vect markveggy.vegtype where="VEGTYPE = 'Wiii'" d.what.vect -a
Hints for GRASS-PostGIS: see e.g. this thread.
Example: Generating a vector map from a point data set |
db.connect driver=shp database=/ssi0/ssi/neteler/grassdata/botswanaLL/shp db.columns BOTSD90
echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h
Note that "shp_fid" column is generated on the fly. Now we can generate the vector points map (which was the sites file in GRASS 5.0):
echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h |v.in.ascii output=BOTSD90map
d.vect BOTSD90map
Finally we have to connect the table to the new vector map. Edit the DB file (if not there, generate it in the MAPSET directory) and add something like:
BOTSD90map 1 BOTSD90 shp_fid /ssi0/ssi/neteler/grassdata/botswanaLL/shp shp
Query some points:
d.what.vect -a BOTSD90map
v.out.ogr BOTSD90map type=point dsn=./ layer=BOTSD90map
Example: Copying a database table from one DBMS to another within GRASS 5.7 |
db.copy from_driver=dbf from_database=./ from_table=census1990wet \ to_driver=pg to_database="host=pgserver,dbname=grass57test" \ to_table=census1990wetB
psql -h pgserver grass57test=> select count(*) from census1990wetB;
Distances from one climatic station to others (point to point distances) |
#show all meteo stations: d.vect meteostations displ=shape,attr att=name #highlight station of interest: d.vect meteostations displ=shape,attr att=name where="name='Dos Gaggio'" col=yellow #extract station of interest (vector point) into a new map: v.extract meteostations out=stationdg where="name='Dos Gaggio'" d.vect stationdg col=blue lcol=blue
Now we can calculate the distances from all stations to the selected station:
#(take care for the 'max' parameter for maximum distance): v.distance -p from=meteostations to=stationdg col=dist up=dist #print with distances sorted: v.distance -p from=meteostations to=stationdg col=dist up=dist | sort -n -k 2 -t '|' [...] from_cat|dist 82|1580.509095 518|5560.323732 54|7458.190196 81|9026.606893 83|9093.747797 647|9820.778075 57|10017.483616 [...]
Clipping vector maps to a particular region |
#set region of interest with: d.zoom/g.region #create area map with single area of current region: v.in.region out=clipregion #cut map according to that area map: v.overlay ainput=clipregion binput=soils out=soils_clipped operator=and #look at the clipped vector map: d.vect soils_clipped
Further Links (related software, SQL reference etc).