TopoJSON output

TopoJSON is another topological format, designed with web mapping in mind. It is supported directly by the D3 and OpenLayers (starting with version 3) frameworks, and can be converted to GeoJSON with a TopoJSON library for use with other tools.

One difficulty with this format is that the edges (called arcs in TopoJSON parlance) are defined by index, while in PostGIS topology they are defined by IDs. To overcome this, an intermediate table called edgemap has to be created:

CREATE TABLE edgemap(arc_id serial, edge_id int unique); 
This table can be temporary, so PostgreSQL will automatically drop it at the end of a session.

Now the function is ready to be used. It accepts two arguments: the TopoGeometry column and the name of the intermediate table:

SELECT topology.astopojson(topogeom,'edgemap') FROM countries WHERE name='Nulland'; 

astopojson
------------------------------------------------
{ "type": "MultiPolygon", "arcs": [[[2,1,0]]]}

The intermediate table will have its rows populated with arc indexes and edge IDs:

SELECT * FROM edgemap; 
arc_id | edge_id
--------+---------
1 | 4774
2 | 4766
3 | 4779

Now the TopoJSON document can be composed using the fragments generated by the topology.AsTopoJSON function and edge geometries. An example, using SQL functions only (no processing in the application layer) was shown in Chapter 7, PostGIS - Creating Simple WebGIS Applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset