Name

AsTopoJSON — Returns the TopoJSON representation of a topogeometry.

Synopsis

text AsTopoJSON(topogeometry tg, regclass edgeMapTable);

Description

Returns the TopoJSON representation of a topogeometry. If edgeMapTable is not null, it will be used as a lookup/storage mapping of edge identifiers to arc indices. This is to be able to allow for a compact "arcs" array in the final document.

The table, if given, is expected to have an "arc_id" field of type "serial" and an "edge_id" of type integer; the code will query the table for "edge_id" so it is recommended to add an index on that field.

[Note]

Arc indices in the TopoJSONjoutput are 0-based but they are 1-based in the "edgeMapTable" table.

A full TopoJSON document will be need to contain, in addition to the snippets returned by this function, the actual arcs plus some headers. See the TopoJSON specification.

Availability: 2.1.0

See Also

ST_AsGeoJSON

Examples

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

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

-- objects
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;

-- footer
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]]
]}