#!/usr/bin/perl
# Vermilion Co. Illinois addressing / road naming grids
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.html
# Created: 2024-05-11T16:29:32+0000
# Last-Updated: 2024-05-15T03:25:52+0000
#     Update #: 127
#
use warnings q!all!;
use strict;
use PointId2Address;
use Bou2colrow;
if ( @ARGV && $ARGV[0] eq "--blanket" ) {    #for offline testing
    my %s;
    for ( 1 .. 6 ) {    # South and west rows of a township.
        for ( Bou2colrow::cr2bUS( 1, $_, ), Bou2colrow::cr2bUS( $_, 1, ), ) {
            undef $s{$_};
        }
    }

    print "$_\n"
      for PointId2Address::twsp_blanket(
        {
            state    => "IL",
            meridian => "02",
            T        => [ 16 .. 23 ],
            R        => [ -14 .. -10 ],
            S        => [ keys %s ],
        }
      );
    exit;
}
## This county has an 'interesting' way of making the eastings of T21N
## fit those on the other side of the correction line, in T20N.

my @e;            #Eastings per mile on north edge of county (T21,22,23)
push @e, $_ * 100 + 70 for 0 .. 6;
push @e, 770, 880, 990, 1090, 1170;
push @e, $_ * 100
  for 13 .. 23;   #Yes, these last ones are 100 higher than their indexes * 100.

sub warp
{    # Given what we thought an address should be, make a locally warped address
    my $input     = $_[0];
    my $remainder = $input % 100;
    my $base      = $input - $remainder;
    my $low       = $base / 100;
    my $high      = $low + 1;
    return f($low) + $remainder * ( f($high) - f($low) ) / 100;

    sub f {    #double checks, then returns array value
        for ( $_[0] ) {
            if ( $_ < 0 || $_ > $#e ) {    #"double" check
                die "Gone beyond range of what we know (\$e[$_[0]])";
            }
        }
        return $e[ $_[0] ];
    }
}

my %grids = (
    1 => {
        origin => {
            id      => "IL020170N0140W0_400100",
            address => [ 0, 100, ],
        },
        num_per_mile => [ (100) x 2 ],
    }
);

## Regarding the grid collision along Collison(!) Road (2500N,
## T20/21N), if I was on this county's addressing board of directors,
## I would ... have only grudgingly allowed one disconformity... E.g.,
## north side 100 more than south side for same PLSS x coordinate. Not
## this hard to model blend they made.

## By the way, if only they had used a additional digit, then city and
## rural could share the same system.

my %strips = (
    1 => [
        [ [ 50, 250, ],  [ 50,   4150, ], ],    #W edge
        [ [ 50, 4250, ], [ 2050, 4250, ], ],    #N edge
        [ [ 2150, 150, ],  [ 2150, 2450, ], ],  #E edge(S)
        [ [ 2050, 2550, ], [ 2050, 3050, ], ],  #E edge(Middle)
        [ [ 2150, 3150, ], [ 2150, 4250, ], ],  #E edge(N):
        ## a wedge in fact. We cleverly give it priority in the N.E.
        ## corner of the county, as this strip would have no E value
        ## validity.
#
#        [ [ 50, 150, ], [ 1650, 150, ], ],    #S edge (W)
        ##S edge (risky bootheel. If success, then can join the 3):
#        [ [ 1750, 150, ], [ 1850, 150, ], ],
#        [ [ 1950, 150, ], [ 2050, 150, ], ],    #S edge (E)
    ]
);
my %o;
for (1) {
    my %h = PointId2Address::sec_center_strips( $grids{$_}, $strips{$_}, );
    for ( keys %h ) {
        next if $h{$_}{y} < 2500;
        next if $h{$_}{z};
        $h{$_}{label_2} = sprintf "%.03dE", warp( $h{$_}{x} );
    }
    @o{ keys %h } = values %h;
}
for ( sort keys %o ) {
    print join ",", $_, $o{$_}{label}, $o{$_}{label_2} || $o{$_}{label};
    print $/;
}

# https://clearinghouse.isgs.illinois.edu/data/reference/blm-illinois-public-land-survey-system
#    IL_CadNSDI_V2.gdb_.zip
# https://clearinghouse.isgs.illinois.edu/data/reference/illinois-public-land-survey-system-plss-boundaries
# https://gbp-blm-egis.hub.arcgis.com/datasets/ffdaac6dfea44c70b77dc0b315c706b6_3/explore?location=40.163508%2C-87.822024%2C9.11

# https://gbp-blm-egis.hub.arcgis.com/search?categories=%252Fcategories%252Fcadastral&groupIds=9d1a7138303b40dfbd4cf2c5ec733bde
