PostGIS topology support has shipped in PostGIS 2.0. It consists in a topology schema model and accessory functions. A lot of working is going on in the PostGIS 2.0 release. Please refer to PostGIS Topology section of manual for details.
With topology support enabled you can store topological elements, define TopoGeometry objects as being composed by these elements, convert TopoGeometry objects to simple Geometry objects to use all functions defined on the latter.
Topological elements are Faces, Edges and Nodes.
Enabling PostGIS Topology in Versions Earlier Than 2.0
In order to enable PostGIS support you need to be equipped with the following:
- A schema-aware PostgreSQL installation (7.3 and up)
- PostGIS 1.3.x or up
- GEOS 2.1 or up
Move under the topology/ directory and run:
$ make $ psql -f topology.sql <your_test_database>
No C libs involved so far, so no need to make install.
To uninstall/disable just drop the 'topology' schema. It contains all the objects installed by the topology.sql script (remember to cascade).
Creating a Topology
Topology data are stored in named SCHEMAs, where the topology name is the name of the SCHEMA containing its data.
A catalogue of avalable topologies is kept under the "topology"."topology" table.
To create/destroy a topology:
SELECT topology.CreateTopology(name, [srid], [tolerance]); SELECT topology.DropTopology(name);
Loading Topology data
To load topology data in a topology you can use INSERT statements filling up the Edge, Node and Face relations under your topology schema:
- edge_id integer PRIMARY KEY
- start_node integer REFERENCES Node.node_id)
- end_node integer REFERENCES Node.node_id)
- next_left_edge integer REFERENCES abs(Edge.edge_id)
- next_right_edge integer REFERENCES abs(Edge.edge_id)
- left_face integer REFERENCES Face.face_id
- right_face integer REFERENCES Face.face_id
- geom geometry ( a linestring )
- node_id integer PRIMARY KEY
- containing_face integer REFERENCES Face.face_id
- geom geometry ( a point )
- face_id integer PRIMARY KEY
- mbr geometry ( can be NULL )
Details on semantic are contained in the SQL/MM specification, which this implementation follows as for these views structure.
Convert shape file and simple feature data to Postgis Topology
Here is a ref to script that copies small tables with simple feature data into to a postgis topology table which are created on the fly. This purpose of this script is to make it easier to Postgis Topology on your simple feature data.
(This script is not created for performance but just to make easier to to get people in to test Postgis Topology on small datasets.)
The script taks at minimum two parameters
1) Parameters one is schema1.simple_feature_table1 used for input.
2) Parameter two is the schema2.topology_table_name for the result.
(More info about parameters can found in the script it self).
Here is an example of how to call it
The result of this file will two tables :
Table 1 : topo_test.rein_konsesjonomr_flate with the attributtes from table one and a topology column
Table 2 : topo_test.rein_konsesjonomr_flate_v with attributtes from table one and a geometry columns casted from the portgis topology column.
To check out the data use for instance qgis with Plugin: PostGIS Topology Editor from Strk .
if you have some shape files added here som you can test here is a example where I uses shp2pgsl to gether with this script.
-- # create schema if not exist -- psql sl -c'CREATE SCHEMA IF NOT EXISTS test;' -- # copy data from shape file to postgis -- shp2pgsql -W ISO-8859-1 -d -D -s 4258 data/muni_surface.shp test.muni_surface | psql sl; -- #copy data from simple feature to topology -- psql sl -c "SELECT topo_help_sf_to_topology_case_1('test.muni_surface','test_topo.muni_surface');" 2>> /tmp/importfromtemp.log;
To instal it
git clone https://github.com/NibioOpenSource/pgtopo_update_sql cd pgtopo_update_sql/src/test/sql/import cat script/func_* | psql
To verify validity of a topology:
SELECT * FROM topology.ValidateTopology(name);
The return set will contain references to elements involved in the invalidity.
Copying a Topology
Editing a Topology
Working with TopoGeometry objects
Getting simple Geometry values from TopoGeometry objects
You currently need to explicit call the TopoGeometry=>Geometry cast function. This will probably be made implicit when the code is more tested:
Testing Topology implementation
Tests are included under the topology/test directory. Run make w/out args to see a list of supported targets.