|
GRASS 5.7: Geometry management, optionally with connections to DBMS
|
Tutorial HOME |
Table of contents
README
GRASS 5.7 is currently under development. In case the examples described here do
not work properly, you are kindly invited to send us further examples and/or
code bugfixes/enhancements.
To get debug information, set variable (set to 0 for debug silence) to 1..5:
The vector geometry management was completely changed in GRASS 5.7 to
achieve more flexibility, support for 3D vectors etc. At time of this
writing following options for vector geometry storage are available:
- native 5.7 vector format (default)
- PostGRASS (PostgreSQL/PostGIS based in a database)
- SHAPE format
- OGR supported formats (ArcInfo, GML etc)
The 5.0 sites format has been migrated into the new vector format.
Vector networking is implemented.
Managing the default settings
|
Per default vector map geometry is stored locally in native 5.7 format.
External vector maps can be virtually linked into GRASS with:
Supported formats depend on the OGR installation, usually SHAPE,
MapInfo, DGN, Arc/Coverages etc are supported.
In GIS the vector data model is used for geographic phenomena which may be
represented by geometric entities (primitives) like points, lines, and
areas. In GRASS 5.7 the vector data model includes the description of
topology, where besides the coordinates describing the location of the
points, lines, boundaries and centroids also their spatial relations are
stored. In general topological GIS require a data structure, where common
boundary between two adjacent areas is stored as a single line, simplifying
the map maintenance.
The new GRASS 5.7 vector architecture overcomes the vector limitations of
GRASS 4.x-5.0 by extending the vector support with attributes stored in
external relational databases and by new 3D capabilities. Besides internal
file based storage the geometry may alternatively be stored in PostGIS
database. This enables users to maintain large data sets with simultaneous
write access. External GIS formats such as SHAPE-files may be used directly
without necessity of format conversion.
There are various possibilities to store vector geometry in GRASS 5.7:
- native format (default)
- OGR library supported formats: SHAPE, Arc-Coverage, DGN, PostgreSQL/PostGIS, GML etc.
See some vector API details here.
Geometry stored in native format
|
Per default the vector geometry is stored in the GRASS 5.7 location
in native format.
For attributes management, see various
methods. You can store attributes in a DBF table, or connect to ODBC,
PostgreSQL or mySQL.
Geometry stored in SHAPE file: Registering a map in SHAPE format
|
Maps in OGR supported formats such as the SHAPE format can be
virtually linked into GRASS 5.7 without importing the files:
v.external dsn=/home/user/shape_data layer=markveggy.shp output=markveggy
#get info about vector map:
v.info markveggy
#look at the map:
d.mon x0
d.vect markveggy
Read also Attributes stored in DBF table.
Geometry access through OGR
|
Now implemented in GRASS 5.7, giving you full MapServer/PostGIS
support. See v.in.ogr and v.out.ogr.
Generating vector geometry from spreadsheet or DBMS table
|
It may happen that you receive a list of points with attributes in
a spreadsheet table (DBF, CSV, Excel, etc). It's fairly easy to
generate a map from such a table, provided that coordinates columns are
present.
Here we assume to have a PostgreSQL table with columns 'east', 'north', 'quota' (z)
and some attribute columns.
v.in.db driver=pg database="host=myserver.itc.it,dbname=mydb,user=name" \
table=meteostations x=east y=north z=quota key=id output=meteostations
This command creates the new vector map (native format) with attributes table in a DBF file.
We can display and query some points:
d.vect meteostations
d.what.vect
It will open a window showing the attributes for the selected point. If you have permissions
to modify the table, you could even modify the attributes now within this attribute popup-window.
Generating vector geometry from PostGIS table
|
It is also possible to create a GRASS map from a PostGIS
table where the coordinates are in PostGIS GEOMETRY:
present.
Here we assume to have a PostgreSQL table with columns 'x', 'y', 'num' (ID)
and some attribute columns:
#establish the connection with db tools to verify the table:
db.connect dr=pg data="host=pgserver.itc.it,user=myname,dbname=mitris"
db.connect -p
#see tables:
db.tables -p
#NOTE - it would be better to create a VIEW of the table:
echo "select x(geo),y(geo),num from localizzazione" | db.select -c
#generate map from geometry/ID:
echo "select x(geo),y(geo),num from localizzazione" | db.select -c | v.in.ascii out=mitris
v.info mitris
The command above created the new vector map (native format). Finally we have to
link the attributes table to the map:
v.db.connect -p
v.db.connect -o mitris dr=pg
data="host=pgserver.itc.it,user=myname,dbname=mitris" table=localizzazione key=num
Now we can display and query some points:
d.vect mitris
d.what.vect
Note that the map was linked to the table. When you delete the map, also
the PostGIS table 'localizzazione' would be deleted! (This behaviour should
be discussed...).
Generating vector geometry from XY or XYZ file
|
If you receive a file with coordinate pairs (and maybe z elevation),
you can easily generate a vector map. Store the points into an
ASCII file, e.g. 'coords.txt' separated by '|' (pipe character).
For 2D maps:
1664619|5103481
1664473|5095782
1664273|5101919
1663427|5105234
1663709|5102614
Import into GRASS:
cat coords.txt | v.in.ascii out=my2dmap
#add missing category numbers (to be able to assign attributes)
v.category in=my2dmap out=my2dmap_final op=add
v.category my2dmap_final op=report
or, 3D case:
1664619|5103481|445
1664473|5095782|534
1664273|5101919|532
1663427|5105234|454
1663709|5102614|525
Import into GRASS:
cat coords.txt | v.in.ascii -z out=my3dmap
#add missing category numbers (to be able to assign attributes)
v.category in=my3dmap out=my3dmap_final op=add
v.category my3dmap_final op=report
Finally, with 'v.db.connect' an attribute table can be assigned.
Further Links (related software, SQL reference etc).