Topology The PostGIS Topology types and functions are used to manage topological objects such as faces, edges and nodes. Sandro Santilli's presentation at PostGIS Day Paris 2011 conference gives a good synopsis of PostGIS Topology and where it is headed Topology with PostGIS 2.0 slide deck. Vincent Picavet provides a good synopsis and overview of what is Topology, how is it used, and various FOSS4G tools that support it in State of the art of FOSS4G for topology and network analysis. An example of a topologically based GIS database is the US Census Topologically Integrated Geographic Encoding and Reference System (TIGER) database. If you want to experiment with PostGIS topology and need some data, check out . The PostGIS topology module has existed in prior versions of PostGIS but was never part of the Official PostGIS documentation. In PostGIS 2.0.0 major cleanup is going on to remove use of all deprecated functions in it, fix known usability issues, better document the features and functions, add new functions, and enhance to closer conform to SQL-MM standards. Details of this project can be found at PostGIS Topology Wiki All functions and tables associated with this module are installed in a schema called topology. Functions that are defined in SQL/MM standard are prefixed with ST_ and functions specific to PostGIS are not prefixed. To build PostGIS 2.0 with topology support, compile with the --with-topology option as described in . Some functions depend on GEOS 3.3+ so you should compile with GEOS 3.3+ to fully utilize the topology support. This section lists the PostgreSQL data types installed by PostGIS Topology. Note we describe the casting behavior of these which is very important especially when designing your own functions. Topology Types getfaceedges_returntype A composite type that consists of a sequence number and edge number. This is the return type for ST_GetFaceEdges Description A composite type that consists of a sequence number and edge number. This is the return type for ST_GetFaceEdges function. sequence is an integer: Refers to a topology defined in the topology.topology table which defines the topology schema and srid. edge is an integer: The identifier of an edge. topogeometry A composite type that refers to a topology geometry in a specific topology, layer, having specific type (1:[multi]point, 2:[multi]line, 3:[multi]poly, 4:collection) with specific identifier id in the topology. The id uniquely defines the topogeometry in the topology. Description A composite type that refers to a topology geometry in a specific topology, layer, having specific type with specific id. The elements of a topogeometry are the properties: topology_id,layer_id,id integer,type integer. topology_id is an integer: Refers to a topology defined in the topology.topology table which defines the topology schema and srid. layer_id is an integer: The layer_id in the layers table that hte topogeometry belongs to. The combination of topology_id, layer_id provides a unique reference in the topology.layers table. type integer between 1 - 4 that defines the geometry type: 1:[multi]point, 2:[multi]line, 3:[multi]poly, 4:collection id is an integer: The id is the autogenerated sequence number that uniquely defines the topogeometry in the respective topology. Casting Behavior This section lists the automatic as well as explicit casts allowed for this data type Cast To Behavior geometry automatic See Also validatetopology_returntype A composite type that consists of an error message and id1 and id2 to denote location of error. This is the return type for ValidateTopology Description A composite type that consists of an error message and two integers. The function returns a set of these to denote validation errors and the id1 and id2 to denote the ids of the topology objects involved in the error. error is varchar: Denotes type of error. Current error descriptors are: coincident nodes, edge crosses node, edge not simple, edge end node geometry mis-match, edge start node geometry mismatch, face overlaps face,face within face, id1 is an integer: Denotes identifier of edge / face / nodes in error. id2 is an integer: For errors that involve 2 objects denotes the secondary edge / or node See Also This section lists the PostgreSQL domains installed by PostGIS Topology. Domains can be used like object types as return objects of functions or table columns. The distinction between a domain and a type is that a domain is an existing type with a check constraint bound to it. Topology Domains TopoElement An array of 2 integers generally used to identify a TopoGeometry component. Description An array of 2 integers used to represent the id and type of a topology primitive or the id and layer of a TopoGeometry. Sets of such pairs are used to define TopoGeometry objects (either simple or hierarchical). Examples SELECT ARRAY[1,2]::topology.topoelement; te ------- {1,2} --Example of what happens when you try to case a 3 element array to topoelement -- NOTE: topoement has to be a 2 element array so fails dimension check SELECT ARRAY[1,2,3]::topology.topoelement; ERROR: value for domain topology.topoelement violates check constraint "dimensions" See Also topoelementarray An array of element_id,element_type values. a bidimensional array of integers: '{{id,type}, {id,type}, ...}' Description An array of 1 or more topoelements ( a bidimensional array of integers: '{{id,type}, {id,type}, ...}'). So an array of 1 or more arrays each having 2 integers generally used to return an array of sets of element id and element type of a topology relation. For types currrently only 3 types are supported: node=1, edge=2, face=3 Examples SELECT '{{1,2},{4,3}}'::topology.topoelementarray As tea; tea ------- {{1,2},{4,3}} -- more verbose equivalent -- SELECT ARRAY[ARRAY[1,2], ARRAY[4,3]]::topology.topoelementarray As tea; tea ------- {{1,2},{4,3}} --using the array agg function packaged with topology -- SELECT topology.TopoElementArray_Agg(ARRAY[e,t]) As tea FROM generate_series(1,4) As e CROSS JOIN generate_series(1,3) As t; tea -------------------------------------------------------------------------- {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3},{4,1},{4,2},{4,3}} SELECT '{{1,2,4},{3,4,5}}'::topology.topoelementarray As tea; ERROR: value for domain topology.topoelementarray violates check constraint "dimensions" See Also , This section lists the Topology functions for building new Topology schemas, validating topologies, and managing TopoGeometry Columns Topology and TopoGeometry Management AddTopoGeometryColumn Adds a topogeometry column to an existing table, registers this new column as a layer in topology.layer and returns the new layer_id. text AddTopoGeometryColumn varchar topology_name varchar schema_name varchar table_name varchar column_name varchar feature_type text AddTopoGeometryColumn varchar topology_name varchar schema_name varchar table_name varchar column_name varchar feature_type integer child_layer Description Each TopoGeometry object belongs to a specific Layer of a specific Topology. Before creating a TopoGeometry object you need to create its TopologyLayer. A Topology Layer is an association of a feature-table with the topology. It also contain type and hierarchy information. We create a layer using the AddTopoGeometryColumn() function: This function will both add the requested column to the table and add a record to the topology.layer table with all the given info. If you don't specify [child_layer] (or set it to NULL) this layer would contain Basic TopoGeometries (composed by primitive topology elements). Otherwise this layer will contain hierarchical TopoGeometries (composed by TopoGeometries from the child_layer). Once the layer is created (it's id is returned by the AddTopoGeometryColumn function) you're ready to construct TopoGeometry objects in it Valid feature_types are: POINT, LINE, POLYGON, COLLECTION Availability: 1.? Examples -- Note for this example we created our new table in the ma_topo schema -- though we could have created it in a different schema -- in which case topology_name and schema_name would be different CREATE SCHEMA ma; CREATE TABLE ma.parcels(gid serial, parcel_id varchar(20) PRIMARY KEY, address text); SELECT topology.AddTopoGeometryColumn('ma_topo', 'ma', 'parcels', 'topo', 'POLYGON'); CREATE SCHEMA ri; CREATE TABLE ri.roads(gid serial PRIMARY KEY, road_name text); SELECT topology.AddTopoGeometryColumn('ri_topo', 'ri', 'roads', 'topo', 'LINE'); See Also , DropTopology Use with caution: Drops a topology schema and deletes its reference from topology.topology table and references to tables in that schema from the geometry_columns table. integer DropTopology varchar topology_schema_name Description Drops a topology schema and deletes its reference from topology.topology table and references to tables in that schema from the geometry_columns table. This function should be USED WITH CAUTION, as it could destroy data you care about. If the schema does not exist, it just removes reference entries the named schema. Availability: 1.? Examples Cascade drops the ma_topo schema and removes all references to it in topology.topology and geometry_columns. SELECT topology.DropTopology('ma_topo'); See Also DropTopoGeometryColumn Drops the topogeometry column from the table named table_name in schema schema_name and unregisters the columns from topology.layer table. text DropTopoGeometryColumn varchar schema_name varchar table_name varchar column_name Description Drops the topogeometry column from the table named table_name in schema schema_name and unregisters the columns from topology.layer table. Returns summary of drop status. NOTE: it first sets all values to NULL before dropping to bypass referential integrity checks. Availability: 1.? Examples SELECT topology.DropTopoGeometryColumn('ma_topo', 'parcel_topo', 'topo'); See Also TopologySummary Takes a topology name and provides summary totals of types of objects in topology text TopologySummary varchar topology_schema_name Description Takes a topology name and provides summary totals of types of objects in topology. Availability: 2.0.0 Examples SELECT topology.topologysummary('city_data'); topologysummary -------------------------------------------------------- Topology city_data (329), SRID 4326, precision: 0 22 nodes, 24 edges, 10 faces, 29 topogeoms in 5 layers Layer 1, type Polygonal (3), 9 topogeoms Deploy: features.land_parcels.feature Layer 2, type Puntal (1), 8 topogeoms Deploy: features.traffic_signs.feature Layer 3, type Lineal (2), 8 topogeoms Deploy: features.city_streets.feature Layer 4, type Polygonal (3), 3 topogeoms Hierarchy level 1, child layer 1 Deploy: features.big_parcels.feature Layer 5, type Puntal (1), 1 topogeoms Hierarchy level 1, child layer 2 Deploy: features.big_signs.feature See Also ValidateTopology Returns a set of validatetopology_returntype objects detailing issues with topology setof validatetopology_returntype ValidateTopology varchar topology_schema_name Description Returns a set of objects detailing issues with topology. Refer to for listing of possible errors. Availability: 1.? Enhanced: 2.0.0 more efficient edge crossing detection and fixes for false positives that were existent in prior versions. Examples SELECT * FROM topology.ValidateTopology('ma_topo'); error | id1 | id2 -------------------+-----+----- face without edges | 0 | See Also , This section covers the topology functions for creating new topologies. Topology Constructors CreateTopology Creates a new topology schema and registers this new schema in the topology.topology table. integer CreateTopology varchar topology_schema_name integer CreateTopology varchar topology_schema_name integer srid integer CreateTopology varchar topology_schema_name integer srid double precision tolerance integer CreateTopology varchar topology_schema_name integer srid double precision tolerance boolean hasz Description Creates a new schema with name topology_name consisting of tables (edge_data,face,node, relation and registers this new topology in the topology.topology table. It returns the id of the topology in the topology table. The srid is the spatial reference identified as defined in spatial_ref_sys table for that topology. Topologies must be uniquely named. The tolerance is measured in the units of the spatial reference system. If the tolerance is not specified defaults to 0. This is similar to the SQL/MM but a bit more functional. hasz defaults to false if not specified. Availability: 1.? Examples This example creates a new schema called ma_topo that will store edges, faces, and relations in Massachusetts State Plane meters. The tolerance represents 1/2 meter since the spatial reference system is a meter based spatial reference system SELECT topology.CreateTopology('ma_topo',26986, 0.5); Create Rhode Island topology in State Plane ft SELECT topology.CreateTopology('ri_topo',3438) As topoid; topoid ------ 2 See Also , , CopyTopology Makes a copy of a topology structure (nodes, edges, faces, layers and TopoGeometries). integer CopyTopology varchar existing_topology_name varchar new_name Description Creates a new topology with name new_topology_name and SRID and precision taken from existing_topology_name, copies all nodes, edges and faces in there, copies layers and their TopoGeometries too. The new rows in topology.layer will contain synthesized values for schema_name, table_name and feature_column. This is because the TopoGeometry will only exist as a definition but won't be available in any user-level table yet. Availability: 2.0.0 Examples This example makes a backup of a topology called ma_topo SELECT topology.CopyTopology('ma_topo', 'ma_topo_bakup'); See Also , ST_InitTopoGeo Creates a new topology schema and registers this new schema in the topology.topology table and details summary of process. text ST_InitTopoGeo varchar topology_schema_name Description This is an SQL-MM equivalent of CreateTopology but lacks the spatial reference and tolerance options of CreateTopology and outputs a text description of creation instead of topology id. Availability: 1.? &sqlmm_compliant; SQL-MM 3 Topo-Geo and Topo-Net 3: Routine Details: X.3.17 Examples SELECT topology.ST_InitTopoGeo('topo_schema_to_create') AS topocreation; astopocreation ------------------------------------------------------------ Topology-Geometry 'topo_schema_to_create' (id:7) created. See Also ST_CreateTopoGeo Adds a collection of geometries to a given empty topology and returns a message detailing success. text ST_CreateTopoGeo varchar atopology geometry acollection Description Adds a collection of geometries to a given empty topology and returns a message detailing success. Useful for populating an empty topology. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details -- X.3.18 Examples -- Populate topology -- SELECT topology.ST_CreateTopoGeo('ri_topo', ST_GeomFromText('MULTILINESTRING((384744 236928,384750 236923,384769 236911,384799 236895,384811 236890,384833 236884, 384844 236882,384866 236881,384879 236883,384954 236898,385087 236932,385117 236938, 385167 236938,385203 236941,385224 236946,385233 236950,385241 236956,385254 236971, 385260 236979,385268 236999,385273 237018,385273 237037,385271 237047,385267 237057, 385225 237125,385210 237144,385192 237161,385167 237192,385162 237202,385159 237214, 385159 237227,385162 237241,385166 237256,385196 237324,385209 237345,385234 237375, 385237 237383,385238 237399,385236 237407,385227 237419,385213 237430,385193 237439, 385174 237451,385170 237455,385169 237460,385171 237475,385181 237503,385190 237521, 385200 237533,385206 237538,385213 237541,385221 237542,385235 237540,385242 237541, 385249 237544,385260 237555,385270 237570,385289 237584,385292 237589,385291 237596,385284 237630))',3438) ); st_createtopogeo ---------------------------- Topology ri_topo populated -- create tables and topo geometries -- CREATE TABLE ri.roads(gid serial PRIMARY KEY, road_name text); SELECT topology.AddTopoGeometryColumn('ri_topo', 'ri', 'roads', 'topo', 'LINE'); See Also , , TopoGeo_AddPoint Adds a point to an existing topology using a tolerance and possibly splitting an existing edge. integer TopoGeo_AddPoint varchar toponame geometry apoint float8 tolerance Description Adds a point to an existing topology and return its identifier. The given point will snap to existing nodes or edges within given tolerance. An existing edge may be split by the snapped point. Availability: 2.0.0 See Also , , , TopoGeo_AddLineString Adds a linestring to an existing topology using a tolerance and possibly splitting existing edges/faces. integer TopoGeo_AddLineString varchar toponame geometry aline float8 tolerance Description Adds a linestring to an existing topology and return a set of edge identifiers forming it up. The given line will snap to existing nodes or edges within given tolerance. Existing edges and faces may be split by the line. Availability: 2.0.0 See Also , , , TopoGeo_AddPolygon Adds a polygon to an existing topology using a tolerance and possibly splitting existing edges/faces. integer TopoGeo_AddPolygon varchar atopology geometry aline float8 atolerance Description Adds a polygon to an existing topology and return a set of face identifiers forming it up. The boundary of the given polygon will snap to existing nodes or edges within given tolerance. Existing edges and faces may be split by the boundary of the new polygon. Availability: 2.0.0 See Also , , , This section covers topology functions for adding, moving, deleting, and splitting edges, faces, and nodes. All of these functions are defined by ISO SQL/MM. Topology Editors ST_AddIsoNode Adds an isolated node to a face in a topology and returns the nodeid of the new node. If face is null, the node is still created. integer ST_AddIsoNode varchar atopology integer aface geometry apoint Description Adds an isolated node with point location apoint to an existing face with faceid aface to a topology atopology and returns the nodeid of the new node. If the spatial reference system (srid) of the point geometry is not the same as the topology, the apoint is not a point geometry, the point is null, or the point intersects an existing edge (even at the boundaries) then an exception is thrown. If the point already exists as a node, an exception is thrown. If aface is not null and the apoint is not within the face, then an exception is thrown. Availability: 1.? &sqlmm_compliant; SQL-MM: Topo-Net Routines: X+1.3.1 Examples See Also , , , ST_AddIsoEdge Adds an isolated edge defined by geometry alinestring to a topology connecting two existing isolated nodes anode and anothernode and returns the edge id of the new edge. integer ST_AddIsoEdge varchar atopology integer anode integer anothernode geometry alinestring Description Adds an isolated edge defined by geometry alinestring to a topology connecting two existing isolated nodes anode and anothernode and returns the edge id of the new edge. If the spatial reference system (srid) of the alinestring geometry is not the same as the topology, any of the input arguments are null, or the nodes are contained in more than one face, or the nodes are start or end nodes of an existing edge, then an exception is thrown. If the alinestring is not within the face of the face the anode and anothernode belong to, then an exception is thrown. If the anode and anothernode are not the start and end points of the alinestring then an exception is thrown. Availability: 1.? &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.4 Examples See Also , , ST_AddEdgeNewFaces Add a new edge and, if in doing so it splits a face, delete the original face and replace it with two new faces. integer ST_AddEdgeNewFaces varchar atopology integer anode integer anothernode geometry acurve Description Add a new edge and, if in doing so it splits a face, delete the original face and replace it with two new faces. Returns the id of the newly added edge. Updates all existing joined edges and relationships accordingly. If any arguments are null, the given nodes are unknown (must already exist in the node table of the topology schema) , the acurve is not a LINESTRING, the anode and anothernode are not the start and endpoints of acurve then an error is thrown. If the spatial reference system (srid) of the acurve geometry is not the same as the topology an exception is thrown. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.12 Examples See Also ST_AddEdgeModFace Add a new edge and, if in doing so it splits a face, modify the original face and add a new face. integer ST_AddEdgeModFace varchar atopology integer anode integer anothernode geometry acurve Description Add a new edge and, if in doing so it splits a face, modify the original face and add a new face. Unless the face being split is the Universal Face, the new face will be on the right side of the newly added edge. Returns the id of the newly added edge. Updates all existing joined edges and relationships accordingly. If any arguments are null, the given nodes are unknown (must already exist in the node table of the topology schema) , the acurve is not a LINESTRING, the anode and anothernode are not the start and endpoints of acurve then an error is thrown. If the spatial reference system (srid) of the acurve geometry is not the same as the topology an exception is thrown. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.13 Examples See Also ST_RemEdgeNewFace Removes an edge and, if the removed edge separated two faces, delete the original faces and replace them with a new face. integer ST_RemEdgeNewFace varchar atopology integer anedge Description Removes an edge and, if the removed edge separated two faces, delete the original faces and replace them with a new face. Returns the id of a newly created face or NULL, if no new face is created. No new face is created when the removed edge is dangling or isolated or confined with the universe face (possibly making the universe flood into the face on the other side). Updates all existing joined edges and relationships accordingly. Refuses to remove an edge partecipating in the definition of an existing TopoGeometry. Refuses to heal two faces if any TopoGeometry is defined by only one of them (and not the other). If any arguments are null, the given edge is unknown (must already exist in the edge table of the topology schema), the topology name is invalid then an error is thrown. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.14 Examples See Also ST_RemEdgeModFace Removes an edge and, if the removed edge separated two faces, delete one of the them and modify the other to take the space of both. integer ST_RemEdgeModFace varchar atopology integer anedge Description Removes an edge and, if the removed edge separated two faces, delete one of the them and modify the other to take the space of both. Preferencially keeps the face on the right, to be symmetric with ST_AddEdgeModFace also keeping it. Returns the id of the face remaining in place of the removed edge. Updates all existing joined edges and relationships accordingly. Refuses to remove an edge partecipating in the definition of an existing TopoGeometry. Refuses to heal two faces if any TopoGeometry is defined by only one of them (and not the other). If any arguments are null, the given edge is unknown (must already exist in the edge table of the topology schema), the topology name is invalid then an error is thrown. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.15 Examples See Also ST_ChangeEdgeGeom Changes the shape of an edge without affecting the topology structure. integer ST_ChangeEdgeGeom varchar atopology integer anedge geometry acurve Description Changes the shape of an edge without affecting the topology structure. If any arguments are null, the given edge does not exist in the node table of the topology schema, the acurve is not a LINESTRING, the anode and anothernode are not the start and endpoints of acurve or the modification would change the underlying topology then an error is thrown. If the spatial reference system (srid) of the acurve geometry is not the same as the topology an exception is thrown. If the new acurve is not simple, then an error is thrown. If moving the edge from old to new position would hit an obstacle then an error is thrown. Availability: 1.1.0 Enhanced: 2.0.0 adds topological consistency enforcement &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details X.3.6 Examples SELECT topology.ST_ChangeEdgeGeom('ma_topo', 1, ST_GeomFromText('LINESTRING(227591.9 893900.4,227622.6 893844.3,227641.6 893816.6, 227704.5 893778.5)', 26986) ); ---- Edge 1 changed See Also ST_ModEdgeSplit Split an edge by creating a new node along an existing edge, modifying the original edge and adding a new edge. text ST_ModEdgeSplit varchar atopology integer anedge geometry apoint Description Split an edge by creating a new node along an existing edge, modifying the original edge and adding a new edge. Updates all existing joined edges and relationships accordingly. Availability: 1.? Changed: 2.0 - In prior versions, this was misnamed ST_ModEdgesSplit &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.9 Examples -- Add an edge -- SELECT topology.AddEdge('ma_topo', ST_GeomFromText('LINESTRING(227592 893910, 227600 893910)', 26986) ) As edgeid; -- edgeid- 3 -- Split the edge -- SELECT topology.ST_ModEdgeSplit('ma_topo', 3, ST_SetSRID(ST_Point(227594,893910),26986) ) As result; result ------------------------- 7 See Also , , , ST_ModEdgeHeal Heal two edges by deleting the node connecting them, modifying the first edge and deleting the second edge. Returns the id of the deleted node. int ST_ModEdgeHeal varchar atopology integer anedge integer anotheredge Description Heal two edges by deleting the node connecting them, modifying the first edge and deleting the second edge. Returns the id of the deleted node. Updates all existing joined edges and relationships accordingly. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.9 See Also ST_NewEdgeHeal Heal two edges by deleting the node connecting them, deleting both edges, and replacing them with an edge whose direction is the same as the first edge provided. int ST_NewEdgeHeal varchar atopology integer anedge integer anotheredge Description Heal two edges by deleting the node connecting them, deleting both edges, and replacing them with an edge whose direction is the same as the first edge provided. Returns the id of the new edge replacing the healed ones. Updates all existing joined edges and relationships accordingly. Availability: 2.0 &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X.3.9 See Also ST_MoveIsoNode Moves an isolated node in a topology from one point to another. If new apoint geometry exists as a node an error is thrown. REturns description of move. text ST_MoveIsoNode varchar atopology integer anedge geometry apoint Description Moves an isolated node in a topology from one point to another. If new apoint geometry exists as a node an error is thrown. If any arguments are null, the apoint is not a point, the existing node is not isolated (is a start or end point of an existing edge), new node location intersects an existing edge (even at the end points) then an exception is thrown. If the spatial reference system (srid) of the point geometry is not the same as the topology an exception is thrown. Availability: 1.? &sqlmm_compliant; SQL-MM: Topo-Net Routines: X.3.2 Examples -- Add an isolated node with no face -- SELECT topology.ST_AddIsoNode('ma_topo', NULL, ST_GeomFromText('POINT(227579 893916)', 26986) ) As nodeid; nodeid -------- 7 -- Move the new node -- SELECT topology.ST_MoveIsoNode('ma_topo', 7, ST_GeomFromText('POINT(227579.5 893916.5)', 26986) ) As descrip; descrip ---------------------------------------------------- Isolated Node 7 moved to location 227579.5,893916.5 See Also ST_NewEdgesSplit Split an edge by creating a new node along an existing edge, deleting the original edge and replacing it with two new edges. Returns the id of the new node created that joins the new edges. integer ST_NewEdgesSplit varchar atopology integer anedge geometry apoint Description Split an edge with edge id anedge by creating a new node with point location apoint along current edge, deleting the original edge and replacing it with two new edges. Returns the id of the new node created that joins the new edges. Updates all existing joined edges and relationships accordingly. If the spatial reference system (srid) of the point geometry is not the same as the topology, the apoint is not a point geometry, the point is null, the point already exists as a node, the edge does not correspond to an existing edge or the point is not within the edge then an exception is thrown. Availability: 1.? &sqlmm_compliant; SQL-MM: Topo-Net Routines: X.3.8 Examples -- Add an edge -- SELECT topology.AddEdge('ma_topo', ST_GeomFromText('LINESTRING(227575 893917,227592 893900)', 26986) ) As edgeid; -- result- edgeid ------ 2 -- Split the new edge -- SELECT topology.ST_NewEdgesSplit('ma_topo', 2, ST_GeomFromText('POINT(227578.5 893913.5)', 26986) ) As newnodeid; newnodeid --------- 6 See Also ST_RemoveIsoNode Removes an isolated node and returns description of action. If the node is not isolated (is start or end of an edge), then an exception is thrown. text ST_RemoveIsoNode varchar atopology integer anode Description Removes an isolated node and returns description of action. If the node is not isolated (is start or end of an edge), then an exception is thrown. Availability: 1.? &sqlmm_compliant; SQL-MM: Topo-Geo and Topo-Net 3: Routine Details: X+1.3.3 Examples -- Add an isolated node with no face -- SELECT topology.ST_RemoveIsoNode('ma_topo', 7 ) As result; result ------------------------- Isolated node 7 removed See Also Topology Accessors GetEdgeByPoint Find the edge-id of an edge that intersects a given point integer GetEdgeByPoint varchar atopology geometry apoint float8 tol Retrieve the id of an edge that intersects a Point The function returns an integer (id-edge) given a topology, a POINT and a tolerance. If tolerance = 0 then the point has to intersect the edge. If the point is the location of a node, then an exception is thrown. To avoid this run the GetNodeByPoint function. If the point doesn't intersect an edge, returns 0 (zero). If use tolerance > 0 and there is more than one edge near the point then an exception is thrown. If tolerance = 0, the function use ST_Intersects otherwise uses ST_DWithin. Availability: 2.0.0 - requires GEOS >= 3.3.0. Examples These examples use edges we created in SELECT topology.GetEdgeByPoint('ma_topo',geom, 1) As with1mtol, topology.GetEdgeByPoint('ma_topo',geom,0) As withnotol FROM ST_GeomFromEWKT('SRID=26986;POINT(227622.6 893843)') As geom; with1mtol | withnotol -----------+----------- 2 | 0 SELECT topology.GetEdgeByPoint('ma_topo',geom, 1) As nearnode FROM ST_GeomFromEWKT('SRID=26986;POINT(227591.9 893900.4)') As geom; -- get error -- ERROR: Two or more edges found See Also , GetFaceByPoint Find the face-id of a face that intersects a given point integer GetFaceByPoint varchar atopology geometry apoint float8 tol Description Retrieve the id of a face that intersects a Point. The function returns an integer (id-face) given a topology, a POINT and a tolerance. If tolerance = 0 then the point has to intersect the face. If the point is the location of a node, then an exception is thrown. To avoid this run the GetNodeByPoint function. If the point doesn't intersect a face, returns 0 (zero). If use tolerance > 0 and there is more than one face near the point then an exception is thrown. If tolerance = 0, the function uses ST_Intersects otherwise uses ST_DWithin. Availability: 2.0.0 - requires GEOS >= 3.3.0. Examples These examples use edges faces created in SELECT topology.GetFaceByPoint('ma_topo',geom, 10) As with1mtol, topology.GetFaceByPoint('ma_topo',geom,0) As withnotol FROM ST_GeomFromEWKT('POINT(234604.6 899382.0)') As geom; with1mtol | withnotol -----------+----------- 1 | 0 SELECT topology.GetFaceByPoint('ma_topo',geom, 1) As nearnode FROM ST_GeomFromEWKT('POINT(227591.9 893900.4)') As geom; -- get error -- ERROR: Two or more faces found See Also , , GetNodeByPoint Find the id of a node at a point location integer GetNodeByPoint varchar atopology geometry point float8 tol Retrieve the id of a node at a point location The function return an integer (id-node) given a topology, a POINT and a tolerance. If tolerance = 0 mean exactly intersection otherwise retrieve the node from an interval. If there isn't a node at the point, it return 0 (zero). If use tolerance > 0 and near the point there are more than one node it throw an exception. If tolerance = 0, the function use ST_Intersects otherwise will use ST_DWithin. Availability: 2.0.0 - requires GEOS >= 3.3.0. Examples These examples use edges we created in SELECT topology.GetNodeByPoint('ma_topo',geom, 1) As nearnode FROM ST_GeomFromEWKT('SRID=26986;POINT(227591.9 893900.4)') As geom; nearnode ---------- 2 SELECT topology.GetNodeByPoint('ma_topo',geom, 1000) As too_much_tolerance FROM ST_GeomFromEWKT('SRID=26986;POINT(227591.9 893900.4)') As geom; ----get error-- ERROR: Two or more nodes found See Also , GetTopologyID Returns the id of a topology in the topology.topology table given the name of the topology. integer GetTopologyID varchar toponame Description Returns the id of a topology in the topology.topology table given the name of the topology. Availability: 1.? Examples SELECT topology.GetTopologyID('ma_topo') As topo_id; topo_id --------- 1 See Also , , , GetTopologySRID Returns the SRID of a topology in the topology.topology table given the name of the topology. integer GetTopologyID varchar toponame Description Returns the spatial reference id of a topology in the topology.topology table given the name of the topology. Availability: 2.0.0 Examples SELECT topology.GetTopologySRID('ma_topo') As SRID; SRID ------- 4326 See Also , , , GetTopologyName Returns the name of a topology (schema) given the id of the topology. varchar GetTopologyName integer topology_id Description Returns the topology name (schema) of a topology from the topology.topology table given the topology id of the topology. Availability: 1.? Examples SELECT topology.GetTopologyName(1) As topo_name; topo_name ----------- ma_topo See Also , , , ST_GetFaceEdges Returns a set of ordered edges that bound aface includes the sequence order. getfaceedges_returntype ST_GetFaceEdges varchar atopology integer aface Description Returns a set of ordered edges that bound aface includes the sequence order. Each output consists of a sequence and edgeid. Sequence numbers start with value 1. Enumeration of each ring edges start from the edge with smallest identifier. Availability: 2.0 &sqlmm_compliant; SQL-MM 3 Topo-Geo and Topo-Net 3: Routine Details: X.3.5 Examples -- Returns the edges bounding face 1 SELECT (topology.ST_GetFaceEdges('tt', 1)).*; -- result -- sequence | edge ----------+------ 1 | -4 2 | 5 3 | 7 4 | -6 5 | 1 6 | 2 7 | 3 (7 rows) -- Returns the sequenc, edge id -- , and geometry of the edges that bound face 1 -- If you just need geom and seq, can use ST_GetFaceGeometry SELECT t.seq, t.edge, geom FROM topology.ST_GetFaceEdges('tt',1) As t(seq,edge) INNER JOIN tt.edge AS e ON abs(t.edge) = e.edge_id; See Also , , ST_GetFaceGeometry Returns the polygon in the given topology with the specified face id. geometry ST_GetFaceGeometry varchar atopology integer aface Description Returns the polygon in the given topology with the specified face id. Builds the polygon from the edges making up the face. Availability: 1.? &sqlmm_compliant; SQL-MM 3 Topo-Geo and Topo-Net 3: Routine Details: X.3.16 Examples -- Returns the wkt of the polygon added with AddFace SELECT ST_AsText(topology.ST_GetFaceGeometry('ma_topo', 1)) As facegeomwkt; -- result -- facegeomwkt -------------------------------------------------------------------------------- POLYGON((234776.9 899563.7,234896.5 899456.7,234914 899436.4,234946.6 899356.9, 234872.5 899328.7,234891 899285.4,234992.5 899145,234890.6 899069, 234755.2 899255.4,234612.7 899379.4,234776.9 899563.7)) See Also GetRingEdges Returns an ordered set of edges forming a ring with the given edge . getfaceedges_returntype GetRingEdges varchar atopology integer aring integer max_edges=null Description Returns an ordered set of edges forming a ring with the given edge. Each output consists of a sequence and a signed edge id. Sequence numbers start with value 1. A negative edge identifier means that the given edge is taken backward. You can pass a negative edge id to start walking backward. If max_edges is not null no more than those records are returned by that function. This is meant to be a safety parameter when dealing with possibly invalid topologies. Availability: 2.0 See Also , GetNodeEdges Returns an ordered set of edges incident to the given node. getfaceedges_returntype GetNodeEdges varchar atopology integer anode Description Returns an ordered set of edges incident to the given node. Each output consists of a sequence and a signed edge id. Sequence numbers start with value 1. A positive edge starts at the given node. A negative edge ends into the given node. Closed edges will appear twice (with both signs). Order is clockwise starting from northbound. This function computes ordering rather than deriving from metadata and is thus usable to build edge ring linking. Availability: 2.0 See Also , This section covers the functions for processing topologies in non-standard ways. Topology Processing Polygonize Find and register all faces defined by topology edges text Polygonize varchar toponame Description Register all faces that can be built out a topology edge primitives. The target topology is assumed to contain no self-intersecting edges. Already known faces are recognized, so it is safe to call Polygonize multiple times on the same topology. This function does not use nor set the next_left_edge and next_right_edge fields of the edge table. Availability: 2.0.0 See Also , AddNode Adds a point node to the node table in the specified topology schema and returns the nodeid of new node. If point already exists as node, the existing nodeid is returned. integer AddNode varchar toponame geometry apoint boolean allowEdgeSplitting=false boolean computeContainingFace=false Description Adds a point node to the node table in the specified topology schema. The function automatically adds start and end points of an edge when called so not necessary to explicitly add nodes of an edge. If any edge crossing the node is found either an exception is raised or the edge is splitted, depending on the allowEdgeSplitting parameter value. If computeContainingFace is true a newly added node would get the correct containing face computed. If the apoint geometry already exists as a node, the node is not added but the existing nodeid is returned. Availability: 2.0.0 Examples SELECT topology.AddNode('ma_topo', ST_GeomFromText('POINT(227641.6 893816.5)', 26986) ) As nodeid; -- result -- nodeid -------- 4 See Also , AddEdge Adds a linestring edge to the edge table and associated start and end points to the point nodes table of the specified topology schema using the specified linestring geometry and returns the edgeid of the new (or existing) edge. integer AddEdge varchar toponame geometry aline Description Adds an edge to the edge table and associated nodes to the nodes table of the specified toponame schema using the specified linestring geometry and returns the edgeid of the new or existing record. The newly added edge has "universe" face on both sides and links to itself. If the aline geometry crosses, overlaps, contains or is contained by an existing linestring edge, then an error is thrown and the edge is not added. The geometry of aline must have the same srid as defined for the topology otherwise an invalid spatial reference sys error will be thrown. Availability: 2.0.0 requires GEOS >= 3.3.0. Examples SELECT topology.AddEdge('ma_topo', ST_GeomFromText('LINESTRING(227575.8 893917.2,227591.9 893900.4)', 26986) ) As edgeid; -- result- edgeid -------- 1 SELECT topology.AddEdge('ma_topo', ST_GeomFromText('LINESTRING(227591.9 893900.4,227622.6 893844.2,227641.6 893816.5, 227704.5 893778.5)', 26986) ) As edgeid; -- result -- edgeid -------- 2 SELECT topology.AddEdge('ma_topo', ST_GeomFromText('LINESTRING(227591.2 893900, 227591.9 893900.4, 227704.5 893778.5)', 26986) ) As edgeid; -- gives error -- ERROR: Edge intersects (not on endpoints) with existing edge 1 See Also , AddFace Registers a face primitive to a topology and get it's identifier. integer AddFace varchar toponame geometry apolygon boolean force_new=false Description Registers a face primitive to a topology and get it's identifier. For a newly added face, the edges forming its boundaries and the ones contained in the face will be updated to have correct values in the left_face and right_face fields. Isolated nodes contained in the face will also be updated to have a correct containing_face field value. This function does not use nor set the next_left_edge and next_right_edge fields of the edge table. The target topology is assumed to be valid (containing no self-intersecting edges). An exception is raised if: The polygon boundary is not fully defined by existing edges or the polygon overlaps an existing face. If the apolygon geometry already exists as a face, then: if force_new is false (the default) the face id of the existing face is returned; if force_new is true a new id will be assigned to the newly registered face. When a new registration of an existing face is performed (force_new=true), no action will be taken to resolve dangling references to the existing face in the edge, node an relation tables, nor will the MBR field of the existing face record be updated. It is up to the caller to deal with that. The apolygon geometry must have the same srid as defined for the topology otherwise an invalid spatial reference sys error will be thrown. Availability: 2.0.0 Examples -- first add the edges we use generate_series as an iterator (the below -- will only work for polygons with < 10000 points because of our max in gs) SELECT topology.AddEdge('ma_topo', ST_MakeLine(ST_PointN(geom,i), ST_PointN(geom, i + 1) )) As edgeid FROM (SELECT ST_NPoints(geom) AS npt, geom FROM (SELECT ST_Boundary(ST_GeomFromText('POLYGON((234896.5 899456.7,234914 899436.4,234946.6 899356.9,234872.5 899328.7, 234891 899285.4,234992.5 899145, 234890.6 899069,234755.2 899255.4, 234612.7 899379.4,234776.9 899563.7,234896.5 899456.7))', 26986) ) As geom ) As geoms) As facen CROSS JOIN generate_series(1,10000) As i WHERE i < npt; -- result -- edgeid -------- 3 4 5 6 7 8 9 10 11 12 (10 rows) -- then add the face - SELECT topology.AddFace('ma_topo', ST_GeomFromText('POLYGON((234896.5 899456.7,234914 899436.4,234946.6 899356.9,234872.5 899328.7, 234891 899285.4,234992.5 899145, 234890.6 899069,234755.2 899255.4, 234612.7 899379.4,234776.9 899563.7,234896.5 899456.7))', 26986) ) As faceid; -- result -- faceid -------- 1 See Also , , This section covers the topology functions for creating new topogeometries. TopoGeometry Constructors CreateTopoGeom Creates a new topo geometry object from topo element array - tg_type: 1:[multi]point, 2:[multi]line, 3:[multi]poly, 4:collection topogeometry CreateTopoGeom varchar toponame integer tg_type integer layer_id topoelementarray tg_objs topogeometry CreateTopoGeom varchar toponame integer tg_type integer layer_id Description Creates a topogeometry object for layer denoted by layer_id and registers it in the relations table in the toponame schema. tg_type is an integer: 1:[multi]point (punctal), 2:[multi]line (lineal), 3:[multi]poly (areal), 4:collection. layer_id is the layer id in the topology.layer table. punctal layers are formed from set of nodes, lineal layers are formed from a set of edges, areal layers are formed from a set of faces, and collections can be formed from a mixture of nodes, edges, and faces. Omitting the array of components generates an empty TopoGeometry object. Availability: 1.? Examples: Form from existing edges Create a topogeom in ri_topo schema for layer 2 (our ri_roads), of type (2) LINE, for the first edge (we loaded in ST_CreateTopoGeo. INSERT INTO ri.ri_roads(road_name, topo) VALUES('Unknown', topology.CreateTopoGeom('ri_topo',2,2,'{{1,2}}'::topology.topoelementarray); Examples: Convert an areal geometry to best guess topogeometry Lets say we have geometries that should be formed from a collection of faces. We have for example blockgroups table and want to know the topo geometry of each block group. If our data was perfectly aligned, we could do this: -- create our topo geometry column -- SELECT topology.AddTopoGeometryColumn( 'topo_boston', 'boston', 'blockgroups', 'topo', 'POLYGON'); -- addtopgeometrycolumn -- 1 -- update our column assuming -- everything is perfectly aligned with our edges UPDATE boston.blockgroups AS bg SET topo = topology.CreateTopoGeom('topo_boston' ,3,1 , foo.bfaces) FROM (SELECT b.gid, topology.TopoElementArray_Agg(ARRAY[f.face_id,3]) As bfaces FROM boston.blockgroups As b INNER JOIN topo_boston.face As f ON b.geom && f.mbr WHERE ST_Covers(b.geom, topology.ST_GetFaceGeometry('topo_boston', f.face_id)) GROUP BY b.gid) As foo WHERE foo.gid = bg.gid; --the world is rarely perfect allow for some error --count the face if 50% of it falls -- within what we think is our blockgroup boundary UPDATE boston.blockgroups AS bg SET topo = topology.CreateTopoGeom('topo_boston' ,3,1 , foo.bfaces) FROM (SELECT b.gid, topology.TopoElementArray_Agg(ARRAY[f.face_id,3]) As bfaces FROM boston.blockgroups As b INNER JOIN topo_boston.face As f ON b.geom && f.mbr WHERE ST_Covers(b.geom, topology.ST_GetFaceGeometry('topo_boston', f.face_id)) OR ( ST_Intersects(b.geom, topology.ST_GetFaceGeometry('topo_boston', f.face_id)) AND ST_Area(ST_Intersection(b.geom, topology.ST_GetFaceGeometry('topo_boston', f.face_id) ) ) > ST_Area(topology.ST_GetFaceGeometry('topo_boston', f.face_id))*0.5 ) GROUP BY b.gid) As foo WHERE foo.gid = bg.gid; -- and if we wanted to convert our topogeometry back -- to a denomalized geometry aligned with our faces and edges -- cast the topo to a geometry -- The really cool thing is my new geometries -- are now aligned with my tiger street centerlines UPDATE boston.blockgroups SET new_geom = topo::geometry; See Also , , , , toTopoGeom Creates a new topo geometry from a simple geometry topogeometry toTopoGeom geometry geom varchar toponame integer layer_id float8 tolerance Description Creates a topogeometry object for layer denoted by layer_id and registers it in the relations table in the toponame schema. Topological primitives required to represent the input geometry will be added, possibly splitting existing ones. Pre-existing TopoGeometry objects will retain their shapes. When tolerance is given it will be used to snap the input geometry to existing primitives. Availability: 2.0 Examples This is a full self-contained workflow -- do this if you don't have a topology setup already -- creates topology not allowing any tolerance SELECT topology.CreateTopology('topo_boston_test', 2249); -- create a new table CREATE TABLE nei_topo(gid serial primary key, nei varchar(30)); --add a topogeometry column to it SELECT topology.AddTopoGeometryColumn('topo_boston_test', 'public', 'nei_topo', 'topo', 'MULTIPOLYGON') As new_layer_id; new_layer_id ----------- 1 --use new layer id in populating the new topogeometry column -- we add the topogeoms to the new layer with 0 tolerance INSERT INTO nei_topo(nei, topo) SELECT nei, topology.toTopoGeom(geom, 'topo_boston_test', 1) FROM neighborhoods WHERE gid BETWEEN 1 and 15; --use to verify what has happened -- SELECT * FROM topology.TopologySummary('topo_boston_test'); -- summary-- Topology topo_boston_test (5), SRID 2249, precision 0 61 nodes, 87 edges, 35 faces, 15 topogeoms in 1 layers Layer 1, type Polygonal (3), 15 topogeoms Deploy: public.nei_topo.topo See Also ,, , TopoElementArray_Agg Returns a topoelementarray for a set of element_id, type arrays (topoelements) topoelementarray TopoElementArray_Agg topoelement set tefield Description Used to create a from a set of . Availability: 2.0.0 Examples SELECT topology.TopoElementArray_Agg(ARRAY[e,t]) As tea FROM generate_series(1,3) As e CROSS JOIN generate_series(1,4) As t; tea -------------------------------------------------------------------------- {{1,1},{1,2},{1,3},{1,4},{2,1},{2,2},{2,3},{2,4},{3,1},{3,2},{3,3},{3,4}} See Also , TopoGeometry Accessors GetTopoGeomElementArray Returns a topoelementarray (an array of topoelements) containing the topological elements and type of the given TopoGeometry (primitive elements) topoelementarray GetTopoGeomElementArray varchar toponame integer layer_id integer tg_id topoelementarray topoelement GetTopoGeomElementArray topogeometry tg Description Returns a containing the topological elements and type of the given TopoGeometry (primitive elements). This is similar to GetTopoGeomElements except it returns the elements as an array rather than as a dataset. tg_id is the topogeometry id of the topogeometry object in the topology in the layer denoted by layer_id in the topology.layer table. Availability: 1.? Examples See Also , GetTopoGeomElements Returns a set of topoelement objects containing the topological element_id,element_type of the given TopoGeometry (primitive elements) setof topoelement GetTopoGeomElements varchar toponame integer layer_id integer tg_id setof topoelement GetTopoGeomElements topogeometry tg Description Returns a set of element_id,element_type (topoelements) for a given topogeometry object in toponame schema. tg_id is the topogeometry id of the topogeometry object in the topology in the layer denoted by layer_id in the topology.layer table. Availability: 1.? Examples See Also , TopoGeometry Outputs AsGML Returns the GML representation of a topogeometry. text AsGML topogeometry tg text AsGML topogeometry tg text nsprefix_in text AsGML topogeometry tg regclass visitedTable text AsGML topogeometry tg regclass visitedTable text nsprefix text AsGML topogeometry tg text nsprefix_in integer precision integer options text AsGML topogeometry tg text nsprefix_in integer precision integer options regclass visitedTable text AsGML topogeometry tg text nsprefix_in integer precision integer options regclass visitedTable text idprefix text AsGML topogeometry tg text nsprefix_in integer precision integer options regclass visitedTable text idprefix int gmlversion Description Returns the GML representation of a topogeometry in version GML3 format. If no nsprefix_in is specified then gml is used. Pass in an empty string for nsprefix to get a non-qualified name space. The precision (default: 15) and options (default 1) parameters, if given, are passed untouched to the underlying call to ST_AsGML. The visitedTable parameter, if given, is used for keeping track of the visited Node and Edge elements so to use cross-references (xlink:xref) rather than duplicating definitions. The table is expected to have (at least) two integer fields: 'element_type' and 'element_id'. The calling user must have both read and write privileges on the given table. For best performance, an index should be defined on element_type and element_id, in that order. Such index would be created automatically by adding a unique constraint to the fields. Example: CREATE TABLE visited ( element_type integer, element_id integer, unique(element_type, element_id) ); The idprefix parameter, if given, will be prepended to Edge and Node tag identifiers. The gmlver parameter, if given, will be passed to the underlying ST_AsGML. Defaults to 3. Availability: 2.0.0 Examples This uses the topo geometry we created in SELECT topology.AsGML(topo) As rdgml FROM ri.roads WHERE road_name = 'Unknown'; -- rdgml-- 384744 236928 384750 236923 384769 236911 384799 236895 384811 236890 384833 236884 384844 236882 384866 236881 384879 236883 384954 236898 385087 236932 385117 236938 385167 236938 385203 236941 385224 236946 385233 236950 385241 236956 385254 236971 385260 236979 385268 236999 385273 237018 385273 237037 385271 237047 385267 237057 385225 237125 385210 237144 385192 237161 385167 237192 385162 237202 385159 237214 385159 237227 385162 237241 385166 237256 385196 237324 385209 237345 385234 237375 385237 237383 385238 237399 385236 237407 385227 237419 385213 237430 385193 237439 385174 237451 385170 237455 385169 237460 385171 237475 385181 237503 385190 237521 385200 237533 385206 237538 385213 237541 385221 237542 385235 237540 385242 237541 385249 237544 385260 237555 385270 237570 385289 237584 385292 237589 385291 237596 385284 237630 ]]> Same exercise as previous without namespace SELECT topology.AsGML(topo,'') As rdgml FROM ri.roads WHERE road_name = 'Unknown'; -- rdgml-- 384744 236928 384750 236923 384769 236911 384799 236895 384811 236890 384833 236884 384844 236882 384866 236881 384879 236883 384954 236898 385087 236932 385117 236938 385167 236938 385203 236941 385224 236946 385233 236950 385241 236956 385254 236971 385260 236979 385268 236999 385273 237018 385273 237037 385271 237047 385267 237057 385225 237125 385210 237144 385192 237161 385167 237192 385162 237202 385159 237214 385159 237227 385162 237241 385166 237256 385196 237324 385209 237345 385234 237375 385237 237383 385238 237399 385236 237407 385227 237419 385213 237430 385193 237439 385174 237451 385170 237455 385169 237460 385171 237475 385181 237503 385190 237521 385200 237533 385206 237538 385213 237541 385221 237542 385235 237540 385242 237541 385249 237544 385260 237555 385270 237570 385289 237584 385292 237589 385291 237596 385284 237630 ]]> See Also ,