Skip to content

Instantly share code, notes, and snippets.

@andrewbattista
Last active July 2, 2020 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewbattista/7e9170192dc4334a547c2e3c09be4fb0 to your computer and use it in GitHub Desktop.
Save andrewbattista/7e9170192dc4334a547c2e3c09be4fb0 to your computer and use it in GitHub Desktop.
Recursive conversion between formats with GDAL

Recursive conversion with GDAL

This shell script assumes that you have already installed GDAL and are converting between .KMZ files and Shapefiles with the names preserved. The specific ogr2ogr commands can be changed as needed. This also assumes many .KMZ files in a single directory. Kudos to Marii Nyrop.

Begin by creating a file named batch-separate-folders.sh with the following and place it in your directory with the KMZs:

for KMZ in *.kmz; do
  IDENTIFIER="$(basename ${KMZ} .kmz)"
  if [ ! -d "${IDENTIFIER}" ];
  then
    mkdir ${IDENTIFIER}
  fi
  SHP="${IDENTIFIER}/${IDENTIFIER}.shp"
  ogr2ogr -f 'ESRI Shapefile' ${SHP} ${KMZ}
  echo "Exported ${KMZ} to ${SHP}"
done

Then run sh ./batch-separate-folders.sh

Alternately, you can place the shapefiles in a single folder. You will want to do this if batch loading files into QGIS. Simply create a file called batch-single-folder.sh with the contents below and place it in the directory will all of your KMZs:

DIR="shapefiles"
if [ ! -d "${DIR}" ];
then
  mkdir ${DIR}
fi
for KMZ in *.kmz; do
  TARGET="$(basename ${KMZ} .kmz).shp"
  ogr2ogr -f 'ESRI Shapefile' ${TARGET} ${KMZ}
  echo "Exported ${KMX} to ${DIR}/${TARGET}"
done

Then run sh ./batch-single-folder.sh

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