2

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

3

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.

  • Hi Tomas, I tried your suggestion and seems it is still not passing me the topojson object with properties. Here is the topojson I got by using the code you provided – Vanni Zhang Jun 27 '14 at 17:55
  • { "type": "Topology", "objects": { "collection": { "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [ 0, 0 ] } ] } }, "arcs": [], "transform": { "scale": [ 1, 1 ], "translate": [ 106.8575693, -6.2179629 ] }, "bbox": [ 106.8575693, -6.2179629, 106.8575693, -6.2179629 ] } – Vanni Zhang Jun 27 '14 at 17:55
  • 1
    You need to make sure you're using version 1.6.11 or later (latest is 1.6.14 at the time of writing). The API for property transforms changed slightly in 1.6.11. – Jason Davies Jun 27 '14 at 18:40
  • @VanniZhang try Jason's suggestion as it works for me, and I'm on 1.6.14. – Tomas Jun 28 '14 at 2:33
  • Thanks a lot guys, I got it working after I installed the latest version of API! – Vanni Zhang Jun 28 '14 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.