#!/bin/bash

rm -rvf japan
mkdir -p build japan

if [ ! -f build/polbnda_jpn.shp ]; then
  curl -o build/gm-jpn-all_u_2_2.zip 'http://www1.gsi.go.jp/geowww/globalmap-gsi/download/data/gm-japan/gm-jpn-all_u_2_2'
  unzip -od build -j build/gm-jpn-all_u_2_2.zip gm-jpn-all_u_2_2/polbnda_jpn.shp gm-jpn-all_u_2_2/polbnda_jpn.dbf
  chmod a-x build/polbnda_jpn.*
fi

geo2topo -q 1e5 -n municipalities=<( \
  shp2json -n build/polbnda_jpn.shp \
    | ndjson-map '(d.id = d.properties.adm_code, delete d.properties, d)' \
    | ndjson-filter '(d.id != "UNK")' \
    | geoproject -n 'd3.geoAzimuthalEqualArea().center([138.5, 36 - 18 / 60]).rotate([-138.5, -36 + 18 / 60]).scale([1700]).translate([2500, -1930])') \
  | toposimplify -f -p 0.001 \
  | topomerge prefectures=municipalities -k 'd.id.slice(0, 2)' \
  | topomerge country=prefectures \
  > japan/japan.json

