Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Last active October 28, 2016 13:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmcbride/f1c652c6480ef04da60e to your computer and use it in GitHub Desktop.
Save bmcbride/f1c652c6480ef04da60e to your computer and use it in GitHub Desktop.
Bash script that uses GDAL/OGR to convert PDF Maps exported KML or KMZ Placemarks, Tracks, and Lines to shapefiles. Modify as needed for additional formats or custom attributes.
#!/bin/bash
echo "File type (kmz or kml):"
read TYPE
echo "Enter the filename (without the .kmz or .kml extension) to process:"
read FILENAME
# Unzip KMZ file
if [[ $TYPE = "kmz" ]]; then
Unzip "$FILENAME.kmz"
fi
# Create SQLite database
ogr2ogr -f "SQLite" -overwrite "$FILENAME.sqlite" "$FILENAME.kml" -lco LAUNDER=NO
# Create Shapefiles (add custom attributes between Desc, and substr)
ogr2ogr -a_srs "EPSG:4326" -overwrite -sql "SELECT geometry, Name AS Name, description AS Descrip, substr(replace(replace(replace(pdfmaps_photos, '<img src=\"', ''), '\" /><br />', ''), 'images/', ', '), 3) AS Photos, datetime(timestamp) AS Timestamp FROM placemarks" "placemarks.shp" "$FILENAME.sqlite"
ogr2ogr -a_srs "EPSG:4326" -overwrite -sql "SELECT geometry, Name AS Name, description AS Descrip, substr(replace(replace(replace(pdfmaps_photos, '<img src=\"', ''), '\" /><br />', ''), 'images/', ', '), 3) AS Photos, datetime(timestamp) AS Timestamp FROM tracks" "tracks.shp" "$FILENAME.sqlite"
ogr2ogr -a_srs "EPSG:4326" -overwrite -sql "SELECT geometry, Name AS Name, description AS Descrip, substr(replace(replace(replace(pdfmaps_photos, '<img src=\"', ''), '\" /><br />', ''), 'images/', ', '), 3) AS Photos, datetime(timestamp) AS Timestamp FROM lines" "lines.shp" "$FILENAME.sqlite"
# Remove intermediate SQLite file
rm $FILENAME.sqlite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment