Skip to content

Instantly share code, notes, and snippets.

@fcavalcantirj
Last active May 15, 2017 21:54
Show Gist options
  • Save fcavalcantirj/3513d9ddb26cab0bebc3933253282c17 to your computer and use it in GitHub Desktop.
Save fcavalcantirj/3513d9ddb26cab0bebc3933253282c17 to your computer and use it in GitHub Desktop.
Bash Script that takes series of kmz files and convert to shapeFiles using ogr2ogr lib.
#!/bin/bash
#******************************************************************************
#
# Name: kmzToKmL.sh
# Project: fretamento
# Purpose: Convert series of kmz files into shapeFiles
# Author: FCavalcanti, BraveInvestments
# Requirements: Make sure ogr2ogr is installed and available on path
# Usage: chmod +x kmzToKmL.sh && ./kmzToKmL.sh
# Inspiration: https://gist.github.com/albert-decatur/714209
#
# https://gis.stackexchange.com/questions/80954/os-x-mavericks-accessing-gdal-from-terminal-issue
#
# ******************************************************************************
# Copyright (c) 2017, FCavalcanti, BraveInvestments
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#******************************************************************************
# step 1
# rename kmz to zip
for kmzFile in ./kmz/*.kmz
do
cp "$kmzFile" "${kmzFile/.kmz/.zip}"
mv "${kmzFile/.kmz/.zip}" "./zip"
done
# step 2
# unzip all files into /zip directory
for zipFile in ./zip/*.zip
do
n=$(unzip -lqq $zipFile | awk '{print $NF}')
e=${n#*.}
unzip $zipFile && mv $n ${zipFile%%_*}".$e"
done
# step 3
# move all kml files from /zip to /kml
for kmlFile in ./zip/*.kml
do
mv "${kmlFile}" "./kml"
done
# step 4
# remove freaking 'zip' from filename (bad step 3)
for filename in ./kml/*.kml; do
[ -f "$filename" ] || continue
mv "$filename" "${filename//zip/}"
done
# step 5
# convert all kml files into shapeFiles
for kml in ./kml/*.kml; do
tempFilename=$(basename "$kml")
extension="${tempFilename##*.}"
filename="${tempFilename%.*}$extension"
ogr2ogr -f 'ESRI Shapefile' ./shp/"$filename" "$kml"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment