#!/bin/bash

# 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.

#
# Author: Cristian Balint <cristian.balint@gmail.com>
#

# arhive originals
rm -rf *.zip *.kml *.kmz
zip -9 mgi-all.zip mgi-025k.??? mgi-050k.??? mgi-100k.???

# remove files
rm -rf mgi-???k-merc.???

# from longlat / to mercator (use MGI Balkan Zone 7 over Srbija)
ogr2ogr -s_srs '+proj=longlat +lat_0=0 +lon_0=21 +k=0.9999 +x_0=7500000 +y_0=0 +ellps=bessel +towgs84=682,-203,480 +no_defs' \
        -t_srs EPSG:31277 mgi-025k-merc.shp mgi-025k.shp
ogr2ogr -s_srs '+proj=longlat +lat_0=0 +lon_0=21 +k=0.9999 +x_0=7500000 +y_0=0 +ellps=bessel +towgs84=682,-203,480 +no_defs' \
        -t_srs EPSG:31277 mgi-050k-merc.shp mgi-050k.shp
ogr2ogr -s_srs '+proj=longlat +lat_0=0 +lon_0=21 +k=0.9999 +x_0=7500000 +y_0=0 +ellps=bessel +towgs84=682,-203,480 +no_defs' \
        -t_srs EPSG:31277 mgi-100k-merc.shp mgi-100k.shp

zip -9 mgi-all-merc.zip mgi-025k-merc.??? mgi-050k-merc.??? mgi-100k-merc.???

# to wgs84
ogr2ogr -s_srs '+proj=longlat +ellps=bessel +towgs84=682,-203,480 +no_defs' -t_srs EPSG:4326 -dsco NameField=LABEL -f 'KML' \
         mgi-025k.kml mgi-025k.shp
ogr2ogr -s_srs '+proj=longlat +ellps=bessel +towgs84=682,-203,480 +no_defs' -t_srs EPSG:4326 -dsco NameField=LABEL -f 'KML' \
         mgi-050k.kml mgi-050k.shp
ogr2ogr -s_srs '+proj=longlat +ellps=bessel +towgs84=682,-203,480 +no_defs' -t_srs EPSG:4326 -dsco NameField=LABEL -f 'KML' \
         mgi-100k.kml mgi-100k.shp

# compress up
zip -9 mgi-100k.kmz mgi-100k.kml
zip -9 mgi-050k.kmz mgi-050k.kml
zip -9 mgi-025k.kmz mgi-025k.kml

zip -9 mgi-wgs84.zip mgi-100k.kmz mgi-050k.kmz mgi-025k.kmz

rm -rf *.kml