[GIS] TopoJSON without Quantization

geojsontopojson

I have been investigating using TopoJSON files to map floor plans for various buildings. As GeoJSON these files render perfectly, however after converting to TopoJSON they are missing a lot of the lines used to render them and the number of geometries is much less than the number of features in the GeoJSON.

I experimented by increasing the quantize parameter but after a certain point it doesn't add any more detail. Is it possible to convert from GeoJSON to TopoJSON without having my features quantized?

Best Answer

TopoJSON uses fixed-precision integer coordinates, so no, you can’t create a TopoJSON file without first quantizing coordinates. The precision is controlled with the -q argument to topojson, as described in the command-line reference.

(Edit: as of TopoJSON 1.4, you can now construct lossless TopoJSON without quantization by saying --no-quantization (or equivalently -q 0).)

That said, it doesn’t sound like quantization is your problem here. Instead, I would guess that your input coordinates are not in WGS84 / EPSG:4326. I would use ogr2ogr to convert your geometry to spherical coordinates in degrees before converting to TopoJSON. For example:

ogr2ogr -f 'ESRI Shapefile' -t_srs EPSG:4326 input-fixed.shp input.shp
topojson -o output.json -- input-fixed.shp

Quantization is unlikely to be the problem here because quantization is localized to the input features’ bounding box, and thus automatically adapts to both small and large features. If you have a map of a very small space (such as building floor plans, and not the western hemisphere), the quantized coordinates will be correspondingly more precise, even at the same value of Q (topojson's -q), because the scale of the topology’s transform will be that much larger.

For example, say the bounding box of your input features is [[-122.395, 37.794], [-122.391, 37.797]], approxiamtely San Francisco’s Ferry Building. And say you used the default Q = 10,000. The quantized fixed coordinate [0,0] then maps to [-122.395, 37.794], and the fixed coordinate [9999,9999] maps to [-122.391, 37.797]. In other words, each increment in longitude corresponds to approximately 0.0000004° (4e-7), a tiny amount.

In contrast, with a world map spanning [[-180, -90], [180, 90]] at the same Q = 10,000, each increment in longitude corresponds to approximately 0.036°.