wiki:Grass7/VectorLib/PostGISInterface

Version 41 (modified by martinl, 11 years ago) ( diff )

--

PostGIS data provider in GRASS 7

PostGIS data can be accessed in GRASS 7 using GRASS-OGR data provider or preferably by native GRASS-PostGIS data provider. Note that GRASS-OGR data provider supports only simple feature access.

Development of PostGIS data provider has been supported by Fondazione Edmund Mach and Comune di Trento (Italy).

This data provider allows GRASS to read (see read_pg.c) and write (see write_pg.c) PostGIS data directly without any external library (in opposite to GRASS-OGR data provider). GRASS-PostGIS data provider is implemented using libpq library.

Note that GRASS-PostGIS data provider is compiled only when GRASS is configured with --with-postgres switch.

Overview of subroutines to read PostGIS data:

LevelSequantial accessRandom access
1 (without topology)V1_read_next_line_pg()V1_read_line_pg()
2 (with pseudo-topology/topology)V2_read_next_line_pg()V2_read_line_pg()

Overview of subroutines to write PostGIS data:

LevelWrite new featureDelete existing featureRewrite existing feature
1 (without topology)V1_write_line_pg()V1_rewrite_line_pg()V1_delete_line_pg()
2 (with pseudo-topology - simple feature access)V2_write_line_sfa()V2_rewrite_line_sfa()V2_delete_line_sfa()
2 (with topology - PostGIS Topology)V2_write_line_pg()V2_delete_line_pg()V2_rewrite_line_pg()

See programmer's manual page for complete overview.

By default data are read/write as simple features. If PostGIS Topology schema is defined then the data provider reads/writes topological elements from provided schema.

Note that direct access similarly to GRASS-OGR data provider is not currently implemented. In other words to access PostGIS data (using GRASS-PostGIS data provider) is required to create a link using v.external command.

Simple features access

GRASS-PostGIS provider allows to read and write PostGIS data stored as simple features on level 1 (without topology) and also on level 2 (in this case a pseudo-topology is built over simple features).

Note: For forcing GRASS to use GRASS-OGR data provider instead of GRASS-PostGIS data provider when accessing/creating PostGIS specify environmental variable GRASS_VECTOR_OGR.

Export GRASS vector data to PostGIS as simple features:

v.out.postgis input=urbanarea dsn=PG:dbname=grass

Read example:

# create a link to PostGIS table
v.external dsn=PG:dbname=grass layer=urbanarea
# access data as normal vector map in GRASS
v.info map=urbanarea
...
 |----------------------------------------------------------------------------|
 | Map format:      PostGIS (PostgreSQL)                                      |
 | DB table:        public.urbanarea                                          |
 | DB name:         grass                                                     |
 | Geometry column: geom                                                      |
 | Feature type:    polygon                                                   |
 | Topology:        pseudo (simple features)                                  |
 |----------------------------------------------------------------------------|
...

Write example:

# define output vector format
v.external.out dsn=PG:dbname=grass format=PostgreSQL
# create random points map
v.random out=test_points n=1000
# access created external data
v.info test_points
...
 |----------------------------------------------------------------------------|
 | Map format:      PostGIS (PostgreSQL)                                      |
 | DB table:        public.test_points                                        |
 | DB name:         grass                                                     |
 | Geometry column: geom                                                      |
 | Feature type:    point                                                     |
 | Topology:        pseudo (simple features)                                  |
 |----------------------------------------------------------------------------|
...

For more examples see wiki page from GRASS Wiki.

Topological access

Export GRASS vector data to PostGIS as topological elements:

v.out.postgis -l input=urbanarea dsn=PG:dbname=grass

Read example:

# create a link to PostGIS table
v.external dsn=PG:dbname=grass layer=urbanarea
# access data as normal vector map in GRASS
v.info map=urbanarea
...
 |----------------------------------------------------------------------------|
 | Map format:      PostGIS (PostgreSQL)                                      |
 | DB table:        public.urbanarea                                          |
 | DB name:         grass                                                     |
 | Geometry column: geom                                                      |
 | Feature type:    polygon                                                   |
 | Topology:        PostGIS (schema: topo_urbanarea)                          |
 | Topology column: topo                                                      |
 |----------------------------------------------------------------------------|
...

Write example:

# define output vector format
v.external.out dsn=PG:dbname=grass format=PostgreSQL options=topology=YES
# create random points map
v.random out=test_points n=1000
# access created external data
v.info test_points
...
 |----------------------------------------------------------------------------|
 | Map format:      PostGIS (PostgreSQL)                                      |
 | DB table:        public.test_points                                        |
 | DB name:         grass                                                     |
 | Geometry column: geom                                                      |
 | Feature type:    point                                                     |
 | Topology:        PostGIS (schema: topo_test_points)                        |
 | Topology column: topo                                                      |
 |----------------------------------------------------------------------------|
...

For more information see PostGIS Topology editing page and wiki page from GRASS Wiki.

Note: See TracWiki for help on using the wiki.