GeoJSON (.geojson) and shapefiles (.shp) are file formats for storing geographic data structures. This guide will go through three methods for converting a GeoJSON file into a shapefile.
Method 1: Using the ogr2ogr shell command
This command comes with the GDAL package, which can be installed from https://gdal.org/download.html#current-release (check the steps that match your platform if you need to install it). I found it preinstalled on Windows.
For Debian-based Linux users, you can install GDAL with the following command.
1 |
sudo apt install gdal-bin |
Once GDAL is installed, you can use the ogr2ogr command directly from the command line to convert GeoJSON to SHP.
1 |
ogr2ogr -f "ESRI Shapefile" </path/to/the/destination/shapefile> </path/to/geojson> |
For example, the following command converts the example.geojson file in the current directory into shapefile.
1 |
ogr2ogr -f "ESRI Shapefile" ./dest_shp/destination_data.shp example.geojson |
You can use the subprocess or os modules to run the ogr2ogr command within a Python script.
In particular, we will use os.system() and subprocess.call() functions to run the ogr2ogr command as a shell process (just like running the command above in the terminal). Here are the examples.
1 2 3 4 |
import subprocess # subprocess.call() accepts a list of command arguments subprocess.call(['ogr2ogr', '-f', 'ESRI Shapefile', './dest_shp/destination_data.shp', 'example.geojson']) |
And for the os.system() function, we will work as follows.
1 2 3 4 |
import os # os.system() accepts command arguments as a string os.system("ogr2ogr -f 'ESRI Shapefile' ./dest_shp/destination_data.shp example.geojson") |
After going through one of the above three options (using the command line, os, or subprocess), let’s check the contents of dest_shp (the output folder).
The output contains four files. All these files make up a complete shapefile.
Note: A valid shapefile contains at least three files. We will discuss what these files are for in the last section.
Method 2: Using the geopandas library
First, install the package using pip by running this command (https://geopandas.org/en/stable/docs/user_guide/io.html).
1 |
pip install geopandas |
This module calls GDAL and Fiona libraries in the background to provide a simple way of converting GeoJSON to SHP. With this package, you conduct conversion in two simple steps: read the GeoJSON and write the data as SHP. Here is an example,
1 2 3 4 5 6 |
import geopandas # Read the GeoJSON geo = geopandas.read_file('example.geojson') # Write the data into Shapefile (.shp) geo.to_file('./file_shp/file.shp') |
Once again, let’s view the output files in the file_shp folder.
Fiona package (https://pypi.org/project/Fiona/) also natively supports the conversion of GeoJSON into Shapefile. You can find an example here: https://gis.stackexchange.com/questions/404777/creating-shapefile-from-json-file-in-python.
Method 3: Using osgeo.gdal() function
The GDAL package discussed in the first method provides the osgeo module that can easily convert GeoJSON into SHP.
1 2 3 4 5 6 |
from osgeo import gdal # Open the GeoJSON src = gdal.OpenEx('example.geojson') # Translate the vector data into Shapefile dest = gdal.VectorTranslate('./test_shp/test.shp', src, format='ESRI Shapefile') |
Files on a Shapefile
A valid shapefile has at least three files described below:
- .shp – the file that holds the actual geometry for all the features,
- .shx – the file containing the geometry indices,
- .dbf – the file that stores feature attributes in a dBASE table.
Other files that can be contained within the shapefile include the following:
- .prj – contains information on projection format, including the coordinate system and projection information.
- .sbn and .sbx – any of these files hold the spatial indices of the features.
- .shp.xml – this file contains the geospatial metadata in XML format.
- .cpg – a file used to specify the code page for identifying the character encoding to be used.