Convert Shapefile to TopoJSON ESRI shapefile are the binary vector data storage format for storing the location, shape, and attributes of geographic features. It is stored as a set of related files and contains one feature class. Whereas TopoJSON (topological geospatial data) is an extension of GeoJSON that encodes topology. The necessity of conversion is to reduce the size of data. While using Shapefile you have to take care of all other mandatory files such as .shx, .dbf, .prj etc. To use shape file zip file is needed so that all file can be processed together. Whereas in other data format such as geojson and topojson one file contains all the necessary information.
Here are two ways to convert shapefile to topojson
Offline Method : Convert Shapefile to topojson through GDAL and then through geo2topo tool npm:
To convert shape file to topojson format offline you need to download GDAL and Node.js. First step is to convert shapefile to geojson data format, which is done by using ogr2ogr command available in GDAL package. After successful installation check ogr2ogr availability by typing ogr2ogr in command prompt or by typing dir that will show all available directories.
Now you can convert shapefile to geojson data format by using given command
- Ogr2ogr –f Geojson file_name.json input.shp
Here –f option is used to shows the file format, which shows output file format is geojson. GDAL takes name of output file prior to input file.
Convert geojson to topojson:
Now to convert geojson data to topojson data format you have to download node.js which is available https://nodejs.org/en/ here. After successful installation you need to check npm availability using command
- npm –check.
If not available use
- npm install -g npm-check
After this you need to install topojson-server for conversion of data that can be done by using
- npm install topojson-server command
Now all setup is ready
Use geo2topo command to convert geojson data to topojson data
- geo2topo –o geo.json topo.json
Here –o option is used to give the output. If command doesn’t run and gives error as no such file or directory. Then specify full path for file for both input and output. The older versions were having topojson as a command to convert geojson data format to topojson data format. Topojson is named as geo2topo command in new version.
Convert Shapefile to TopoJSON – Online Method
Mapshaper is an online tool that converts shape file to topojson data format directly. You only need to drag the shape file to http://mapshaper.org/ site. Then use export option to convert this data into topojson format. In Mapshaper there are many option are available to simplify the geometry, which you can explore.
I hope this article helped you in converting the file from Shapefile to TopoJSON. If you find any problem in performing operation do let me know by commenting below.