Topology Topology_ 8:TopoGeometry Constructor

Posted by dsantamassino on Tue, 14 Dec 2021 18:29:31 +0100

8. TopoGeometry constructor

8.1. CreateTopoGeom

CreateTopoGeom - from the array of topological elements tg_type: 1:[multi]point, 2:[multi]line,3:[multi]poly, 4:collection creates a new topological geometry object

8.1. 1. Summary

topogeometry CreateTopoGeom(varchar toponame, integer tg_type, integer layer_id, topoelementarray tg_objs);
topogeometry CreateTopoGeom(varchar toponame, integer tg_type, integer layer_id);

8.1. 2. Description

For layer_ The layer represented by ID creates a topological geometry object and registers it in the toponame schema's relational table.
Tg_ Typee is an integer: 1: [many] points (dots), 2: [many] lines (straight lines), 3: [many] many (areas), 4: sets. Layer_id is the layer id in the topology. Layer table.

A point layer consists of a set of nodes, a linear layer consists of a set of edges, and a polygon layer consists of a set of faces. A set can be composed of a mixture of nodes, edges, and faces.
Omitting the component array will result in an empty TopoGeometry object.

8.1. 3. Samples

  • From existing edges

At ri_topo mode creates a topology graph for Layer 2 (our ri_roads) of type (2)LINE for the first edge (we load it in ST_CreateTopoGeo)

INSERT INTO ri.ri_roads(road_name, topo) VALUES('Unknown', topology.CreateTopoGeom('ri_topo',2,2,'{{1,2}}'::topology.topoelementarray);
  • Converting an area geometry to a best guessed topological geometry

Suppose we have a geometry that should consist of a series of polygons. For example, we have a blockgroups table and want to know the topological geometry of each block group. If our data are identical, we can do this:

-- Creating Topological Geometry Columns
SELECT topology.AddTopoGeometryColumn('topo_boston','boston', 'blockgroups', 'topo', 'POLYGON');
addtopgeometrycolumn
1
-- Update our column assumptions  
-- Everything aligns perfectly 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 bINNER 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 seldom perfect, allowing some errors  
-- Within what we think of as block group boundaries  

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;

-- If we want to convert our topology back  
-- To a named geometry aligned with our faces and edges  
-- Converting a Topology Graph to a Geometry Graphic  
-- What's really cool is my new geometry  
UPDATE boston.blockgroups SET new_geom = topo::geometry;

8.2. toTopoGeom

toTopoGeom - Converts simple geometries to topological geometries

8.2. 1. Summary

topogeometry toTopoGeom(geometry geom, varchar toponame, integer layer_id, float8 tolerance);
topogeometry toTopoGeom(geometry geom, topogeometry topogeom, float8 tolerance);

8.2. 2. Description

Converts a simple geometry to a topological geometry.
The topological primitives required to represent the input geometry will be added to the underlying topology and may be split
They will be associated with the output TopoGeometry in the relationship table.
Existing TopoGeometry objects (except if topogeom is given) retain their shape.
When a tolerance is given, it will be used to capture the input geometry to an existing primitive.
In the first form, a new TopoGeometry is created for a given layer_id of a given topology.
In the second form, the converted primitive is added to the existing TopoGeometry.
It may add space to its final shape. To completely replace the old shape with the new one, see clearTopoGeom.

8.2. 3. Samples

  • This is a complete self-contained workflow
-- If you don't already have a topology, you can do so  
-- Create a topology that does not allow any tolerances  
SELECT topology.CreateTopology('topo_boston_test', 2249);

--Create a new table
CREATE TABLE nei_topo(gid serial primary key, nei varchar(30));

--Add a topological geometry column to it
SELECT topology.AddTopoGeometryColumn('topo_boston_test', 'public', 'nei_topo', 'topo', 'MULTIPOLYGON') As new_layer_id;

--new_layer_id
-----------
--1

-- Use a new layer when populating a new topological geometry column id  
-- We add the topology graph to the new layer with a tolerance of 0  
INSERT INTO nei_topo(nei, topo) 
SELECT nei, topology.toTopoGeom(geom, 'topo_boston_test', 1) FROM neighborhoods WHERE gid BETWEEN 1 and 15;

-- Used for validation
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
-- Reduce the polygons of all topological geometries by 10 meters
UPDATE nei_topo SET topo = ST_Buffer(clearTopoGeom(topo), -10);

-- Get the zero zone left by the above operation  
-- I think GRASS Call this polygon0 layer  

SELECT ST_GetFaceGeometry('topo_boston_test', f.face_id)
    FROM topo_boston_test.face f
    WHERE f.face_id > 0
    AND NOT EXISTS ( 
        -- Check not TopoGeometry Reference to this side  
        SELECT * FROM topo_boston_test.relation
        WHERE layer_id = 1 AND element_id = f.face_id);

8.3. TopoElementArray_Agg

TopoElementArray_Agg - Returns a set of elements_ topoelementarray of id, array of types (topoelements)

8.3. 1. Summary

topoelementarray TopoElementArray_Agg(topoelement set tefield);

8.3. 2. Samples

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}}

Topics: PostgreSQL