#!/usr/bin/perl
# sdrn --- South Dakota road names
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.html
# Created: 2023-12-27T12:20:57+0000
# Last-Updated: 2023-12-30T05:13:50+0000
#     Update #: 170
#
use warnings q!all!;
use strict;
use PointId2Address;
use Data::Dump q!pp!;

=head1 Computations

Gasp. We only dare try to put grid ticks on the two "100" St. and Ave. axes of South Dakota.

=cut

use Data::Dumper;
my %o;
for (
    {    #Black Hills Meridian governed part:
        address => [ 100, -193 ],
        id      => "SD070070N0010E0_100700",    #From Lawrence Co. map
    },
    {    #5th Principal Meridian governed part:
        address => [ 286, -100 ],   # Campbell Co. map's first AVE. extrapolated
        id      => "SD051280N0800W0_700700",
    },
    ## We're only doing the N and W borders of the state.
) {
    $o{ substr $_->{id}, 0, 4 } = {
        num_per_mile => [ 1, 1 ],
        origin       => $_
    };
}
##Alas, due to northward curvature of the earth, certain avenues have been removed from the sequence.
##so we must make this hand made lookup table. But they even removed some on the Nebraska border...
# https://dot.sd.gov/inside-sddot/forms-publications/maps/cadd-mapping county maps

# Wow this turned out to be super complicated!

my %twsp2ave =
  (    #yes, if an AVE. is on a border, I assign it to the county to the east.
    SD05 => [    # 5th Principal Meridian
        286 .. 297, 299 .. 323,                # Campbell Co.
        324 .. 331, 333 .. 343, 345 .. 373,    # McPherson Co.
        375 .. 379, 382 .. 412,                # Brown Co.
        413 .. 436,                            # Marshall Co.
        (undef) x 6,                           # For the missing R55W
        437 .. 438, 440 .. 449,                # Marshall Co. Sisseton  Resv.
        450 .. 469,    # Roberts Co. Sisseton Resv.
        468 .. 480     # Roberts Co. Upon recovery from Sisseton we lurch back.
              # Things look smooth on the ground. It is our townships that have
              # lurched back, so we must compensate.

    ],
    SD07 => [    # Black Hills Meridian
        100 .. 153, 155,    # Harding Co., 100 would actually be in Montana
        156 .. 200,   # Perkins Co., yes then a 203 on the 1/2 mile county line.
        206 .. 218, 220 .. 243, 245 .. 289    # Corson Co.
    ],
    _304th_st => [   # And just out of curiosity, let's see how many avenues get
         # skipped along 304th/305th St. (Nebraska border, at least in the west.)
        100 .. 153
        , # Fall River Co., Black Hills Meridian, 154 is on the half mile/county line.
        ("Named") x 2,
        155 .. 199,    # Oglala Lakota Co., now 6th Principal Meridian
        200 .. 202, 205 .. 244,    # Bennett Co., 6th Principal Meridian
        245 .. 296, 298,           # Todd Co., 6th Principal Meridian.
                                   # OK, now no more oddities:
        299 .. 332,    # Tripp Co., now 5th Principal Meridian
        333 .. 384,    # Gregory Co., 5th Principal Meridian...
        385 .. 404,    # Charles Mix Co. Now 303rd St. apparently takes
                       # the place of 304th's latitude and range line, etc. Odd.
        405 .. 428,    # Bon Homme Co.
        429 .. 452,    # Yankton Co.
        453 .. 470,    # Clay Co.
        471 .. 484,    # Union Co.
    ]
  );

while (<>) {
    chomp;
    my @F     = split /,/;
    my $st_pm = substr $F[2], 0, 4;
    $o{$st_pm}{target} =
      { id => $F[2] };    #we wipe out the previous {target}{miles}
    my @a = PointId2Address::id2addr( $o{$st_pm} );
    my $n;
    if ( $F[2] =~ /SD0512[89]0N|SD070230N/ ) {    #North border
        $st_pm = substr $F[2], 0, 4;
        $n =
          $twsp2ave{$st_pm}->[ $a[0] - $twsp2ave{$st_pm}->[0] ];   #Fancy lookup
    }
    else {                                                         #West border
        $n = $a[2];
    }
    die "@F bad" unless length $n;
    print join ",", @F[ 0, 1 ], $n;
    print "\n";
}
