Skip to content

Instantly share code, notes, and snippets.

@wolfjohns
Created May 12, 2022 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfjohns/b327af9700f7b0ad1faa23ed4dad3768 to your computer and use it in GitHub Desktop.
Save wolfjohns/b327af9700f7b0ad1faa23ed4dad3768 to your computer and use it in GitHub Desktop.
lib/Airport/Search.pm
package Airport::Search;
use strict;
use warnings;
use Data::Dump 'pp';
use feature 'say';
use List::Util qw(min max);
my $airport_data = Airport::Data::parse_airports("t/data/airports1.csv");
sub get_name_matching_airports {
#my $fields = parse_airports($filename);
#my ($match, $str) = @_;
my @match;
my %args = (
airports => undef,
#matching => undef, # ANDREW FIX B
matching_string => undef,
word => undef,
@_
);
for my $x ( @{ $args{"airports"} }) {
#for my $x (%args{"airports"}) {
#if ($x->{name} =~ /$args{"matching"}/i) { # ANDREW FIX C
# if (defined $args{"word"}) { # ANDREW FIX D
if ($args{"word"}) {
if ($x->{name} =~ /\b$args{"matching_string"}\b/i) {
#push $rah_airports_found, $x;
push @match, $x;
}
} else {
if ($x->{name} =~ /$args{"matching_string"}/i) {
#push $rah_airports_found, $x;
push @match, $x;
}
}
}
#pp %args{airports};
#return $rah_airports_found;
return \@match;
}
sub shift2(\@) {
return splice(@{$_[0]}, 0, 2);
}
sub get_latlong_matching_airports {
#my @arr;
my %args = (
airports => undef, #<arrayref airport list>, $airport_data will be the input from irport::Data::parse_airports("t/data/airports1.csv");
lat => undef, #<floating point> latitude
long => undef, #<floating point> # longitude
max => undef, #<floating point> # max - the distance within which an airport is a match.
@_
);
for my $x ( @{ $args{"airports"} }) {
my (@lat, @long);
if ($args{"lat"}) {
if ($x->{latitude_deg} =~ /(^\d+(?:\.\d+)?)/) {
push @lat, $x;
}
if ($args{"long"}) {
if ($x->{longitude_deg =~ /(^\d+(?:\.\d+)?)/) {
push @long, $x
}
return @lat, @long;
my(x1, x2) = shift2(@lat);
my(y1, y2) = shift2(@long);
while ($max != $distance) {
my $dist = sqrt(abs($y1 - $y2)**2 + (min(abs($x1 - $x2), abs(360 - abs($x1 - $x2))))**2);
if ($max
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment