Skip to content

Instantly share code, notes, and snippets.

Last active November 10, 2022 21:05
What would you like to do?
Bulk convert shapefiles to geojson using ogr2ogr
# Bulk convert shapefiles to geojson using ogr2ogr
# For more information, see
# Note: Assumes you're in a folder with one or more zip files containing shape files
# and Outputs as geojson with the crs:84 SRS (for use on GitHub or elsewhere)
#geojson conversion
function shp2geojson() {
ogr2ogr -f GeoJSON -t_srs crs:84 "$1.geojson" "$1.shp"
#unzip all files in a directory
for var in *.zip; do unzip "$var"; done
#convert all shapefiles
for var in *.shp; do shp2geojson ${var%\.*}; done
# You'd probably want to `mv *.geojson [path-to-git-repo]/` at this point
# so you could commit the file to GitHub
# Happy mapping!
Copy link


Copy link

Heres a simple one liner if you already have a folder containing all the .shp that you want converted. Just cd into the folder containing the shapefiles and run this:

for filename in ./*.shp; do mkdir -p geojson; ogr2ogr -f "GeoJSON" "./geojson/$filename.geojson" "$filename";done

Install gdal if you get a ogr2ogr command not found error:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment