]> granicus.if.org Git - postgis/commitdiff
Fix AsTopoJSON example to output correctly formatted arcs (#2444)
authorSandro Santilli <strk@keybit.net>
Thu, 22 Aug 2013 15:32:26 +0000 (15:32 +0000)
committerSandro Santilli <strk@keybit.net>
Thu, 22 Aug 2013 15:32:26 +0000 (15:32 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@11852 b70326c6-7e19-0410-871a-916f4a2858ee

doc/extras_topology.xml

index d09f38cdb6b029e6c909612b56ea712f2c8d315b..1d9c838346aaf5b3abc42dd971a8f05d12a451f7 100644 (file)
@@ -3175,33 +3175,52 @@ A full TopoJSON document will be need to contain, in addition to the snippets re
 CREATE TEMP TABLE edgemap(arc_id serial, edge_id int unique);
 
 -- header
-SELECT '{ "type": "Topology", "transform": { "scale": [1,1], "translate": [0,0] }, "objects": {';
+SELECT '{ "type": "Topology", "transform": { "scale": [1,1], "translate": [0,0] }, "objects": {'
 
 -- objects
-SELECT '"' || feature_name || '": ' || AsTopoJSON(feature, 'edgemap')
+UNION ALL SELECT '"' || feature_name || '": ' || AsTopoJSON(feature, 'edgemap')
 FROM features.big_parcels WHERE feature_name = 'P3P4';
 
 -- arcs
-SELECT '}, "arcs": ['
-  UNION ALL
-SELECT (regexp_matches(ST_AsGEOJSON(ST_SnapToGrid(e.geom,1)), '\[.*\]'))[1] as t
-FROM edgemap m, city_data.edge e WHERE e.edge_id = m.edge_id;
+WITH edges AS ( 
+  SELECT m.arc_id, e.geom FROM edgemap m, city_data.edge e
+  WHERE e.edge_id = m.edge_id
+), points AS (
+  SELECT arc_id, (st_dumppoints(geom)).* FROM edges
+), compare AS (
+  SELECT p2.arc_id,
+         CASE WHEN p1.path IS NULL THEN p2.geom
+              ELSE ST_Translate(p2.geom, -ST_X(p1.geom), -ST_Y(p1.geom))
+         END AS geom
+  FROM points p2 LEFT OUTER JOIN points p1
+  ON ( p1.arc_id = p2.arc_id AND p2.path[1] = p1.path[1]+1 )
+  ORDER BY arc_id, p2.path
+), arcsdump AS (
+  SELECT arc_id, (regexp_matches( ST_AsGeoJSON(geom), '\[.*\]'))[1] as t
+  FROM compare
+), arcs AS (
+  SELECT arc_id, '[' || array_to_string(array_agg(t), ',') || ']' as a FROM arcsdump
+  GROUP BY arc_id
+  ORDER BY arc_id
+)
+SELECT '}, "arcs": [' UNION ALL
+SELECT array_to_string(array_agg(a), E',\n') from arcs
 
 -- footer
-SELECT ']}'::text as t
+UNION ALL SELECT ']}'::text as t;
 
 -- Result:
 { "type": "Topology", "transform": { "scale": [1,1], "translate": [0,0] }, "objects": {
 "P3P4": { "type": "MultiPolygon", "arcs": [[[-1]],[[6,5,-5,-4,-3,1]]]}
 }, "arcs": [
-[[25,30],[31,30],[31,40],[17,40],[17,30],[25,30]]
-[[35,6],[35,14]]
-[[35,6],[47,6]]
-[[47,6],[47,14]]
-[[47,14],[47,22]]
-[[35,22],[47,22]]
-[[35,14],[35,22]]
-]}
+ [[25,30],[6,0],[0,10],[-14,0],[0,-10],[8,0]],
+ [[35,6],[0,8]],
+ [[35,6],[12,0]],
+ [[47,6],[0,8]],
+ [[47,14],[0,8]],
+ [[35,22],[12,0]],
+ [[35,14],[0,8]]
+ ]}
 </programlisting>
             </refsection>
          </refentry>