How to Convert a shapefiles to Geojson
Shapefiles and GeoJSON are both widely used file formats for storing and exchanging geographic data. However, they have some key differences in terms of file format, data structure, feature attributes, size, and support. In this blog post, we will explore the process of converting shapefiles to GeoJSON using various methods such as online tools, command line tools, programming libraries, and GIS software like QGIS, Fiona and PyShp, ArcMap, and R.
Quickmaptools.com is by far the easiest way to convert between Shapefiles and Geojson online. But if you are looking for desktop options continue reading!
We will also compare the key differences between shapefiles and GeoJSON and discuss when and why you might want to convert one format to the other. Whether you’re a GIS professional, a developer, or a data scientist, this post will provide you with the information you need to effectively work with these two popular formats.
Key Differences Between Shapefiles and GeoJSON
GeoJSON and shapefile are both file formats used to store and exchange geographic data, but they have some key differences:
- File format: GeoJSON is a JSON-based format, which stands for JavaScript Object Notation. It is a lightweight, text-based format that can be easily read and written by both humans and machines. Shapefile, on the other hand, is a binary format that is typically stored in multiple files with different file extensions, such as .shp, .shx, and .dbf.
- Data structure: GeoJSON uses a simple JSON data structure to store geographic features, with each feature represented as a collection of key-value pairs. Shapefile, on the other hand, stores geographic features in a more complex structure, with each feature represented by a set of related files that store different types of information, such as geometry, attributes, and indexing.
- The feature attributes: GeoJSON stores feature attributes as key-value pairs within the feature object, which makes it easy to access and query specific attributes. Shapefile stores feature attributes in a separate file with a .dbf extension, which makes it more difficult to access and query specific attributes.
- Size: Shapefiles tend to be larger in size than the equivalent GeoJSON file, especially if the shapefile contains many features or complex geometries.
- Support: GeoJSON is a widely supported format, and it can be used with a variety of GIS software, web mapping libraries, and other tools. Shapefile, on the other hand, is primarily used with Esri ArcGIS software and other GIS tools that support the Esri file format.
- Use cases: GeoJSON is widely used in web mapping and web-based GIS applications, and it is also widely supported in many GIS software and libraries, while shapefile is mostly used in Esri ArcGIS and other GIS software that support the Esri file format.
Feature | GeoJSON | Shapefile |
---|---|---|
File format | JSON-based | Binary |
Data structure | Simple JSON data structure | Complex structure with different files for different types of information |
Feature attributes | Stored as key-value pairs within the feature object | Stored in a separate .dbf file |
Size | Generally smaller | Can be larger, especially for shapefiles with many features or complex geometries |
Support | Widely supported in web mapping, web-based GIS, and various GIS software and libraries | Primarily used with Esri ArcGIS and other desktop/server-side GIS software |
Use cases | Web mapping, web-based GIS, and various GIS software and libraries | Esri ArcGIS and other desktop/server-side GIS software |
Converting a Shapefile to GeoJson using Ogr2ogr
ogr2ogr: A command line tool that is part of the GDAL library and can be used to convert between various vector file formats, including shapefiles and GeoJSON.
The basic process for converting a shapefile to GeoJSON using ogr2ogr is to run the following command:
ogr2ogr -f "GeoJSON" output.json input.shp
Converting a Shapefile to GeoJson using QGIS
Here is a step-by-step process for converting a shapefile to GeoJSON using QGIS:
- Open QGIS and click on the “Add Vector Layer” button or go to Layer > Add Layer > Add Vector Layer.
- In the “Source” field, browse to the location of the shapefile that you want to convert. Select the file and click on “Open.”
- Once the shapefile is loaded, go to Layer > Save As.
- In the “Save As” dialog box, select “GeoJSON” as the “Save as type.”
- In the “File name” field, provide a name for the output GeoJSON file and select a location to save the file.
- Click on the “OK” button to start the conversion process.
- Once the conversion is complete, the GeoJSON file will be saved in the location you specified.
You can also use the Vector > Data Management Tools > Export > Save selected features as...
to save the selected features in a shapefile as a geojson file.
Further Reading: Creating Shapefile in QGIS
Converting a Shapefile to GeoJson using Python
Here is a step-by-step process for converting a shapefile to GeoJSON using Fiona and PyShp libraries in Python:
Install Fiona and PyShp libraries using pip:
pip install Fiona
pip install pyshp
Import the necessary libraries in your Python script:
import fiona
import shapefile
Open the shapefile using Fiona:
with fiona.open("input.shp") as src:
meta = src.meta
Create a new shapefile writer using PyShp and set the necessary parameters:
with fiona.open("input.shp") as src:
for feature in src:
shp.shape(feature['geometry'])
shp.record(feature['properties'])
Save the new shapefile:
shp.save()
Once the script is executed, it will create a new GeoJSON file called “output.json” in the same location as your input shapefile.
Converting a Shapefile to GeoJson using ArcGIS
Here is a step-by-step process for converting a shapefile to GeoJSON in ArcGIS:
- Open ArcMap and add the shapefile to the map by clicking the “Add Data” button or going to File > Add Data > Add Data.
- Right-click on the shapefile in the Table of Contents and select “Data” > “Export Data.”
- In the “Export Data” dialog box, select “GeoJSON” as the “Output format.”
- In the “Output file” field, provide a name for the output GeoJSON file and select a location to save the file.
- Click on the “OK” button to start the conversion process.
- Once the conversion is complete, the GeoJSON file will be saved in the location you specified.
Alternatively, you can also use “ArcToolbox” to convert shapefile to geojson,
- Open the ArcToolbox window by clicking the “ArcToolbox” button on the main toolbar or going to “Tools” > “ArcToolbox.”
- Navigate to “Conversion Tools” > “To Geodatabase” > “Features To JSON”
- Fill the necessary fields, like Input Features, Output json file.
- Run the tool, Once the process is done, you will have a geojson file in the location you specified.
.
Converting a Shapefile to GeoJson using R
Here is a step-by-step process for converting a shapefile to GeoJSON in R:
Install and load the rgdal
and geojsonio
packages:
install.packages("rgdal")
install.packages("geojsonio")
library(rgdal)
library(geojsonio)
Use the readOGR
function from rgdal
package to read the shapefile and store it in a variable:
shapefile <- readOGR("path/to/shapefile.shp")
Use the geojson_write
function from geojsonio
package to write the shapefile to a GeoJSON file:
geojson_write(shapefile, "path/to/output.geojson")
Alternatively, you can also use the sf
package to convert shapefile to geojson,
Install and load the sf
package:
install.packages("sf")
library(sf)
Use the st_read
function from sf
package to read the shapefile and store it in a variable:
shapefile <- st_read("path/to/shapefile.shp")
Use the st_write
function from sf
package to write the shapefile to a GeoJSON file:
st_write(shapefile, "path/to/output.geojson")
Convert a shapefile to GeoJSON online
Here are two great options for converting Shapefiles to GeoJSON online and for free!
Try https://products.aspose.app/gis/conversion/shapefile-to-geojson
or https://mygeodata.cloud/converter/shp-to-geojson