Mt. Wrangell,AK,62N,144W
Hico,TX,32N,99W
Neotsu,OR,45N,124W
Applegate,CA,39N,121W
Arbuckle,CA,39N,122W
Lakeport,CA,39N,123W
Hot Springs,CA,40N,121W
####
#!/usr/bin/perl -w
use strict;
my $data = "test.txt"; # here is the raw latitude/longitude data
my ($xlat, $xlon) = qw(39N 122W); # here's what we'll search for, +/- $variance
my (%lat_lon, @lat, @lon, @final_lat, @final_lon, %dups);
my $variance = 1; # change this to the degree variance desired
open (DATA, "<$data") || die "Can't open $data for reading: $!\n";
while () {
chomp;
my ($city, $state, $lat, $lon) = split /,/;
$lat_lon{$lat}{$lon}->[0] = $city;
$lat_lon{$lat}{$lon}->[1] = $state;
}
close (DATA) || die "Can't close $data: $!\n";
# find all lats which are +/- $variance of target lat
foreach my $lat_key (keys %lat_lon) {
my ($lat, $lat_NS, $xlat_NS);
$lat = $1, $lat_NS = $2 if $lat_key =~ /^(\d{1,3})([NS])$/o;
$xlat_NS = $2 if $xlat =~ /^(\d{1,3})([NS])$/o;
if ($xlat_NS eq $lat_NS) {
push (@lat, $lat_key) if ($1 <= $lat + $variance) && ($1 >= $lat - $variance);
}
}
# find all lons which are +/- $variance of target lon
foreach my $good_lat (@lat) {
foreach my $lon_key (keys %{$lat_lon{$good_lat}}) {
my ($lon, $lon_WE, $xlon_WE);
$lon = $1, $lon_WE = $2 if $lon_key =~ /^(\d{1,3})([WE])$/o;
$xlon_WE = $2 if $xlon =~ /^(\d{1,3})([WE])$/o;
if ($xlon_WE eq $lon_WE) {
push (@lon, $lon_key) if ($1 <= $lon + $variance) && ($1 >= $lon - $variance);
}
}
}
# remove duplicate latitudes and longitudes
foreach (@lat) {
push (@final_lat, $_) unless $dups{$_}++;
}
foreach (@lon) {
push (@final_lon, $_) unless $dups{$_}++;
}
####
"Some city, County", State, 45N, 120W