[GIS] Issue with using TopoJSON server API to return TopoJSON object with properties

d3geojsonnode-jstopojson

I am creating a node application and using TopoJSON server API reference (see the link below) to convert a GeoJSON object to a TopoJSON object.

TopoJSON server API reference

I used these three lines of code below which returns the TopoJSON topology but without returning the properties from the GeoJSON object.

var collection = {type: "FeatureCollection", features: […]}; // GeoJSON
var topology = topojson.topology({collection: collection}); // convert to TopoJSON
console.log(topology.objects.collection); // inspect TopoJSON

However, I also want to include the properties of each topology to the final TopoJSON object, and according to the API, I need to use this function below copy all properties from the GeoJSON object:

function propertyTransform(properties, key, value) {
  properties[key] = value;
  return true;
}

But my issue is I don't know how to use this propertyTransform function as an option inside of the topojson.topology() function in order to return the TopoJSON object with properties.

Does anyone know how to do it or provide me some example code?

Thanks a lot!

Vanni

Best Answer

The documentation is not that clear, I had the same issue and needed to dig around in the topojson test scripts to work it out.

https://github.com/mbostock/topojson/blob/master/test/feature-test.js

You do it by passing a function as an option, like this:

var geojson = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[106.8575693,-6.2179629]},"properties":{"pkey":25,"created_at":"2014-02-12 18:03:13"}}]}

var topojson = topojson.topology({collection:geojson},{"property-transform":function(object){return object.properties;}});`

This will encode the feature properties in the topology.