1-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2--
3-- PostGIS - Spatial Types for PostgreSQL
4-- http://postgis.net
5--
6-- Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
7--
8-- This is free software; you can redistribute and/or modify it under
9-- the terms of the GNU General Public Licence. See the COPYING file.
10--
11-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12
13--{
14-- CopyTopology(name_source, name_target)
15--
16-- Makes a copy of a topology (primitives + topogeometry collections) .
17-- Returns the new topology id.
18--
19CREATE OR REPLACE FUNCTION topology.CopyTopology(atopology varchar, newtopo varchar)
20RETURNS int
21AS
22$$
23DECLARE
24  rec RECORD;
25  rec2 RECORD;
26  oldtopo_id integer;
27  newtopo_id integer;
28  n int4;
29  ret text;
30BEGIN
31
32  SELECT * FROM topology.topology where name = atopology
33  INTO strict rec;
34  oldtopo_id = rec.id;
35  -- TODO: more gracefully handle unexistent topology
36
37  SELECT topology.CreateTopology(newtopo, rec.SRID, rec.precision, rec.hasZ)
38  INTO strict newtopo_id;
39
40  -- Copy faces
41  EXECUTE 'INSERT INTO ' || quote_ident(newtopo)
42    || '.face SELECT * FROM ' || quote_ident(atopology)
43    || '.face WHERE face_id != 0';
44  -- Update faces sequence
45  EXECUTE 'SELECT setval(' || quote_literal(
46      quote_ident(newtopo) || '.face_face_id_seq'
47    ) || ', (SELECT last_value FROM '
48    || quote_ident(atopology) || '.face_face_id_seq))';
49
50  -- Copy nodes
51  EXECUTE 'INSERT INTO ' || quote_ident(newtopo)
52    || '.node SELECT * FROM ' || quote_ident(atopology)
53    || '.node';
54  -- Update node sequence
55  EXECUTE 'SELECT setval(' || quote_literal(
56      quote_ident(newtopo) || '.node_node_id_seq'
57    ) || ', (SELECT last_value FROM '
58    || quote_ident(atopology) || '.node_node_id_seq))';
59
60  -- Copy edges
61  EXECUTE 'INSERT INTO ' || quote_ident(newtopo)
62    || '.edge_data SELECT * FROM ' || quote_ident(atopology)
63    || '.edge_data';
64  -- Update edge sequence
65  EXECUTE 'SELECT setval(' || quote_literal(
66      quote_ident(newtopo) || '.edge_data_edge_id_seq'
67    ) || ', (SELECT last_value FROM '
68    || quote_ident(atopology) || '.edge_data_edge_id_seq))';
69
70  -- Copy layers and their TopoGeometry sequences
71  FOR rec IN SELECT * FROM topology.layer WHERE topology_id = oldtopo_id
72  LOOP
73    INSERT INTO topology.layer (topology_id, layer_id, feature_type,
74      level, child_id, schema_name, table_name, feature_column)
75      VALUES (newtopo_id, rec.layer_id, rec.feature_type,
76              rec.level, rec.child_id, newtopo,
77              'LAYER' ||  rec.layer_id, '');
78    -- Create layer's TopoGeometry sequences
79    EXECUTE 'SELECT last_value FROM '
80      || quote_ident(atopology) || '.topogeo_s_' || rec.layer_id
81      INTO STRICT n;
82    EXECUTE 'CREATE SEQUENCE ' || quote_ident(newtopo)
83      || '.topogeo_s_' || rec.layer_id;
84    EXECUTE 'SELECT setval(' || quote_literal(
85      quote_ident(newtopo) || '.topogeo_s_' || rec.layer_id
86      ) || ', ' || n || ')';
87  END LOOP;
88
89  -- Copy TopoGeometry definitions
90  EXECUTE 'INSERT INTO ' || quote_ident(newtopo)
91    || '.relation SELECT * FROM ' || quote_ident(atopology)
92    || '.relation';
93
94  RETURN newtopo_id;
95END
96$$
97LANGUAGE 'plpgsql' VOLATILE STRICT;
98
99--} TopologySummary
100