#!/bin/bash
#
# Download and prepare sea (North Sea/Noordzee) polygons. Need GDAL installed.
#
# NB these files are already available as downloads on https://data.nlextract.nl/osm/nl/ .

/bin/rm -rf *-water-polygons-split-3857* *.sql.gz > /dev/null 2>&1

CLIP_NL="-20000 275000 300000 650000"
CLIP_NCP="-285401.920 22598.080 595401.920 903401.920"

# Get the data from OSM
wget https://osmdata.openstreetmap.de/download/simplified-water-polygons-split-3857.zip
unzip simplified-water-polygons-split-3857.zip
wget https://osmdata.openstreetmap.de/download/water-polygons-split-3857.zip
unzip water-polygons-split-3857.zip

# NL Area
export OGR_OPTS="--config PG_USE_COPY YES -lco GEOMETRY_NAME=geom -nlt PROMOTE_TO_MULTI -overwrite -s_srs EPSG:3857 -t_srs EPSG:28992 -clipdst ${CLIP_NL}"

# 1. simplified-water-polygons
# NB  -where "x > 0" is to rule out the extent polygon newly created as by-product from clipping !
export DUMP_FILE="sea-polygons-simplified-28992-nl.sql"
echo "Creating ${DUMP_FILE}"
ogr2ogr -f PGDump ${DUMP_FILE} simplified-water-polygons-split-3857 ${OGR_OPTS}  -where "x > 0"
gzip ${DUMP_FILE}

# 2. water-polygons
export DUMP_FILE="sea-polygons-28992-nl.sql"
echo "Creating ${DUMP_FILE}"
ogr2ogr -f PGDump ${DUMP_FILE} water-polygons-split-3857 ${OGR_OPTS} -where "x > 0"
gzip ${DUMP_FILE}

# NCP (Nederlands Continentaal Plat) Area
# See https://www.geonovum.nl/uploads/standards/downloads/nederlandse_richtlijn_tiling_-_versie_1.1.pdf
#
export OGR_OPTS="--config PG_USE_COPY YES -lco GEOMETRY_NAME=geom  -nlt PROMOTE_TO_MULTI -overwrite -s_srs EPSG:3857 -t_srs EPSG:28992 -clipdst ${CLIP_NCP}"

# 1. simplified-water-polygons
# NB  -where "x > 0" is to rule out the extent polygon newly created as by-product from clipping !
export DUMP_FILE="sea-polygons-simplified-28992-ncp.sql"
echo "Creating ${DUMP_FILE}"
ogr2ogr -f PGDump ${DUMP_FILE} simplified-water-polygons-split-3857 ${OGR_OPTS}  -where "x > 0"
gzip ${DUMP_FILE}

# 2. water-polygons
export DUMP_FILE="sea-polygons-28992-ncp.sql"
echo "Creating ${DUMP_FILE}"
ogr2ogr -f PGDump ${DUMP_FILE} water-polygons-split-3857 ${OGR_OPTS} -where "x > 0"
gzip ${DUMP_FILE}

/bin/rm -rf *water-polygons-split-3857*
