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

Function: createtopogeom

 

 

Schema

topology

 

Owner

postgres

 

Descriptions

args: toponame, tg_type, layer_id, tg_objs - Creates a new topo geometry object from topo element array - tg_type: 1:[multi]point, 2:[multi]line, 3:[multi]poly, 4:collection

 

Options

Option

Value

Returns

topology.topogeometry

Language

plpgsql

Parameters

toponame varchar

tg_type integer

layer_id integer

in tg_objs topology.topoelementarray

 

Definition

CREATE OR REPLACE FUNCTION topology.createtopogeom (
 toponame varchar,
 tg_type integer,
 layer_id integer,
 tg_objs topology.topoelementarray
)
RETURNS topology.topogeometry AS
$span$
DECLARE

 i integer;
 dims varchar;
 outerdims varchar;
 innerdims varchar;
 obj_type integer;
 obj_id integer;
 ret topology.TopoGeometry;
 rec RECORD;
 layertype integer;
 layerlevel integer;
 layerchild integer;
BEGIN

 IF
tg_type < 1 OR tg_type > 4 THEN
   RAISE EXCEPTION
'Invalid TopoGeometry type % (must be in the range 1..4)', tg_type;
 END IF;

 -- Get topology id into return TopoGeometry
 SELECT id INTO ret.topology_id
   FROM topology.topology WHERE name = toponame;

 IF NOT FOUND THEN
   RAISE EXCEPTION
'Topology % does not exist', quote_literal(toponame);
 END IF;

 --
 -- Get layer info
 --

 layertype := NULL;
 FOR rec IN EXECUTE 'SELECT * FROM topology.layer'
      ' WHERE topology_id = '
|| ret.topology_id
   || ' AND layer_id = ' || layer_id
 LOOP
   layertype = rec.feature_type;
   layerlevel = rec.level;
   layerchild = rec.child_id;
 END LOOP;

 -- Check for existence of given layer id
 IF layertype IS NULL THEN
   RAISE EXCEPTION
'No layer with id % is registered with topology %', layer_id, toponame;
 END IF;

 -- Verify compatibility between layer geometry type and
 -- TopoGeom requested geometry type

 IF layertype != 4 and layertype != tg_type THEN
   RAISE EXCEPTION
'A Layer of type % cannot contain a TopoGeometry of type %', layertype, tg_type;
 END IF;

 -- Set layer id and type in return object
 ret.layer_id = layer_id;
 ret.type = tg_type;

 --
 -- Get new TopoGeo id from sequence
 --

 FOR rec IN EXECUTE 'SELECT nextval(' ||
   quote_literal(
     quote_ident(toponame) || '.topogeo_s_' || layer_id
   ) || ')'
 LOOP
   ret.id = rec.nextval;
 END LOOP;

 -- Loop over outer dimension
 i = array_lower(tg_objs, 1);
 LOOP
   obj_id = tg_objs[i][1];
   obj_type = tg_objs[i][2];

   -- Elements of type 0 represent emptiness, just skip them
   IF obj_type = 0 THEN
     IF
obj_id != 0 THEN
       RAISE EXCEPTION
'Malformed empty topo element {0,%} -- id must be 0 as well', obj_id;
     END IF;
   ELSE
     IF
layerlevel = 0 THEN -- array specifies lower-level objects
       IF tg_type != 4 and tg_type != obj_type THEN
         RAISE EXCEPTION
'A TopoGeometry of type % cannot contain topology elements of type %', tg_type, obj_type;
       END IF;
     ELSE -- array specifies lower-level topogeometries
       IF obj_type != layerchild THEN
         RAISE EXCEPTION
'TopoGeom element layer do not match TopoGeom child layer';
       END IF;
       -- TODO: verify that the referred TopoGeometry really
       -- exists in the relation table ?

     END IF;

     --RAISE NOTICE 'obj:% type:% id:%', i, obj_type, obj_id;

     --
     -- Insert record into the Relation table
     --

     EXECUTE 'INSERT INTO '||quote_ident(toponame)
       || '.relation(topogeo_id, layer_id, '
          'element_id,element_type) '
          ' VALUES ('
||ret.id
       ||','||ret.layer_id
       || ',' || obj_id || ',' || obj_type || ');';
   END IF;

   i = i+1;
   IF i > array_upper(tg_objs, 1) THEN
     EXIT
;
   END IF;
 END LOOP;

 RETURN ret;

END
$span$
LANGUAGE
'plpgsql'
VOLATILE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST
100;

COMMENT ON FUNCTION topology.createtopogeom(toponame varchar, tg_type integer, layer_id integer, tg_objs topology.topoelementarray)
IS 'args: toponame, tg_type, layer_id, tg_objs - Creates a new topo geometry object from topo element array - tg_type: 1:[multi]point, 2:[multi]line, 3:[multi]poly, 4:collection';

This file was generated with SQL Manager for PostgreSQL (www.pgsqlmanager.com) at 13/03/2014 13:23
Previous topic Chapter index Next topic