### Makefile Otero Co. CO, USA
## Author: Dan Jacobson https://www.jidanni.org/
## Copyright: https://www.gnu.org/licenses/gpl.html
## Created: 2023-09-24T10:02:56+0000
## Last-Updated: 2023-12-30T23:43:49+0000
##     Update #: 72
## We were going to put grids on the S and W edges of the county,
## as those are the axes. However, the E edge is longer...
U=../../../../utilities
export PATH := $U:$(PATH)
export PERLLIB = $U
KO = 	-f LIBKML \
	-dsco NAME="Otero County Colorado address and road name grid" \
	-dsco DESCRIPTION="Using BLM PLSS data. See $(\
	)https://www.jidanni.org/geo/house_numbering/grids/us/co/otero/otero_county"
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 $<)"'

# I wanted to put each axis' points into a different KMZ sub folder or
# sub document. Two axes, two folders. But with LIBKML I ended up
# doing handstands with directories of csv files to produce layers and
# in the end I got something that even viking couldn't read. So never
# mind. Anyway, our goal isn't to make pretty KMZ files, but to simply
# show on peoples screens nice spots where grid ticks might go.
# Therefore, fine, I will stick with flat KMZ files with no sub
# folders or sub documents.

o.vik:
o.csv:road_names n.csv; ./$^ > $@
s=sqlite3 $<
C= ~/Downloads/BLM_CO_PLSS_Points_Corners.csv # Wow, 16 points per square mile!
# I wanted to order them by POINTID, but for TxxS the front is in a different order than the _xxxxxx of course.
# Also my csv import becomes type TEXT, so I use "+ 0".
n.csv:c.sqlite
	perl -wle 'my %h=(X=>"CO060270S05[4-9]0W0_[1-6]00100", Y=>"CO0602[1-7]0S0530W0_100[1-6]00"); $(\
	)for(sort keys %h){print "SELECT XCOORD,YCOORD,POINTID FROM tab1 WHERE POINTID REGEXP $(\
	)\"$$h{$$_}\" ORDER BY $${_}COORD + 0;"}'| $s -csv > $@
c.sqlite:$C #76 Megabytes
	echo .import --csv $< tab1 | sqlite3 $@
%.kmz %.kml:%.csv; ogr2ogr $@ $? $(KO) $(CO)
%.vik:%.kmz
	cd /tmp && nohup viking -e $(PWD)/$? &
	sleep 11
.PRECIOUS: %.kmz
