### Makefile ---
DS =\
-dsco NAME="台中市新社區 Xinshe District, Taichung City, 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-04-13T08:05:54+0000
## Last-Updated: 2024-10-20T20:16:25+0000
##     Update #: 1265
export WeSnLimit = 2500
# Lanes less than this are N-S, else E-W.
# A Utah style addressing grid. But we move the origin off the map, so
# no need for N., S., E., W. Also we make full use of the [9999]
# numbers we plan to use, where you can determine the direction of the
# road (here we call them lanes) from its number... But not odd roads
# N/S, instead lower numbered roads N/S, with fold a $(WeSnLimit). On
# Openstreetmap for this area we also see the, alas, inferior current
# local house numbers scattered around.
.SECONDARY:
.SECONDEXPANSION:
prereq=/usr/bin/gdal_create #check if this machine has gdal installed
$(prereq):
	@echo 1>&2 Dude, THIS machine doesn\'t have THAT gdal stuff installed.
	@echo 1>&2 Am I making sense?
	@exit 59
MAKEFLAGS = --warn-undefined-variables
H = houses
xs.vik:
%.kml:%.csv; $(FA) $(F0) -f LIBKML #to make much slimmer KMLs then ogrmerge *.csv would allow
xs.kmz: a1.V.kmz zxl.V.kmz; ogrmerge -overwrite_ds -o $@ $^ $(DS)
numbers_per_grid = 100
remote = n3
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"

zzz = 1
houses.V.vik:
$H.V.kmz: $H.0.lines.kml $H.1.lines.kml a1.$H.kml
	ogrmerge -f LIBKML -overwrite_ds -o $@ $^  #Yes, get warnings as of 2024.
%.V.kmz: %.0.lines.kml %.1.lines.kml
	ogrmerge -f LIBKML -overwrite_ds -o $@ $^  #Yes, get warnings as of 2024.
FA = ogr2ogr $@ $<
F0 = -sql 'SELECT Name FROM "$(basename $<)"'
F1 = --optfile $(wordlist 2,2,$^) -lco GEOMETRY=AS_WKT -order 1
a1_ring  = POLYGON (($(Ba1main),$(Bshijun),$(Bxinwucun),$(wordlist 1,2,$(Ba1main))))
zxl_ring = POLYGON (($(Bcliff),$(Bshijun),$(Bxinwucun),$(wordlist 1,2,$(Bcliff))))
#buf_siz=-.001
buf_siz = -.000001
buf = SELECT ST_Buffer(ST_GeomFromText('$(1)'),$(buf_siz),1) AS WKT, 1 AS NAME
houses.buf.csv:a1.houses.csv Makefile
	ogr2ogr $@ $< -lco GEOMETRY=AS_WKT -dialect SQLITE -sql \
	"SELECT ST_Buffer(Extent(GEOMETRY),.002) AS WKT, 1 AS Name FROM 'a1.houses'"
%.buf.csv:; ogr2ogr $@ :memory: -lco GEOMETRY=AS_WKT -sql "$(call buf,$($*_ring))"
%.lines.csv:%.Q.csv $$(basename $$*).gcp $$(basename $$*).buf.csv
	$(FA) $(F0) $(F1) -clipdst $(basename $*).buf.csv
%.Q.csv:%.A.csv; perl -pwle 'if ($(zzz)){}else{s/$$/巷/ unless /WKT/}' $< > $@
%.A.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(@\$$_ @\$$_[$*])}"
a1.%.xyz: $(prereq); N=8500 E=2200 S=3400 W=100; $(Wedge) > $@
zxl.%.xyz:$(prereq); N=7777 E=2500 S=2500 W=500; $(Wedge) > $@
$H.%.xyz: $(prereq); N=6300 E=1200 S=5800 W=900; $(Wedge) > $@
G = xargs --max-args=4 echo -gcp  > $@
HGCP =	1000 6000 120.803215 24.212148 1100 6100 120.805087 24.213155 \
	1200 6200 120.806933 24.214168 1000 6200 120.803247 24.214207 \
	1200 5900 120.806917 24.211076
$H.gcp:; echo $(HGCP) | $G
a1.gcp:; echo \
	$(HGCP) 1000 5700 120.803166 24.209020 \
	1000 7800 120.803445 24.230673 300 6000 120.790356 24.212259 \
	1600 6000 120.814270 24.212073 1700 3800 120.815894 24.189259 \
	700 6700 120.797792 24.219434 400 6600 120.792225 24.218430 \
	1000 6000 120.803198 24.212141 1000 5700 120.803166 24.209020 \
	1000 7800 120.803445 24.230673 300 6000 120.790356 24.212259 \
	1600 6000 120.814270 24.212073 1700 3800 120.815894 24.189259 \
	700 6700 120.797792 24.219434 400 6600 120.792225 24.218430 | $G
zxl.gcp:; echo 1900 3100 120.801976 24.206769  2200 3400 120.795603 24.207983 1300 3400 120.807612 24.197629 | $G
a1.$H.csv: a1.$H.raw.csv $H.gcp;       $(FA) $(F0) $(F1)
%.csv:%.raw.csv a1.gcp
	ogr2ogr $@ $< -sql 'SELECT Name FROM "$(basename $<)"' --optfile a1.gcp -lco GEOMETRY=AS_WKT -order 1
a1.$H.raw.csv: addr2xy
	echo 6000 巷1145號 6100 巷 1042 號 1100巷 6158號 1100巷6051號 |\
	perl -pwle 's/號/$$&\n/g'|./$< | perl -pwle 's/\d+$$// if $(zzz)' > $@
wit = zxl.buf.kml a1.buf.kml a1.0.lines.kml a1.1.lines.kml zxl.1.lines.kml \
	zxl.0.lines.kml 00.kml 6111.kml big_road.kml #picbox.kml
xs2:kclean; make numbers_per_grid=500 $(wit)
vvr:; ssh $(remote) make -C $(PWD) vv
#vv:; viking $(wit) #order of $(wit) very important, else labels overwritten, etc.
vv:; viking 00.kml 6111.kml big_road.kml #picbox.kml
A = 0
L = $(WeSnLimit)
C = 10000
xl=980
xh=1220
yl=5970
yh=6230
picbox.raw.csv:
	echo WKT,Name > $@
	echo >> $@ '"POLYGON (($(xl) $(yl), $(xh) $(yl), $(xh) $(yh), $(xl) $(yh), $(xl) $(yl)))",.'
big_road.raw.csv:
	echo WKT,Name '"POINT ($A $L)",$A' '"POINT ($L $L)",$L' '"POINT ($L $C)",$C'|xargs -n 1 > $@
	echo >> $@ '"LINESTRING ($A $L,$L $L)",$L' #use two segments, to force label to stay on the line
	echo >> $@ '"LINESTRING ($L $L,$L $C)",$L'
Ax = -1000
Cx = 12000
Lx = 3500
00.raw.csv:
#	echo WKT,Name '"POINT (0 0)",R'|xargs -n 1 > $@
	echo WKT,Name > $@
	echo >> $@ '"LINESTRING ($(Ax) 0, $(Lx) 0)",0'
	echo >> $@ '"LINESTRING (0 $(Ax), 0 $(Cx))",0'
6111.raw.csv:
	echo WKT,Name > $@
	echo >> $@ '"LINESTRING ($(A) 6100, $(L) 6100)",6100'
	echo >> $@ '"LINESTRING (1100 $(Lx), 1100 $(Cx))",1100'
a2.gcp:; echo  2000 3300 120.821440 24.184178  2000 2700 120.820797 24.178061 \
	1900 3000 120.819338 24.181286 | $G
a3.gcp:; echo 1300 7900 120.809028 24.231993  1300 7700 120.809543 24.229997 \
	1400 7600 120.811968 24.229518 | $G
xinsancun.gcp:; echo 1250 7100 120.807112 24.223407  1250 6900 120.807720 24.221202 \
	1200 6900 120.807227 24.221043 | $G
xiaping.gcp:; echo 2100 3800 120.823771 24.189371 \
	2200 3900 120.825617 24.190453  2100 3600 120.823890 24.187311 \
	2100 4000 120.823771 24.191336 | $G
Bshijun = 120.79417 24.20592 ,120.79434 24.20745 ,120.79477 24.20882 ,120.79545 24.20952 ,120.79584 24.21081 ,\
	120.79575 24.21210 ,120.79691 24.21191 ,120.79957 24.21484 ,120.80052 24.21535 ,120.80107 24.21433 ,\
	120.80060 24.21332 ,120.80099 24.21222 ,120.80129 24.21132 ,120.80163 24.21124 ,120.80206 24.21140 ,\
	120.80219 24.21073 ,120.80202 24.20979 ,120.80185 24.20889 ,120.80309 24.20823 ,120.80507 24.20764 ,\
	120.80601 24.20666 ,120.80657 24.20478
Bxinwucun = 120.80979 24.20056 ,120.81073 24.19821 ,120.81180 24.19582 ,120.81365 24.19468 ,120.81519 24.19339 ,\
	120.81519 24.18995 ,120.81528 24.18619
Bcliff = 120.81313 24.18337 ,120.81103 24.18760 ,\
	120.80760 24.19061 ,120.80477 24.19402 ,120.79996 24.19832 ,120.79696 24.20251 ,120.79618 24.20369 ,\
	120.79387 24.20541 ,120.79412 24.20604
Ba1main = 120.81502 24.18415 , 120.81571 24.18451 , 120.81678 24.18779 , 120.81833 24.19053 ,\
	120.82103 24.19281 , 120.82369 24.19465 , 120.82305 24.19786 , 120.82176 24.20110 ,\
	120.81970 24.20290 , 120.81743 24.20651 , 120.81567 24.21148 , 120.81352 24.21492 ,\
	120.81236 24.21664 , 120.81000 24.21797 , 120.80871 24.21852 , 120.80687 24.22138 ,\
	120.80695 24.22349 , 120.80678 24.22396 , 120.80511 24.22682 , 120.80507 24.22842 ,\
	120.80378 24.23312 , 120.80253 24.23641 , 120.80090 24.23695 , 120.80077 24.23527 ,\
	120.79970 24.23292 , 120.79850 24.23061 , 120.79571 24.22701 , 120.79314 24.22447 ,\
	120.79103 24.22095 , 120.78979 24.22009 , 120.78653 24.21637 , 120.78644 24.20870 ,\
	120.78833 24.20748 , 120.79009 24.20682 , 120.79253 24.20604 , 120.79382 24.20545
clean:; for i in xyz txt csv gcp kml; do find -maxdepth 1 -name \*.$$i -delete; done
#xinshe.kmz is special.
kclean: clean; for i in kmz; do find -maxdepth 1 ! -name xinshe.kmz -name \*.$$i -delete; done
zxl.convert:Makefile
	perl -nwle 'next unless /zxl/; print;'\
	's!(?<=[\s=])[0-9]+(?=\s)!$$&<$(WeSnLimit)?2900-$$&:9000-$$&!ge; print;' $<