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

Function: asgml

 

 

Schema

topology

 

Owner

postgres

 

Descriptions

args: tg, nsprefix_in, precision, options, visitedTable, idprefix, gmlversion - Returns the GML representation of a topogeometry.

 

Options

Option

Value

Returns

text

Language

plpgsql

Parameters

tg topology.topogeometry

nsprefix_in text

precision_in integer

options_in integer

visitedtable pg_catalog.regclass

idprefix text

gmlver integer

 

Definition

CREATE OR REPLACE FUNCTION topology.asgml (
 tg topology.topogeometry,
 nsprefix_in text,
 precision_in integer,
 options_in integer,
 visitedtable pg_catalog.regclass,
 idprefix text,
 gmlver integer
)
RETURNS text AS
$span$
DECLARE

 nsprefix text;
 precision int;
 options int;
 visited bool;
 toponame text;
 gml text;
 sql text;
 rec RECORD;
 rec2 RECORD;
BEGIN

 nsprefix := 'gml:';
 IF nsprefix_in IS NOT NULL THEN
   IF
nsprefix_in = '' THEN
     nsprefix = nsprefix_in;
   ELSE
     nsprefix = nsprefix_in || ':';
   END IF;
 END IF;

 precision := 15;
 IF precision_in IS NOT NULL THEN
   precision = precision_in;
 END IF;

 options := 1;
 IF options_in IS NOT NULL THEN
   options
= options_in;
 END IF;

 -- Get topology name (for subsequent queries)
 SELECT name FROM topology.topology into toponame
             WHERE id = tg.topology_id;

 -- Puntual TopoGeometry
 IF tg.type = 1 THEN
   gml = '<' || nsprefix || 'TopoPoint>';
   -- For each defining node, print a directedNode
   FOR rec IN  EXECUTE 'SELECT r.element_id, n.geom from '
     || quote_ident(toponame) || '.relation r LEFT JOIN '
     || quote_ident(toponame) || '.node n ON (r.element_id = n.node_id)'
     || ' WHERE r.layer_id = ' || tg.layer_id
     || ' AND r.topogeo_id = ' || tg.id
   LOOP
     gml = gml || '<' || nsprefix || 'directedNode';
     -- Do visited bookkeeping if visitedTable was given
     IF visitedTable IS NOT NULL THEN
       EXECUTE
'SELECT true FROM '
               || visitedTable::text
               || ' WHERE element_type = 1 AND element_id = '
               || rec.element_id LIMIT 1 INTO visited;
       IF visited IS NOT NULL THEN
         gml = gml || ' xlink:href="#' || idprefix || 'N' || rec.element_id || '" />';
         CONTINUE;
       ELSE
         -- Mark as visited
         EXECUTE 'INSERT INTO ' || visitedTable::text
           || '(element_type, element_id) VALUES (1, '
           || rec.element_id || ')';
       END IF;
     END IF;
     gml = gml || '>';
     gml = gml || topology._AsGMLNode(rec.element_id, rec.geom, nsprefix_in, precision, options, idprefix, gmlver);
     gml = gml || '</' || nsprefix || 'directedNode>';
   END LOOP;
   gml = gml || '</' || nsprefix || 'TopoPoint>';
   RETURN gml;

 ELSIF tg.type = 2 THEN -- lineal
   gml = '<' || nsprefix || 'TopoCurve>';

   FOR rec IN SELECT (ST_Dump(topology.Geometry(tg))).geom
   LOOP
     FOR
rec2 IN EXECUTE
       'SELECT e.*, ST_LineLocatePoint($1'
       || ', ST_LineInterpolatePoint(e.geom, 0.2)) as pos'
       || ', ST_LineLocatePoint($1'
       || ', ST_LineInterpolatePoint(e.geom, 0.8)) as pos2 FROM '
       || quote_ident(toponame)
       || '.edge e WHERE ST_Covers($1'
       || ', e.geom) ORDER BY pos'
       -- TODO: add relation to the conditional, to reduce load ?
       USING rec.geom
     LOOP

       gml = gml || '<' || nsprefix || 'directedEdge';

       -- if this edge goes in opposite direction to the
       --       line, make it with negative orientation

       IF rec2.pos2 < rec2.pos THEN -- edge goes in opposite direction
         gml = gml || ' orientation="-"';
       END IF;

       -- Do visited bookkeeping if visitedTable was given
       IF visitedTable IS NOT NULL THEN

         EXECUTE
'SELECT true FROM '
           || visitedTable::text
           || ' WHERE element_type = 2 AND element_id = '
           || rec2.edge_id LIMIT 1 INTO visited;
         IF visited THEN
           -- Use xlink:href if visited
           gml = gml || ' xlink:href="#' || idprefix || 'E' || rec2.edge_id || '" />';
           CONTINUE;
         ELSE
           -- Mark as visited otherwise
           EXECUTE 'INSERT INTO ' || visitedTable::text
             || '(element_type, element_id) VALUES (2, '
             || rec2.edge_id || ')';
         END IF;

       END IF;


       gml = gml || '>';

       gml = gml || topology._AsGMLEdge(rec2.edge_id,
                                       rec2.start_node,
                                       rec2.end_node, rec2.geom,
                                       visitedTable,
                                       nsprefix_in, precision,
                                       options, idprefix, gmlver);


       gml = gml || '</' || nsprefix || 'directedEdge>';
     END LOOP;
   END LOOP;

   gml = gml || '</' || nsprefix || 'TopoCurve>';
   return gml;

 ELSIF tg.type = 3 THEN -- areal
   gml = '<' || nsprefix || 'TopoSurface>';

   -- For each defining face, print a directedFace
   FOR rec IN  EXECUTE 'SELECT f.face_id from '
     || quote_ident(toponame) || '.relation r LEFT JOIN '
     || quote_ident(toponame) || '.face f ON (r.element_id = f.face_id)'
     || ' WHERE r.layer_id = ' || tg.layer_id
     || ' AND r.topogeo_id = ' || tg.id
   LOOP
     gml = gml || '<' || nsprefix || 'directedFace';
     -- Do visited bookkeeping if visitedTable was given
     IF visitedTable IS NOT NULL THEN
       EXECUTE
'SELECT true FROM '
               || visitedTable::text
               || ' WHERE element_type = 3 AND element_id = '
               || rec.face_id LIMIT 1 INTO visited;
       IF visited IS NOT NULL THEN
         gml = gml || ' xlink:href="#' || idprefix || 'F' || rec.face_id || '" />';
         CONTINUE;
       ELSE
         -- Mark as visited
         EXECUTE 'INSERT INTO ' || visitedTable::text
           || '(element_type, element_id) VALUES (3, '
           || rec.face_id || ')';
       END IF;
     END IF;
     gml = gml || '>';
     gml = gml || topology._AsGMLFace(toponame, rec.face_id, visitedTable,
                                      nsprefix_in, precision,
                                      options, idprefix, gmlver);
     gml = gml || '</' || nsprefix || 'directedFace>';
   END LOOP;
   gml = gml || '</' || nsprefix || 'TopoSurface>';
   RETURN gml;

 ELSIF tg.type = 4 THEN -- collection
   RAISE EXCEPTION 'Collection TopoGeometries are not supported by AsGML';

 END IF;


 RETURN gml;

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

COMMENT ON FUNCTION topology.asgml(tg topology.topogeometry, nsprefix_in text, precision_in integer, options_in integer, visitedtable pg_catalog.regclass, idprefix text, gmlver integer)
IS 'args: tg, nsprefix_in, precision, options, visitedTable, idprefix, gmlversion - Returns the GML representation of a topogeometry.';

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