#!/usr/bin/perl
# PLSS point ids to addresses, for Orem UT
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.htm
# Created: 2024-03-25T03:56:33+0000
# Last-Updated: 2024-03-25T19:32:00+0000
#     Update #: 35
#
use strict;
use warnings q(all);
use PointId2Address;
use JSON;
my %grid = (
    location => {    #Just for our example today
        state   => "UT",
        county  => "Utah",
        city    => "Orem",
        country => "US"
    },
    origin => {    #Some place where we will anchor the address grid to the PLSS
        id      => 'UT260060S0020E0_500440',    #Standard PLSS corner ID of the
                                                #(virtual) corner of
                                                #Center & Main Sts. of Orem.
        address =>
          [ (0) x 2 ]    #Yes, that's the address at this (virtual) corner.
    },
    num_per_mile => [ (800) x 2 ] #Some towns might count faster along one axis.
);
if ( @ARGV && $ARGV[0] eq "--json" )
{    #Print a JSON version for demonstration purposes.
    my $json = JSON->new->pretty->canonical;
    print $json->encode( \%grid );
    exit;
}
while (<>) {
    next unless /\d/;    #Skip CSV headers
    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 $/;
}
