#!/usr/bin/perl
# PLSS point ids to addresses, for Salt Lake City UT
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.htm
# Created: 2024-03-06T00:00:58+0000
# Last-Updated: 2024-03-09T22:28:33+0000
#     Update #: 23
#

# Utah has the world's most advanced grid numbers to road name system(s)!
# So in fact one needn't bother adding labels to where the grid values are,
# as those values are the names of the roads themselves.

# But let's just make a simple chart anyway...

use strict;
use warnings q(all);
use PointId2Address;

my %grid = (
    origin => {
        id => 'UT260010N0010E0_100100', # corner of South Temple and Main Street
        ## address => [ (0) x 2 ]          # the address there, default, so commenting out
    },
    ## num_per_mile => [ (640) x 2 ]       # from City ordinance.

    ## This means only every five miles will a section line fall on a
    ## 100's edge! Also I find it doesn't hold rather well for some
    ## parts of the city, e.g., should be 666.67 just SE of downtown.
    ## This, plus the nonexistent relationship between the PLSS and
    ## the street grid in Salt Lake City leads me to no longer use
    ## this file for there.

    ## Wait,
    ## https://www.reddit.com/r/SaltLakeCity/comments/err7vp/comment/ff60lfc/ :
    ## The standard in Salt Lake is 8 blocks to a mile, but only if
    ## you are counting it by address numbers. When you take the
    ## width of a street into it, it's 7 blocks.

    num_per_mile => [ (700) x 2 ]    # OK maybe this is what they mean
);
die "I gave up on tieing SLC addresses to the PLSS.";
while (<>) {
    chomp;
    my @F = split /,/;
    $grid{target} =
      { id => $F[2] };               #we wipe out any previous {target}{miles}
    my @p = PointId2Address::id2addr( \%grid );
    die "No address for $_" unless @p;
    print join ",", @F[ 0, 1 ], "@p";
    print $/;
}
exit;
## What would be the PLSS point IDs of quarter section corners that
## hit a hundred block, on a diagonal, say to the SE?
for ( 0 .. 5 ) {
    for ( $_ * 800 ) {
        printf "%4d E %4d S %s$/", ($_) x 2,
          PontId2Address::addr2id( \%grid, $_, -$_ );
    }
}
