You may be interested to refer following articles:
Different ways to convert Shapefile to GeoJSON
I.) Online Method : Convert Shapefile to GeoJSON:
The simplest and fast way to access the internet is through the online method, as you don’t need to install and configure any tool on your PC. Just drag the Shapefile files, one click on button and convert to GeoJSON. There are many website which provides the tool to convert the files by browsing them to server side and convert and also some client side conversion too. Lets explore the list, which is only client side as client side conversion will secure your file and doesn’t take your data pack for uploading the file:
a.) GIS Map Engine :
This is presented by Engineer Philosophy pvt. ltd, working in GIS Sector an IT company. What you need to do is to just Register, login and upload only .shp and .dbf file and then export the uploaded layer to GeoJSON file by just selecting the option which appear at upper right corner in Navigation bar. You can also explore the attribute table from the website directly and filter the same which no other online tool provides.
Not only this, but you can classify the Map according to the shapefile dbf attributes and automatically classify and color it. The classified map can be exported as Scalable Vector Graphics i.e svg file.
Disclaimer: The above project is handled by me and my team and the project is in beta phase while you can definetly convert the shapefile to GeoJSON at client side securing your shapefile data. I would welcome your suggestion to improve the tool by commenting below or contacting me.
b.) Map Shapper :
Map Shapper is the best tool which we should refer first. It is the best online tool which also handles large shapefiles and renders it quickly. Its an open source project and is licensed under MPL 2.0. Just drag the shapefile or zipped shapefile which will render the shapefile on map and can be further exported as geojson file with the option provided in tool itself.
With No doubt Map Shaper is much more better than any other tool for now, while the only reason why I placed this tool in second place just because the first tool was my own tool which provides more option for classifying the map, see all attributes in the data table and also exported as svg file.
2.) Offline Method – Convert Shapefile to GeoJSON in QGIS:
1.) Open QGIS Desktop and Select Layer -> Add Layer -> Add Vector Layer or click on Add Vector layer icon appears at the upper left in the application.
2.) Select the zipped shapefile or any of file i.e .shp or .dbf or .shx by browing the folder. Note, that all the three files i.e .shp, .dbf and .shx file should be present in the same foder with same name.
3.) Once uploaded, the map will be rendered and showed in QGIS application. Now navigate to Layer menu and select Save As option or just from the layers panel select the layer and right click from it to select Save As option.
4.) A window pop up will opne up. From the window just select the GeoJSON format and provide the name of the file in the input box as shown in the image provided below. Save the file. It will export the file in GeoJSON format.
3.) Offline Method – Convert Shapefile to GeoJSON with command line ogr2ogr tool:
Firstly you need to install an ogr2ogr tool of GDAL which is a GIS utlitly command line tool so as to convert one file to another. Now if the ogr2ogr is installed globally check by simple tying the ogr2ogr in command line if it is working or not. Or if it residing in some file system you need to navigate to ogr2ogr file and type in the same in command line. Now Just type in the following:
- “ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp”
To get more option list and format you may see this gdal org link.
This tool will be helpful if you want to convert bulk files from shapefile to geojson. You can refer the .sh script found at github.
4.) Convert Shapefile to GeoJSON -API or library:
Shapefile is a binary file and cannot be viewed directly in notepad or textpad as like with GeoJSON file. So if you want to create your own API, to convert Shapefile to GeoJSON you should first have a knowledge of how to read the binary shapefile. You may refer to some of the articles which I have written some post which would help you to get the basic knowledge to know the format of files and read.
You may further refer to some of the best Shapefile to GeoJSON converter API which is open source project:
https://github.com/wavded/js-shapefile-to-geojson : This is dedicated library to convert to GeoJSON, created by Marc Harter and Licensed with MIT.
https://github.com/gipong/shp2geojson.js : This library is also MIT Licensed and is inspired by the above wavded library which uploads zipped file of shapefile. The library provided an example to use with leaflet js.
https://github.com/mbloch/mapshaper : Map Shaper as desricbed above is licensed under MPL 2.0 and can be very useful if you want to convert to GeoJSON as well as TopoJSON.
Hope this article helped you. If are facing some problem in converting the shapefile to geojson do comment below with the problem statement. We will definitely look over that. Also you can provide your valuable comment below if you are using any other library or tool to cover the same.