pggeodb.nancy.inra.fr/db_robot - db_robot on pggeodb.nancy.inra.fr
Previous topic Chapter index Next topic

Function: totopogeom

 

 

Schema

topology

 

Owner

postgres

 

Descriptions

args: geom, toponame, layer_id, tolerance - Converts a simple Geometry into a topo geometry

 

Options

Option

Value

Returns

topology.topogeometry

Language

plpgsql

Parameters

ageom public.geometry

atopology varchar

alayer integer

atolerance double precision = 0

 

Definition

CREATE OR REPLACE FUNCTION topology.totopogeom (
 ageom public.geometry,
 atopology varchar,
 alayer integer,
 atolerance double precision = 0
)
RETURNS topology.topogeometry AS
$span$
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
$span$
LANGUAGE
'plpgsql'
VOLATILE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST
100;

COMMENT ON FUNCTION topology.totopogeom(ageom public.geometry, atopology varchar, alayer integer, atolerance double precision)
IS 'args: geom, toponame, layer_id, tolerance - Converts a simple Geometry into a topo geometry';

This file was generated with SQL Manager for PostgreSQL (www.pgsqlmanager.com) at 26/02/2014 11:51
Previous topic Chapter index Next topic