### Makefile ---
DS=\
-dsco NAME="嘉義縣六腳鄉 Liujiao Township, Chiayi County, Taiwan" \
-dsco DESCRIPTION="理想門牌 / 路名系統 Example ideal address / lane systems. \
見 See $(subst $(HOME)/,https://www.,$(PWD))"
## Author: Dan Jacobson https://www.jidanni.org/
## Copyright: https://www.gnu.org/licenses/gpl.html
## Created: 2024-07-06T03:49:33+0000
## Last-Updated: 2024-07-10T06:07:41+0000
##     Update #: 299
U = ../../../../utilities
b=~/Downloads/boundaries
I=liujiao_chiayi_tw
#Too many crooked patches. Let's use a straight TWD97 translation and scaling.
osmid=2861455# https://www.openstreetmap.org/relation/2861455
R=$b/$I.kml
J=$b/chiayi_county_tw.kml
st.V.vik:
$R:; $U/fetch_boundary $(osmid) $@
$J:; $U/fetch_boundary 2908173 $@ #https://www.openstreetmap.org/relation/2908173
mm:$J
st.%.xyz: Makefile
	set -- $(clip);\
	W=$$1 N=$$2 E=$$3 S=$$4; \
	$(Wedge) > $@
.SECONDARY:
MAKEFLAGS = --warn-undefined-variables
%.kml: %.csv
	ogr2ogr $@ $< -sql 'SELECT Name FROM "$(basename $<)"' -s_srs "$(prj)" \
	-t_srs 'EPSG:4326' -f LIBKML -clipsrc $(clip) -simplify 999999
numbers_per_grid=100
remote=n3
uu:xiang.extent.txt
Ox=168300
Oy=2596200
#origin:twd97.extent.txt; sed q $< #then tweak it to end in 00...
xiang.%: wgs84.%; xargs -n 2 < $< | proj $(prj)
#twd97.%: wgs84.%; xargs -n 2 < $< | proj EPSG:3826 > $@ #better round to 100 someday
wgs84.extent.txt:$R; ogrinfo -al $< | perl -nwle 'next unless /Extent/; s![^0-9.]! !g; print;' > $@
v=viking
%.vik:%.kmz
	cd /tmp && nohup ssh $(remote) \
	'if pidof -q $v; then killall --wait --signal SIGHUP $v; fi; \
	$v --map 13 -e $(PWD)/$< & sleep 11'
%.V.kmz: %.0.lines.kml %.1.lines.kml; ogrmerge -f LIBKML -overwrite_ds -o $@ $^
prj = +proj=tmerc +lon_0=121 +k=0.9999 +x_0=$$((250000-$(Ox))) +y_0=$$((0-$(Oy))) +to_meter=2
#xOx=100000
#xOy=2450000
#xianprj = +proj=tmerc +lon_0=121 +k=0.9999 +x_0=$$((250000-$(xOx))) +y_0=$$((0-$(xOy))) +to_meter=2
#lon lat road_orientation side name
places=\
120.292794 23.495614 南北 西 農會 祘新路\
120.292107 23.492706 南北 東 郵局 南宮路\
120.294089 23.492862 東西 南 戶政所 祘興路\
120.290748 23.492931 東西 北 消防局 祘興路\
120.264897 23.511512 東西 北 rural+ unknown
# From our 縣 model, left side is even as we march forward along the axis.
# So N and W sides are even
examples:
	@perl -wle 'print "<TR>", join "<TH>", "", qw!X Y 路向 側 單位 路 址</TR>!;'
	@echo $(places) | xargs -n 6 | proj $(prj) |\
	perl -anwle '$$n=int $$F[0+($$F[2] eq "南北")]; if($$F[3]=~/北|西/){if($$n % 2){$$n++}}'\
	'elsif(!($$n%2)){$$n++}; print "<TR>", join "<TD>","", @F, "$$n 號</TR>";'
st=4380 900 5100 1800
zoom=4550 1200 4910 1530
xiang=35.43 31.02 6451.44 5444
clip=$(xiang)
SHELL=/bin/bash
#.SHELLFLAGS=-cx
%.lines.csv:%.xyz # Drape contours over the wedge. Our own version of "gdal_create -outsize 2 2":
	gdal_contour -i $(numbers_per_grid) -q -a Name $< $@ -lco GEOMETRY=AS_WKT
Wedge = perl -wle "for([$$W,$$N],[$$E,$$N],[$$W,$$S],[$$E,$$S]){print qq(@\$$_ @\$$_[$*])}"
clean:; for i in txt xyz csv kml; do find -maxdepth 1 -name \*.$$i -delete; done
#kclean: clean; for i in kmz; do find -maxdepth 1 ! -name xinshe.kmz -name \*.$$i -delete; done