[943] Converting a GeoJSON file to a Shapefile in Python

发布时间 2023-11-17 07:33:16作者: McDelfino

To convert a GeoJSON file to a Shapefile in Python, you can use the geopandas library, which provides convenient tools for working with geospatial data formats. Here are the steps to do this:

Once you have geopandas installed, you can use it to read the GeoJSON file and then save it as a Shapefile. Here's an example of how to do this:

import geopandas as gpd

# Replace 'input.geojson' with the path to your GeoJSON file
geojson_file = 'input.geojson'

# Replace 'output.shp' with the desired name for your Shapefile
shapefile = 'output.shp'

# Read the GeoJSON file using geopandas
gdf = gpd.read_file(geojson_file)

# Save the GeoDataFrame as a Shapefile
gdf.to_file(shapefile, driver='ESRI Shapefile')

In this code:

  • You import the geopandas library.
  • You specify the path to your input GeoJSON file (replace 'input.geojson' with the actual path).
  • You specify the desired name for the output Shapefile (replace 'output.shp' with the desired name).

Make sure you have the necessary permissions to read from the input directory and write to the output directory. This code will read the GeoJSON file, convert it into a GeoDataFrame (a data structure for working with geospatial data), and then save it as a Shapefile using the specified name.

Once you run this code, you should have a Shapefile (with .shp, .shx, .dbf, and other associated files) in the specified output location.