Name

AsTopoJSON — 返回拓扑几何的 TopoJSON 表示形式。

Synopsis

text AsTopoJSON(topogeometry tg, regclass edgeMapTable);

描述

返回拓扑几何的 TopoJSON 表示形式。 如果edgeMapTable不为空,它将用作边标识符到弧索引的查找/存储映射。 这是为了能够在最终文档中允许紧凑的“arcs”数组。

该表(如果给定)应具有“serial”类型的“arc_id”字段和整数类型的“edge_id”字段; 该代码将查询表中的“edge_id”,因此建议在该字段上添加索引。

[Note]

TopoJSON 输出中的弧索引是从 0 开始的,但在“edgeMapTable”表中它们是从 1 开始的。

除了该函数返回的片段之外,完整的 TopoJSON 文档还需要包含实际的弧和一些标头。 请参阅 TopoJSON 规范

可用性:2.1.0

增强:2.2.1 添加了对 puntal 输入的支持

相关信息

ST_AsGeoJSON

示例

CREATE TEMP TABLE edgemap(arc_id serial, edge_id int unique);

-- header
SELECT '{ "type": "Topology", "transform": { "scale": [1,1], "translate": [0,0] }, "objects": {'

-- objects
UNION ALL SELECT '"' || feature_name || '": ' || AsTopoJSON(feature, 'edgemap')
FROM features.big_parcels WHERE feature_name = 'P3P4';

-- arcs
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
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],[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]]
 ]}