#!/usr/bin/perl
# PLSS point ids to addresses, for Northbrook IL

# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.htm
# Created: 2023-12-14T03:48:23+0000
# Last-Updated: 2024-04-26T01:29:05+0000
#     Update #: 47
#
use strict;
use warnings FATAL => q(all);
use PointId2Address;
my $num_per_mile = 800;
my %grid         = (
    origin =>
      {    # Oh, pick, Pfingsten & Walters, sort of in the middle of Northbrook
        id => "IL030420N0120E0_300520"
        ,    #I didn't bother finding the 0,0. I just picked this known point!
        address => [ -2800, -1400 ]
      },
    num_per_mile => [ ($num_per_mile) x 2 ]
);
my $func1;
if ( $ENV{diag} ) {
    $func1 = sub {
        return undef unless $_[0] == $_[2];
        return undef if $_[0] && $_[1] eq "E";
        return $_[0];
    };
}
else {
    $func1 = sub {
        return undef
          if $_[1] eq "E";    #We aren't interested in that half section.
        my ( $w, $s ) = @_[ 0, 2 ];
        return $w unless $s;
        return $s unless $w - 4400;
        return "@_";
    };
}

#only the SW quadrant is used, so we make the numbers positive
while (<>) {
    chomp;
    my @F = split /,/;
    $grid{target} = { id => $F[2] };   #we wipe out any previous {target}{miles}
    my $p = &$func1( PointId2Address::id2addr( \%grid ) );
    if ( defined $p ) {
        printf "%s,%s,%s\n", @F[ 0, 1 ], $p;
    }
}

=head3 Commentary

Gosh, I realize now that everything I do is constrained to sticking
on section lines... No e.g., x4xx4x possible. So we can't use the
Northbrook E=0 axis as if falls on a half section line. So we'll use
The township line at the west for an "axis".

=cut
