### Makefile ---
N=Salt Lake City, Utah address / road name grid
DESC1=Simple example.#NOT using PLSS data.
## Author: Dan Jacobson https://www.jidanni.org/
## Copyright: https://www.gnu.org/licenses/gpl.html
## Created: 2024-03-05T13:22:27+0000
## Last-Updated: 2024-05-04T03:13:49+0000
##     Update #: 234

U = ../../../../utilities
export PERLLIB = $U
export PATH := $U:$(PATH)
include $U/m1.makefile
SHELL = /bin/bash
W=salt_lake_city_ut
$W.vik:
C= > $@.t && test -s $@.t && mv $@.t $@
# We decide to totally chuck the PLSS, which usually is our ace in the
# hole, but here in Salt Lake City turns out to be rather unrelated to
# the street pattern.

# So we'll just use a few Ground Control Points
GCPs=\
-gcp 0   -500 -111.89114803 40.75846686 \
-gcp 0   0    -111.89114717 40.76932606 \
-gcp 500 -500 -111.87685794 40.758524   \
-gcp 500 0    -111.87685039 40.76940631
GG: #or even less!
	echo 300 -370 | gdaltransform \
-gcp 0   -500 -111.89114803 40.75846686 \
-gcp 0   0    -111.89114717 40.76932606 \
-gcp 500 0    -111.87685039 40.76940631

example_for_gdaltransform_page:
	c="gdaltransform -output_xy \
-gcp 0   -500 -111.89114803 40.75846686 \
-gcp 0   0    -111.89114717 40.76932606 \
-gcp 500 0    -111.87685039 40.76940631";\
echo 300 -370 370 S. 300 E.  | $$c;\
echo -111.88705 40.76502 New building| $$c -i;\
echo i.e., 143 E 200 S;\
echo 0 0 Great Salt Lake Base and Meridian monument | $$c

T=/tmp/d

# A simple diagonal to the venue of
# https://wiki.openstreetmap.org/wiki/Utah/Naming_Conventions/Activity
$W.csv:
	perl -wle 'for(0..4){for($$_*100){print $$_, " ", -$$_;}}' > $T
	< $T gdaltransform $(GCPs) | paste - $T|perl -anwle \
	'print join ",", @F[0,1,3];' $C

# Older PLSS-based approaches that we gave up on:
# In fact on USGS maps the PLSS section lines are in *dashed* red lines too.
D=~/Downloads
B=UtahPLSSGCDBPoints
Q=UtahPLSSQuarterQuarterSections
ln_one_time: # Must do to make ogr2ogr read it. Also no symlinks on 9p file system.
	cd ~/Downloads && \
	set -xeu; for i in Utah*zip; do ln $$i $${i/zip/gdb.zip}; done
/tmp/$B.geojson:$D/$B*geojson; ln -s $< $@
old$W.csv:$W1.csv #gosh, I find I must tweak. The $Q stuff above was more accurate.
#	ogr2ogr -ct '+proj=geogoffset +dlon=0.28 +dlat=0.86' I cannot get working, so...
	perl -F, -anwle '$$F[0]+=-.0007; $$F[1]+=-.0001; $(\
	)for($$F[2]){@m=split; for(@m){$$_=sprintf "%.0f", $$_*500/480 if /\d/;}$$_="@m";} $(\
	)next unless $$F[2]=~/0\b/; print join ",", @F;' $< $C
$W1.csv:$W0.csv pid2addr; ./pid2addr $< $C
# https://gis.utah.gov/data/cadastre/plss/
# https://opendata.gis.utah.gov/datasets/utah-plss-point-gcdb/explore
# https://opendata.gis.utah.gov/datasets/utah-plss-quarter-quarter-sections-gcdb/explore
$W0.csv:/tmp/$B.geojson # Makefile
	ogr2ogr \
        -sql "SELECT XCOORD,YCOORD,POINTID FROM $B WHERE (\
	POINTID LIKE 'UT260010N0010E0_1__100' OR \
	POINTID LIKE 'UT260010S0010E0_1__6__' )" /tmp/g.csv $<
	cat /tmp/g.csv | sed 1d | tr -d \" $C
# https://codelibrary.amlegal.com/codes/saltlakecityut
# 14.08.020: BUILDING NUMBERING SYSTEM:
# ...in accordance with the Salt Lake County addressing system, allowing one number every 8.25 feet.
# The initial point shall be the junction of Main Street and South Temple...

# https://library.municode.com/ut/salt_lake_county
# 2.49.090 - ... The initial point of intersection in the countywide grid
# system shall be the junction of Main Street (meridian street) and South
# Temple Street (baseline street axis) in Salt Lake City,...

m:; units 8.25ft mi #640 numbers per mile, yes, 320 on each side of a street.
#(But see ./pid2addr where we learn it is 700!)
m00: # So where will 100's match a section line?
	for i in `seq 0 15`; do echo -n $$i:; units --terse $${i}mi 8.25ft; done|\
	perl -F: -apwle 's/$$/**/ unless $$F[1] % 100;'
m4: # So where will 100's match a 1/4 section line?
	perl -we 'for(0..32){printf "%d %d/4 %s", int ($$_/4), $$_%4, $$p= $$_ * 640 / 4;$(\
	); print "**" unless $$p % 100; print $$/;}'
clean:; rm *.csv
