1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--
-- PostGIS - Spatial Types for PostgreSQL
-- http://postgis.net
--
-- Copyright (C) 2011-2012 Sandro Santilli <strk@kbt.io>
--
-- This is free software; you can redistribute and/or modify it under
-- the terms of the GNU General Public Licence. See the COPYING file.
--
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/* #define POSTGIS_TOPOLOGY_DEBUG 1 */
-- {
-- Convert a simple geometry to a topologically-defined one
--
-- See http://trac.osgeo.org/postgis/ticket/1017
--
-- }{
CREATE OR REPLACE FUNCTION topology.toTopoGeom(ageom Geometry, atopology varchar, alayer int, atolerance float8 DEFAULT 0)
RETURNS topology.TopoGeometry
AS
$$
DECLARE
layer_info RECORD;
topology_info RECORD;
tg topology.TopoGeometry;
typ TEXT;
BEGIN
-- Get topology information
BEGIN
SELECT *
FROM topology.topology
INTO STRICT topology_info WHERE name = atopology;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION 'No topology with name "%" in topology.topology',
atopology;
END;
-- Get layer information
BEGIN
SELECT *, CASE
WHEN feature_type = 1 THEN 'puntal'
WHEN feature_type = 2 THEN 'lineal'
WHEN feature_type = 3 THEN 'areal'
WHEN feature_type = 4 THEN 'mixed'
ELSE 'unexpected_'||feature_type
END as typename
FROM topology.layer l
INTO STRICT layer_info
WHERE l.layer_id = alayer
AND l.topology_id = topology_info.id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION 'No layer with id "%" in topology "%"',
alayer, atopology;
END;
-- Can't convert to a hierarchical topogeometry
IF layer_info.level > 0 THEN
RAISE EXCEPTION 'Layer "%" of topology "%" is hierarchical, cannot convert to it.',
alayer, atopology;
END IF;
--
-- Check type compatibility and create empty TopoGeometry
-- 1:puntal, 2:lineal, 3:areal, 4:collection
--
typ = geometrytype(ageom);
IF typ = 'GEOMETRYCOLLECTION' THEN
-- A collection can only go collection layer
IF layer_info.feature_type != 4 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a collection feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg := topology.CreateTopoGeom(atopology, 4, alayer);
ELSIF typ = 'POINT' OR typ = 'MULTIPOINT' THEN -- puntal
-- A point can go in puntal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 1 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a puntal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg := topology.CreateTopoGeom(atopology, 1, alayer);
ELSIF typ = 'LINESTRING' or typ = 'MULTILINESTRING' THEN -- lineal
-- A line can go in lineal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 2 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a lineal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg := topology.CreateTopoGeom(atopology, 2, alayer);
ELSIF typ = 'POLYGON' OR typ = 'MULTIPOLYGON' THEN -- areal
-- An area can go in areal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 3 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold an areal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg := topology.CreateTopoGeom(atopology, 3, alayer);
ELSE
-- Should never happen
RAISE EXCEPTION
'Unsupported feature type %', typ;
END IF;
tg := topology.toTopoGeom(ageom, tg, atolerance);
RETURN tg;
END
$$
LANGUAGE 'plpgsql' VOLATILE STRICT;
-- }
-- {
-- Convert a simple geometry to a topologically-defined one
-- adding its components to a pre-existing TopoGeometry
--
-- }{
CREATE OR REPLACE FUNCTION topology.toTopoGeom(ageom Geometry, tg topology.TopoGeometry, atolerance float8 DEFAULT 0)
RETURNS topology.TopoGeometry
AS
$$
DECLARE
layer_info RECORD;
topology_info RECORD;
rec RECORD;
rec2 RECORD;
elem TEXT;
elems TEXT[];
sql TEXT;
typ TEXT;
tolerance FLOAT8;
alayer INT;
atopology TEXT;
BEGIN
#ifdef POSTGIS_TOPOLOGY_DEBUG
RAISE DEBUG 'TopoGeometry is "%", its topology_id is "%"', tg, topology_id(tg);
#endif
-- Get topology information
SELECT id, name FROM topology.topology
INTO topology_info
WHERE id = topology_id(tg);
IF NOT FOUND THEN
RAISE EXCEPTION 'No topology with id "%" in topology.topology',
topology_id(tg);
END IF;
alayer := layer_id(tg);
atopology := topology_info.name;
-- Get tolerance, if 0 was given
tolerance := COALESCE( NULLIF(atolerance, 0), topology._st_mintolerance(topology_info.name, ageom) );
-- Get layer information
BEGIN
SELECT *, CASE
WHEN feature_type = 1 THEN 'puntal'
WHEN feature_type = 2 THEN 'lineal'
WHEN feature_type = 3 THEN 'areal'
WHEN feature_type = 4 THEN 'mixed'
ELSE 'unexpected_'||feature_type
END as typename
FROM topology.layer l
INTO STRICT layer_info
WHERE l.layer_id = layer_id(tg)
AND l.topology_id = topology_info.id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION 'No layer with id "%" in topology "%"',
alayer, atopology;
END;
-- Can't convert to a hierarchical topogeometry
IF layer_info.level > 0 THEN
RAISE EXCEPTION 'Layer "%" of topology "%" is hierarchical, cannot convert a simple geometry to it.',
alayer, atopology;
END IF;
--
-- Check type compatibility and set TopoGeometry type
-- 1:puntal, 2:lineal, 3:areal, 4:collection
--
typ = geometrytype(ageom);
IF typ = 'GEOMETRYCOLLECTION' THEN
-- A collection can only go to collection layer
IF layer_info.feature_type != 4 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a collection feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg.type := 4;
ELSIF typ = 'POINT' OR typ = 'MULTIPOINT' THEN -- puntal
-- A point can go in puntal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 1 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a puntal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg.type := 1;
ELSIF typ = 'LINESTRING' or typ = 'MULTILINESTRING' THEN -- lineal
-- A line can go in lineal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 2 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold a lineal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg.type := 2;
ELSIF typ = 'POLYGON' OR typ = 'MULTIPOLYGON' THEN -- areal
-- An area can go in areal or collection layer
IF layer_info.feature_type != 4 and layer_info.feature_type != 3 THEN
RAISE EXCEPTION
'Layer "%" of topology "%" is %, cannot hold an areal feature.',
layer_info.layer_id, topology_info.name, layer_info.typename;
END IF;
tg.type := 3;
ELSE
-- Should never happen
RAISE EXCEPTION
'Unexpected feature dimension %', ST_Dimension(ageom);
END IF;
-- Now that we have an empty topogeometry, we loop over distinct components
-- and add them to the definition of it. We add them as soon
-- as possible so that each element can further edit the
-- definition by splitting
FOR rec IN SELECT id(tg), alayer as lyr,
geom, ST_Dimension(gd.geom) as dims
FROM ST_Dump(ageom) AS gd
WHERE NOT ST_IsEmpty(gd.geom)
LOOP
-- NOTE: Switched from using case to this because of PG 10 behavior change
-- Using a UNION ALL only one will be processed because of the WHERE
-- Since the WHERE clause will be processed first
FOR rec2 IN SELECT primitive
FROM
(
SELECT topology.topogeo_addPoint(atopology, rec.geom, tolerance)
WHERE rec.dims = 0
UNION ALL
SELECT topology.topogeo_addLineString(atopology, rec.geom, tolerance)
WHERE rec.dims = 1
UNION ALL
SELECT topology.topogeo_addPolygon(atopology, rec.geom, tolerance)
WHERE rec.dims = 2
) AS f(primitive)
LOOP
elem := ARRAY[rec.dims+1, rec2.primitive]::text;
IF elems @> ARRAY[elem] THEN
#ifdef POSTGIS_TOPOLOGY_DEBUG
RAISE DEBUG 'Elem % already in %', elem, elems;
RAISE DEBUG '% @> ARRAY[%] returned true', elems, elem;
#endif
ELSE
#ifdef POSTGIS_TOPOLOGY_DEBUG
RAISE DEBUG 'Elem % NOT in %', elem, elems;
#endif
elems := elems || elem;
-- TODO: consider use a single INSERT statement for the whole thing
sql := 'INSERT INTO ' || quote_ident(atopology)
|| '.relation(topogeo_id, layer_id, element_type, element_id) VALUES ('
|| rec.id || ',' || rec.lyr || ',' || rec.dims+1
|| ',' || rec2.primitive || ')'
-- NOTE: we're avoiding duplicated rows here
|| ' EXCEPT SELECT ' || rec.id || ', ' || rec.lyr
|| ', element_type, element_id FROM '
|| quote_ident(topology_info.name)
|| '.relation WHERE layer_id = ' || rec.lyr
|| ' AND topogeo_id = ' || rec.id;
#ifdef POSTGIS_TOPOLOGY_DEBUG
RAISE DEBUG '%', sql;
#endif
EXECUTE sql;
END IF;
END LOOP;
END LOOP;
RETURN tg;
END
$$
LANGUAGE 'plpgsql' VOLATILE STRICT;
-- }
|