PostGIS: Difference between revisions
Line 89: | Line 89: | ||
=== Direct write access === | === Direct write access === | ||
'''Warning:''' direct write access via OGR library is currently under development (GRASS 7 only). | '''Warning:''' direct write access via OGR library is currently '''under development''' (GRASS 7 only). | ||
GRASS 7 allows to write output vector map directly via OGR library. For defining output vector data format is designed {{cmd|v.external.out|version=70}} module. For example | GRASS 7 allows to write output vector map directly via OGR library. For defining output vector data format is designed {{cmd|v.external.out|version=70}} module. For example |
Revision as of 14:49, 27 November 2012
When accessing PostGIS data in GRASS you can
- import PostGIS (simple features) data into GRASS topological format or,
- link PostGIS data as GRASS vector maps
See also working with external data in GRASS 7.
Help pages
- Vector data processing in GRASS GIS help page
- Database management in GRASS GIS help page
- SQL support in GRASS GIS help page
- PostgreSQL DB driver in GRASS help page
- ODBC DB driver in GRASS help page
Import into GRASS
- v.in.ogr - imports geometry data from PostGIS into native GRASS vector format, attributes are stored using default DB settings (see db.connect for details)
- v.clean - clean up data after import
Note 1: There is a fundamental difference between the PostGIS format which is non-topological (OGC simple feature-based) and the internal GRASS format which is topological and which, thus, does not really allow for overlapping polygons. You can digitize them, but they are not really useful...
Note 2: GRASS 7 allows also reading and writing topological PostGIS data (see PostGIS Topology for details).
Link to GRASS
- input maps: v.external
- in GRASS 6 the map links created by v.external are read only
- GRASS 7 supports also write access to the map links, see working with external data in GRASS 7 for details
- output maps: v.external.out (GRASS 7 only)
You can register a PostGIS table in GRASS using v.external. The command bellow creates a link to the table 'lakes' located in database 'pgis_nc'.
v.external dsn=PG:dbname=pgis_nc layer=lakes
The command creates in the current mapset new vector map 'lakes', the module also builds pseudo-topology for this map,
To link a PostGIS table from non-public schema
v.external dsn=PG:<connection sting> layer=<schema>.<layer> output=<layer>
Important note: In GRASS 6 the created links area read-only. GRASS 7 also allows to modify linked layers directly via OGR or PostGIS data provider, see working with external data in GRASS 7 for details.
Export to PostGIS
To export GRASS vector map layer as PostGIS table use v.out.ogr, eg.
v.out.ogr in=lakes@PERMANENT dsn=PG:dbname=pgis_nc format=PostgreSQL type=area
Note that exporting data can be quite time-consuming task, especially when input vector map attributes are stored in DBF format. It's recommended to store attribute data in SQLite format rather then in old-fashioned DBF format.
See also wxGUI Graphical Modeler.
Direct access to PostGIS data (GRASS 7 only)
Important note: Direct read/write access is available only in GRASS 7.
Direct read access
GRASS 7 allows to access PostGIS data directly via virtual mapset called 'OGR'. In this case parameter map or input is used for OGR data source and layer for table. In the command bellow is accessed PostGIS table 'lakes' from database 'pgis_nc'.
v.info map=PG:dbname=pgis_nc@OGR layer=lakes
To access table from non-public schema (or schemas which are not in the search path), type
v.info map=PG:<OGR data source> layer=<schema>.<table>
When accessing external data directly you don't need to create a map link by v.external. The major drawback of direct read access is that the pseudo-topology is built each time when accessing the data. From this point of view the direct access is useful when accessing data once or few times, otherwise is better to link the data by v.external.
Direct write access
Warning: direct write access via OGR library is currently under development (GRASS 7 only).
GRASS 7 allows to write output vector map directly via OGR library. For defining output vector data format is designed v.external.out module. For example
v.external.out dsn=PG:dbname=pgis_nc format=PostgreSQL
causes that every newly created vector map will be stored as PostGIS layer in database 'pgis_nc' without any data elements created in the current mapset. Such PostGIS layer can be linked afterwards via v.external or accessed directly as described in the section above. For example
v.extract input=lakes out=reservoir where="FTYPE = 'RESERVOIR'" v.info map=PG:dbname=pgis_nc@OGR layer=reservoir
To switch back to GRASS native format enter
v.external.out -r