.PHONY: all clean

GENERATED_FILES = \
	ch.json

all: ch.json

clean:
	rm -rf -- $(GENERATED_FILES) build

build/VEC200_Commune.%:
	mkdir -p build
	curl -o $@ -3 'https://raw.githubusercontent.com/interactivethings/swiss-maps/2.1.0/src/V200/$(notdir $@)'

build/cantons.shp: $(addprefix build/VEC200_Commune.,shp dbf prj shx)
	rm -f -- $@
	ogr2ogr -f 'ESRI Shapefile' -where 'COUNTRY = "CH"' $@ $<

build/lakes.shp: build/cantons.shp
	rm -f -- $@
	ogr2ogr -f 'ESRI Shapefile' -where 'SEENR < 9999 AND SEENR > 0' $@ $<

build/cantons-unmerged.json: build/cantons.shp
	node_modules/.bin/topojson \
		-o $@ \
		--no-quantization \
		--id-property=+KANTONSNR \
		-- cantons=$<

build/lakes-unmerged.json: build/lakes.shp
	node_modules/.bin/topojson \
		-o $@ \
		--no-quantization \
		--id-property=+SEENR \
		-- lakes=$<

build/%.json: build/%-unmerged.json
	node_modules/.bin/topojson-merge \
		-o $@ \
		--in-object=$* \
		--out-object=$* \
		-- $<

ch.json: build/lakes.json build/cantons.json
	node_modules/.bin/topojson \
		-o $@ \
		--width=960 \
		--height=500 \
		--margin=10 \
		--simplify=.5 \
		-- $^
