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

2 Answers 2

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.

5
  • 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 Commented Jun 27, 2014 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 ] } Commented Jun 27, 2014 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. Commented Jun 27, 2014 at 18:40
  • @VanniZhang try Jason's suggestion as it works for me, and I'm on 1.6.14.
    – Tomas
    Commented Jun 28, 2014 at 2:33
  • Thanks a lot guys, I got it working after I installed the latest version of API! Commented Jun 28, 2014 at 2:49
0

The answer by @Tomas works for me. Thanks. And I code the following snippets to identify geojson and topojson respectively.

// Firstly reference topojson library. topojson-server for convert geojson to topojson. Maybe you need other reference style. Here's observable website style
function _topojson(require) {
  return (
    require('topojson-client@3', 'topojson-simplify@3', 'topojson-server')
  )
}
// requested geojson or topojson address
const url = `https://raw.githubusercontent.com/nvkelso/natural-earth-vector/ca96624a56bd078437bca8184e78163e5039ad19/geojson/ne_50m_admin_0_countries.geojson`;
// const url = `https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json`;

// request data by url
function _world(require, d3) {
  let countries = get_file({ url, d3, require });
  return countries
}

function _borders(topojson, world) { // function to handle data with different format to get countries border in my project

  let countries = null
  if (url.endsWith('geojson')) { // geojson format handler
    world = topojson.topology({ collection: world }, { "property-transform": function (object) { return object.properties; } })
    countries = world.objects.collection
  } else {
    countries = world.objects.countries; // maybe need adjust the property according to different sources
  }
  if (world.objects) {
    const borders = topojson.mesh(world, countries, (a, b) => a !== b);
    return borders
  } else {
    debugger
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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