PostGIS: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
Line 35: Line 35:
* output maps: {{cmd|v.external.out|version=70}} (GRASS 7 only)
* output maps: {{cmd|v.external.out|version=70}} (GRASS 7 only)


You can register a PostGIS layer into GRASS using {{cmd|v.external}}, eg. PostGIS layer 'lakes' from database 'pgis_nc'
You can register a PostGIS table in GRASS using {{cmd|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
  v.external dsn=PG:dbname=pgis_nc layer=lakes


It will create in the current mapset new vector map 'lakes', the module also builds pseudo-topology for this map,
The command creates in the current mapset new vector map 'lakes', the module also builds pseudo-topology for this map,


To link a PostGIS layer from non-public schema
To link a PostGIS table from non-public schema


  v.external dsn=PG:<connection sting> layer=<schema>.<layer> output=<layer>
  v.external dsn=PG:<connection sting> layer=<schema>.<layer> output=<layer>


''Important note:'' In GRASS 6 this link is read-only, in GRASS 7 it's possible to modify linked layers directly via OGR library, see [[Working with external data in GRASS 7#Vector data|working with external data in GRASS 7]].
''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#Vector data|working with external data in GRASS 7]] for details.
 
You can also link a GRASS layer to a PostgreSQL attribute table, though {{cmd|v.db.connect}}.


{{YouTube|eB3_SmE3E9Q|desc=Link PostGIS layers as GRASS vector maps}}
{{YouTube|eB3_SmE3E9Q|desc=Link PostGIS layers as GRASS vector maps}}

Revision as of 14:18, 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

Import into GRASS

  1. 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)
  2. 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).


Importing PostGIS layers into GRASS

Link to GRASS

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.


Link PostGIS layers as GRASS vector maps

Export to PostGIS

To export GRASS vector map layer to PostGIS layer 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. For example when exporting vector map 'lakes' from North Carolina sample dataset.

  • Attributes in DBF format
real    1m15.072s
user    1m6.160s
sys     0m3.824s
  • Attributes in SQLite format
real    0m11.796s
user    0m5.564s
sys     0m4.148s


Creating model which exports all GRASS vector maps from given mapset to PostGIS database
Creating model which exports all GRASS vector maps from given mapset to PostGIS database (including schema)

1) create schema with name of input mapset

2) copy vector maps from selected mapset to the current and stores their attributes in SQLite database

3) exports GRASS vector maps to the selected PostGIS database

See also wxGUI Modeler.

Direct access to PostGIS data

Important note: Direct read/write access is implemented only in GRASS 7.

Direct read access

GRASS 7 enables the users to access PostGIS layers directly via virtual mapset 'OGR' and modules parameters map=OGR_datasource@OGR and layer=OGR_layer, eg. to access PostGIS layer 'lakes' from database 'pgis_nc'

v.info map=PG:dbname=pgis_nc@OGR layer=lakes

The direct read access avoids need of creating a link via v.external and accessing PostGIS data directly without creating any data elements in the current mapset. The main drawback of direct read access is that the pseudo-topology is built each time when accessing the data.

Direct write access

Note: 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

See also