1

There are many articles about how convert GeoJSON to TopoJSON using JavaScript, but I could not find how to convert TopoJSON to GeoJSON in the browser.

Response:

Using http://d3js.org/topojson.v1.min.js, TopoJSON API’s feature() method we can convert a high-level TopoJSON object back into a GeoJSON structure:

geojson = topojson.feature(topojson,topojson.objects[topologyKey]);
6
  • 1
    Which is the best article you could find? At what point did you get stuck?
    – Mesa
    Jan 28, 2019 at 19:55
  • I find the answer, thx very much, using the TopoJson: d3js.org/topojson.v1.min.js, i can convert topoJson in GeoJson:
    – gurrumo
    Jan 28, 2019 at 20:20
  • Using the TopoJSON API’s feature() method we can convert a high-level TopoJSON object back into a GeoJSON structure
    – gurrumo
    Jan 28, 2019 at 20:21
  • 1
    Welcome to GIS SE! As a new user, please be sure to take the short tour to learn about this site's focused Q&A format. Please also consider editing your question to address @Mesa's comment. And if you found your own solution, you can post (and accept) it as an answer!
    – Andy
    Jan 28, 2019 at 20:22
  • 1
    Please write your self-answer in the area reserved for answers and not inside the question body.
    – PolyGeo
    Jan 28, 2019 at 21:32

2 Answers 2

0

The answer isn't quite correct as the 1st topojson in geojson = topojson.feature(topojson, topojson.objects[topologyKey]); is the function from d3js.org/topojson... while the 2nd and 3rd topojson is an object variable, e.g. one uploaded from countries-110m.json. Let's call such variable topoJson then the correct statement is:

geojson = topojson.feature(topoJson,topoJson.objects[topologyKey]);

There is a more recent version: d3js.org/topojson.v3.min.js or if you prefer more readable JavaScript: "https://d3js.org/topojson.v3.js".

It may be also useful to note in

geojson = topojson.feature(topoJson,topoJson.objects[topologyKey]);

the topologyKey can be for instance countries or land; so if you are applying it to such topojson(s) like countries-110m.json, i.e.:

geojson = topojson.feature(topoJson,topoJson.objects.countries);

and

geojson = topojson.feature(topoJson,topoJson.objects.land);

And if you want the features of both, countries and land, then you can do:

countries = topojson.feature(topoJson, topoJson.objects.countries);
land = topojson.feature(topoJson, topoJson.objects.land);
geoJson = {
  type: 'FeatureCollection', 
  features: countries.features.concat(land.features)
};

(This can be put on just 2 lines of code...) (BTW using names like topoJson or geojson is a bit misleading - JSON is a text, while these variables are objects...)

0

sorry for posting a refrence but its nice readme says

" TopoJSON is an extension of GeoJSON that encodes topology. Rather than representing geometries discretely, geometries in TopoJSON files are stitched together from shared line segments called arcs. This technique is similar to Matt Bloch’s MapShaper and the Arc/Info Export format, .e00. "

https://github.com/topojson/topojson

//<script src="topojson.js">

    geojson = topojson.feature(topoJson,topoJson.objects[topologyKey]);
   

check out some esample data

https://jsoneditoronline.org/#left=cloud.366d7085cbe4445a8840ed1add74d617&right=cloud.f540ef831c104257aded604da3df1363

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.