Search code examples
leafletgeojsontopojson

TopJSON in JS converted to GeoJSON


I am tinkering with file which has GeoJSON data of states but stored as a variable in JS. Here is the file http://leafletjs.com/examples/us-states.js

However I am now using Topojson and reading the documentation https://github.com/mbostock/topojson/wiki/API-Reference topojson.feature(topology, object) converts GeoJSON to Topojson.

However is there a way that I can store the Topojson format in JS format beforehand like I was doing for Geojson and then convert to GeoJSON calling some function?

Any help will be appreciated!


Solution

  • TopoJSON has a server and clientside API. Serverside (commandline) is for turning GeoJSON (and other formats, shp etc) into a smaller format called TopoJSON. Clientside API turns TopoJSON back into GeoJSON.

    The TopoJSON client API supports converting TopoJSON objects back into GeoJSON for use in a web browser. This functionality is provided by topojson.js

    Ofcourse you can edit TopoJSON files, just as well as you can edit GeoJSON files. They are both subsets of JSON. Javascript Object Notation.

    JSON, (canonically pronounced /ˈdʒeɪsən/ jay-sən;[1] sometimes JavaScript Object Notation), is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the primary data format used for asynchronous browser/server communication (AJAJ), largely replacing XML (used by AJAX).

    You can edit Geo just as well as Topo, it's just text.

    Server: https://github.com/mbostock/topojson/wiki/API-Reference#server-api

    Client: https://github.com/mbostock/topojson/wiki/API-Reference#client-api