-e '/^\(CREATE\|ALTER\) \(CAST\|TYPE\|TABLE\|SCHEMA\|DOMAIN\|TRIGGER\)/,/\;/d' \
$< > $@
-topology.sql.in: sql/sqlmm.sql sql/populate.sql sql/polygonize.sql sql/gml.sql sql/query/getnodebypoint.sql sql/query/getedgebypoint.sql sql/query/getfacebypoint.sql sql/query/GetRingEdges.sql sql/manage/TopologySummary.sql sql/manage/CopyTopology.sql sql/manage/ManageHelper.sql sql/topoelement/topoelement_agg.sql sql/topogeometry/type.sql
+topology.sql.in: sql/sqlmm.sql sql/populate.sql sql/polygonize.sql sql/gml.sql sql/query/getnodebypoint.sql sql/query/getedgebypoint.sql sql/query/getfacebypoint.sql sql/query/GetRingEdges.sql sql/manage/TopologySummary.sql sql/manage/CopyTopology.sql sql/manage/ManageHelper.sql sql/topoelement/topoelement_agg.sql sql/topogeometry/type.sql sql/topogeometry/totopogeom.sql
uninstall_topology.sql: topology.sql ../utils/create_undef.pl
$(PERL) ../utils/create_undef.pl $< $(POSTGIS_PGSQL_VERSION) > $@
--- /dev/null
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+--
+-- PostGIS - Spatial Types for PostgreSQL
+-- http://postgis.refractions.net
+--
+-- Copyright (C) 2012 Sandro Santilli <strk@keybit.net>
+--
+-- This is free software; you can redistribute and/or modify it under
+-- the terms of the GNU General Public Licence. See the COPYING file.
+--
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+-- {
+-- Convert a simple geometry to a topologically-defined one
+--
+-- See http://trac.osgeo.org/postgis/ticket/1017
+--
+-- }{
+CREATE OR REPLACE FUNCTION topology.toTopoGeom(geom Geometry, toponame varchar, layer_id int, tolerance float8 DEFAULT 0)
+ RETURNS topology.TopoGeometry
+AS
+$$
+DECLARE
+ layer_info RECORD;
+ topology_info RECORD;
+BEGIN
+
+ -- Get topology information
+ BEGIN
+ SELECT * FROM topology.topology
+ INTO STRICT topology_info WHERE name = toponame;
+ EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RAISE EXCEPTION 'No topology with name "%" in topology.topology',
+ toponame;
+ END;
+
+ -- Get layer information
+ BEGIN
+ SELECT * FROM topology.layer l
+ INTO STRICT layer_info
+ WHERE l.layer_id = layer_id
+ AND l.topology_id = topology_info.id;
+ EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RAISE EXCEPTION 'No layer with id "%" in topology "%"',
+ layer_id, toponame;
+ 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',
+ layer_id, toponame;
+ END IF;
+
+ --
+ -- TODO: Check type compatibility
+ -- A point can go in puntal or collection layer
+ -- A line can go in lineal or collection layer
+ -- An area can go in areal or collection layer
+ -- A collection can only go collection layer
+ -- What to do with EMPTies ?
+ --
+
+ RAISE EXCEPTION 'toTopoGeometry not implemented yet';
+
+END
+$$
+LANGUAGE 'plpgsql' STABLE STRICT;
+-- }
regress/topoelementarray_agg.sql \
regress/topogeometry_type.sql \
regress/topo2.5d.sql \
+ regress/totopogeom.sql \
regress/droptopology.sql \
regress/copytopology.sql \
regress/createtopogeom.sql \
--- /dev/null
+\set VERBOSITY terse
+set client_min_messages to ERROR;
+
+select 'create', createtopology('tt') > 0;
+
+-- Invalid calls
+select totopogeom('POINT(0 0)'::geometry, 'unexistent', 1);
+select totopogeom('POINT(0 0)'::geometry, 'tt', 1);
+select totopogeom(null, 'tt', 1);
+select totopogeom('POINT(0 0)'::geometry, '', 1);
+select totopogeom('POINT(0 0)'::geometry, null, 1);
+select totopogeom('POINT(0 0)'::geometry, 'tt', null);
+
+-- Create simple puntual layer ( will be layer 2 )
+CREATE TABLE tt.f_puntal(id serial);
+SELECT 'simple_puntual_layer', AddTopoGeometryColumn('tt', 'tt', 'f_puntal','g','POINT');
+
+-- Create a hierarchical layer (will be layer 2)
+CREATE TABLE tt.f_hier(id serial);
+SELECT 'hierarchical_layer', AddTopoGeometryColumn('tt', 'tt', 'f_hier','g','COLLECTION', 1);
+
+-- A couple more invalid calls
+select totopogeom('POINT(0 0)'::geometry, 'tt', 3); -- non existent layer
+select totopogeom('POINT(0 0)'::geometry, 'tt', 2); -- invalid (hierarchical) layer
+-- TODO: add more invalid calls due to type mismatch
+
+
+DROP TABLE tt.f_hier;
+DROP TABLE tt.f_puntal;
+select droptopology('tt');
--- /dev/null
+create|t
+ERROR: No topology with name "unexistent" in topology.topology
+ERROR: No layer with id "1" in topology "tt"
+ERROR: No topology with name "" in topology.topology
+simple_puntual_layer|1
+hierarchical_layer|2
+ERROR: No layer with id "3" in topology "tt"
+ERROR: Layer "2" of topology "tt" is hierarchical, cannot convert to it
+Topology 'tt' dropped
-- TopoGeometry
#include "sql/topogeometry/type.sql"
+#include "sql/topogeometry/totopogeom.sql"
-- GML
#include "sql/gml.sql"