Next, we will move on to preparing this data relationship for the Web and its spatiotemporal visualization.
TopoJSON is a variant of JSON, which uses the topological relationships between the geometric features to greatly reduce the size of the vector data and thereby improves the browser's rendering performance and reduces the risk of delay due to data transfers.
The following code is an example of GeoJSON, showing two of our State House Districts. The format is familiar—based on our previous work with JSON—with sets of coordinates that define a polygonal area grouped together. The repeated sections are marked by ellipses (…).
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "STATEFP": "42", "SLDLST": "181", "GEOID": "42181", "NAMELSAD": "State House District 181", "LSAD": "LL", "LSY": "2014", "MTFCC": "G5220", "FUNCSTAT": "N", "ALAND": 8587447.000000, "AWATER": 0.000000, "INTPTLAT": "+39.9796799", "INTPTLON": "-075.1533540" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -75.176782, 39.987337 ], [ … ] ]] } }, {…}]}The following code is the corresponding representation of the same two State House Districts in TopoJSON, as discussed earlier.
Although this example uses the same coordinate system (WGS84/EPSG:4326) as that used before, it is expressed as simple pairs of abstract space coordinates. These are ultimately transformed into the WGS coordinate system using the scale and translate data in the transform section of the data.
By taking advantage of the shared topological relationships between geometric objects, the amount of data can be drastically reduced from 21K to 7K. That's a reduction of 2/3! You will see in the following code that each polygon is not clearly represented on its own but rather through these topological relationships. The repeated sections are marked by ellipses (…).
{"type":"Topology","objects":{"geojson":{"type":"GeometryCollection","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"geometries":[{"type":"Polygon","properties":{"STATEFP":"42","SLDLST":"181","GEOID":"42181","NAMELSAD":"State House District 181","LSAD":"LL","LSY":"2014","MTFCC":"G5220","FUNCSTAT":"N","ALAND":8587447,"AWATER":0,"INTPTLAT":"+39.9796799","INTPTLON":"-075.1533540"},"arcs":[[0,1]]},{"type":"Polygon","properties":{"STATEFP":"42","SLDLST":"197","GEOID":"42197","NAMELSAD":"State House District 197","LSAD":"LL","LSY":"2014","MTFCC":"G5220","FUNCSTAT":"N","ALAND":8251026,"AWATER":23964,"INTPTLAT":"+40.0054726","INTPTLON":"-075.1405813"},"arcs":[[-1,2]]}]}},"arcs":[[[903,5300],[…]]]],"transform":{"scale":[0.0000066593659365934984,0.000006594359435944118],"translate":[-75.176782,39.961088]}}Similar to TopoJSON, Vector simplification removes the nodes in a line or polygon layer and will often greatly increase the browser's rendering performance while decreasing the file size and network transfer time.
As the vector shapes can have an infinite level of complexity, in theory, simplification methods can decrease the complexity by more than 99 percent while still preserving the perceivable shape of the geometry. In reality, the level of complexity at which perception becomes significantly affected will almost never be this high; however, it is common to have a very acceptable perception change at 90 percent complexity loss. The more sophisticated simplification methods have improved results.
A number of simplification methods are commonly in use, each having strengths for particular data characteristics and outcomes. If one does not produce a good result, you can always try another. In addition to the method itself, you will also usually be asked to define a threshold parameter for an acceptable amount of complexity loss, as defined by the percentage of complexity lost, area, or some other measure.
The Visvalingam effective area method is the only method of simplification offered in the TopoJSON command-line tool that we will use. Mapshaper, a web-based tool that we will take a look at offers all these three methods but uses the weighted area method by default.
Other options affecting the data size are often offered alongside the simplification methods.
Both Web and desktop TopoJSON conversion tools that we will use support these simplification options. That way, you can simplify a polygon at the same time as you reduce the data size through the topological relationship notation.
If you wish to produce data other than for TopoJSON, you will need to find another way to do the simplification.
QGIS provides Simplify Geometries out of the box (navigate to Vector | Geometry Tools | Simplify Geometries), which does a Douglas-Peucker simplification. While it is the most popular method, it may not be the most effective one (see the following section for more).
The Simplify plugin offers a Visalvingam method in addition to Douglas-Peuker.
There are a few options for writing TopoJSON. We will take a look at one for the desktop, which requires a software installation, and one via the web browser. As you might imagine, the desktop option will be more stable for doing anything in a customized way, which the web browser does not support, and is also more stable with the more complex feature sets. The web browser has the advantage of not requiring an install.
You can use the web-based mapshaper software from http://www.mapshaper.org/ to convert from shapefile and other formats to TopoJSON and vice versa. Perform the following steps to convert the State Legislative Districts shapefile to TopoJSON:
c5/data/original/tl_2014_42_sldl/tl_2014_42_sldl.shp) from your local computer or drag a file in. As the page indicates, Shapefile, GeoJSON, and TopoJSON are supported.You will get the following screen in your browser:

The command-line tool is useful if you are working with a larger or more complicated dataset. The downside is that it requires that you install Node.js as it is a node package. For our purposes, Node.js is similar to Python. It is an interpreter environment for JavaScript, allowing the programs written in JavaScript to be run locally. In addition, it includes a package manager to install the needed dependencies. It also includes a web server—essentially running JavaScript as a server-side language.
Perform the following steps:
>npm install -g topojson
cd c:\packt\c5\data\output and input the following:
>topojson -p -o house_district.json house_district.shp
You will now get the following output:

Mapshaper also has a command-line tool, which we did not evaluate here.