= PostGIS data provider in GRASS 7 = PostGIS data can be accessed in GRASS 7 using [wiki:Grass7/VectorLib/OGRInterface 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 [http://www.iasma.it/ Fondazione Edmund Mach] and [http://www3.comune.trento.it/ Comune di Trento] (Italy). This data provider allows GRASS to ''read'' (see [source:grass/trunk/lib/vector/Vlib/read_pg.c read_pg.c]) and ''write'' (see [source:grass/trunk/lib/vector/Vlib/write_pg.c write_pg.c]) PostGIS data directly without any external library (in opposite to GRASS-OGR data provider). GRASS-PostGIS data provider is implemented using [http://www.postgresql.org/docs/9.2/static/libpq.html 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: ||'''Level'''||'''Sequantial access'''||'''Random access'''|| ||1 (without topology)||[http://grass.osgeo.org/programming7/read__pg_8c.html#a654fbf094198b35199d2eac15b2e1b13 V1_read_next_line_pg()]||[http://grass.osgeo.org/programming7/read__pg_8c.html#a7c66b4bca09746dadb01d428285ad70a V1_read_line_pg()] ||2 (with pseudo-topology/topology)||[http://grass.osgeo.org/programming7/read__pg_8c.html#a1cbb8c310ff27ace719f0d3b618b9705 V2_read_next_line_pg()]||[http://grass.osgeo.org/programming7/read__pg_8c.html#a52b5d48ac7fc14bc7482bfff6c992429 V2_read_line_pg()] Overview of subroutines to '''write''' PostGIS data: ||'''Level'''||'''Write new feature'''||'''Delete existing feature'''||'''Rewrite existing feature''' ||1 (without topology)||[http://grass.osgeo.org/programming7/write__pg_8c.html#a8127019aaeb9c61409f5777a48cbf95f V1_write_line_pg()]||[http://grass.osgeo.org/programming7/write__pg_8c.html#a15daeb6c7a1baf659be970b5fa9f3fba V1_rewrite_line_pg()]||[http://grass.osgeo.org/programming7/write__pg_8c.html#ae16c44528a13f7343d08f073015e83f5 V1_delete_line_pg()] ||2 (with pseudo-topology - simple feature access)||[http://grass.osgeo.org/programming7/write__sfa_8c.html#ab9cb491b38a015a1a3f419d03f7b02b1 V2_write_line_sfa()]||[http://grass.osgeo.org/programming7/write__sfa_8c.html#a35c4d5d9199aac66e331198d0281fa1f V2_rewrite_line_sfa()]||[http://grass.osgeo.org/programming7/write__sfa_8c.html#a8ffaeddebd619612152c1a2970f51c3a V2_delete_line_sfa()] ||2 (with topology - PostGIS Topology)||[http://grass.osgeo.org/programming7/write__pg_8c.html#a73b18af6db4e309f57bdd13ed3e9ad9e V2_write_line_pg()]||[http://grass.osgeo.org/programming7/write__pg_8c.html#a9d4a61ac5e9502d4bdc7db8ac2c46636 V2_delete_line_pg()]||[http://grass.osgeo.org/programming7/rewind__pg_8c.html#a04cab46522ad3fd51de8fa7e42b34497 V2_rewrite_line_pg()] See [http://grass.osgeo.org/programming7/vlibPg.html 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 [wiki:Grass7/VectorLib/OGRInterface#DirectOGRreadaccess 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. == Known issues == * `v.build opt=cdump` produces empty output * currently spatial and category index is build in the memory based on topo == 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 [http://grasswiki.osgeo.org/wiki/PostGIS 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 [wiki:Grass7/VectorLib/PostGISEditing PostGIS Topology editing] page and [http://grasswiki.osgeo.org/wiki/PostGIS_Topology wiki page] from GRASS Wiki.