# Makefile --- Lake Co. IL
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.html
# Created: 2023-11-22T14:00:05+0000
# Last-Updated: 2024-01-01T01:50:28+0000
#     Update #: 260

# Now using PLSS as basis.
U = ../../../../utilities
export PERLLIB = $U
export PATH := $U:$(PATH)
B=~/Downloads/PLSSFirstDivision.geojsonl
D=~/IL_CadNSDI_V2.gdb.zip # Yes, we found a wonderful file full of sections
T=lake_co_il
G=egrep
#export intervals=10 #uncomment to make a proof of concept, but too dense, 1/10 of mile grid
$T.vik:
#Two steps:
S=PLSS_corners_4
$T0.csv:$B $U/$S.pm Makefile
	$G 'IL030430N0(09|10|11|12)0E0SN3' $< |\
	sides=1 perl -M$S -we '$S::parse_seq;' |\
	$G 1..$$ > $@.t
	$G 'IL0304[3456]0N0090E0SN(33|28|21|16|09|04)' $< |\
	sides=2 perl -M$S -we '$S::parse_seq;' |\
	$G _3 >> $@.t
	mv $@.t $@
$T.csv:$T0.csv pid2addr
	./pid2addr $< |sort -u -o $@
#Wow, one day use REGEXPs to dig just what we need:
wonder_ls:$D; ogrinfo -ro -al -features -dialect SQLITE -sql $(\
	)'SELECT * FROM PLSSFirstDivision WHERE FRSTDIVID REGEXP "IL030430N0100E0" LIMIT 1' $?
#I found 25636 of the Illinois sections had four (unique) nodes on their rings...
#7 3, 25636 4, 12315 5, 5568 6, 3488 7, 3425 8, 1820 9,
%.analy:%.nifo; sed 's/.*,//;' $?|sort -n|uniq -c |sort -k 2n
#How about for Lake Co? I found 320 4, 36 5, 12 6, 2 7, 1 8, 3 9...
pat=IL0304[3-6]0N01[0-2]0E0
%.analy_lc:%.nifo; grep $(pat)$?|sed 's/.*,//;'|sort -n|uniq -c |sort -k 2n
#And I bet they are the same data... YUP!
%.kmz:%.GeoJSON; ogr2ogr -f LIBKML $@ -progress $?
n:d.json
	Nodeinfo $?
F=IL030430N0100E0SN360
IL030450N0090E0SN330.v:
IL030460N0090E0SN040.v:
%.v:d.json
	filter=$* OnePLSSSec $? > $*.csv
	make $*.vik
o.vik:
o.csv:d.json
	filter=$F OnePLSSSec $? > $@
address_test:
	debug=1 ./make_wants|perl -MBou2colrow -nwle \
	'print join ",", 22, 333, Bou2colrow::trsec_dir2id($$_, "SW");'|\
	./pid2addr|sort -t , -k 3n
Oldlake_co_il.csv:pid2addr cor.csv; ./$^ > $@
KO = 	-f LIBKML \
	-dsco NAME="Lake Co. IL Address Grid" \
	-dsco DESCRIPTION="Using BLM PLSS data. See $(subst $(HOME)/,https://www.,$(PWD))"
CO=	-oo HEADERS=NO \
	-oo KEEP_GEOM_COLUMNS=NO \
	-oo X_POSSIBLE_NAMES=field_1 \
	-oo Y_POSSIBLE_NAMES=field_2 \
	-sql 'SELECT field_3 AS Name FROM "$(basename $<)"'
%.kmz %.kml:%.csv; ogr2ogr $@ $? $(KO) $(CO)
%.vik:%.kmz; cd /tmp && nohup viking -e $(PWD)/$? & sleep 11
p=PLSS_corners
cor.csv:d.json $U/$p.pm
#Exclude "troublemaker" stunted section at Wisconsin border
	avoid=IL030460N0090E0SN040 perl -M$p -we '$p::process()' $< > $@.t
#WEST\|SOUTH edges of county:
	egrep IL03...0N0090E0_300.00\|IL030430N...0E0_.00100 $@.t |\
	grep -v IL030430N0090E0_[12]00100  > $@ #snip McHenry Co. partial township
	rm $@.t
d.json:; ./make_wants|fetch_blm_sections > $@.t && mv $@.t $@
$B:$D #And learned we didn't need their centers...
	ogr2ogr -f GeoJSONSeq $@ -progress -sql "SELECT FRSTDIVID FROM PLSSFirstDivision" $?
.PRECIOUS: %.kmz
