note
Aldebaran
<i>I can't see your code because Gitlab...</i>
<p>Nuts. I apologize for the dry link. I don't mean to be coy about posting source, but rather more mindful of the initial S in SSCCE. It's not punitively long:</p>
<p>Source:</p>
<readmore>
<c>
#!/usr/bin/perl
use v5.030; # strictness implied
use warnings;
use feature qw[ signatures ];
no warnings qw[ experimental::signatures ];
use Data::Dumper;
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::TimeZone;
use Log::Log4perl;
use Try::Tiny;
our $debug = 0;
my $logger = init_log();
my $ref_events = init_event();
my @events = @$ref_events;
my $pi = atan2( 1, 1 ) * 4;
$logger->info("pi is $pi ");
my $redrock = 5200; #Willsey thumbnail for Bonneville Trail elevation
$logger->info("Bonneville max altitude in feet: $redrock");
## event loop
# Initial previous event to something
my ( $prev_lat, $prev_long ) =
( 42, -112 ); #some point in the snake river plain
for my $event (@events) {
my $epoch = parse_event($event);
## determine altitude
die unless exists $event->{location}; # restriction
my $return = get_elevation_from_coordinates( $event->{location}->{lat},
$event->{location}->{lon}, $debug );
$logger->info(
" $event->{name} $event->{location}->{lat} $event->{location}->{lon}");
$logger->info("return from the google is $return meters");
my $feet = 3.28084 * $return;
$logger->info("Altitude in feet is $feet");
my $diff = $redrock - $feet;
$logger->info("Difference from max Bonneville elevation is $diff ft");
$logger->info("==============");
my $d = distance(
$prev_lat, $prev_long,
$event->{location}->{lat},
$event->{location}->{lon}, "M"
);
$logger->info("distance is $d miles");
$logger->info("==============");
# rotate values
$prev_lat = $event->{location}->{lat};
$prev_long = $event->{location}->{lon};
}
## end event loop
sub init_log {
{
package Log::Log4perl;
sub get_logger ($pkg) { return bless [], $pkg; }
sub show (@arg) { warn @arg, "\n"; }
sub debug ( $ignore, @rest ) { show( 'DEBUG: ', @rest ); }
sub info ( $ignore, @rest ) { show( 'INFO: ', @rest ); }
sub warn ( $ignore, @rest ) { show( 'WARNING: ', @rest ); }
sub error ( $ignore, @rest ) { die 'ERROR: ', @rest, "\n"; }
}
my $logger2 = Log::Log4perl->get_logger();
$logger2->info("$0");
return $logger2;
}
sub init_event {
my $date_str = "2021-10-14";
my $time_str = "03:22:31";
my @events = (
{
"name" => "Boise",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -116.2, lat => 43.61 },
},
{
"name" => "near sublett",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -113.2104084, lat => 42.3278114 }
},
{
"name" => "snowville",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -112.7105234, lat => 41.9655701 },
42.152429, -112.9842191
},
{
"name" => "juniper",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -112.9842191, lat => 42.152429 }
},
{
"name" => "on the outcrop",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -111.7849951, lat => 40.703684, },
},
{
"name" => "Cascade way",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -111.7941259, lat => 40.7062734 },
},
{
"name" => "Mantua",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -111.944728, lat => 41.5073303 },
},
{
"name" => "dry creek",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -111.9537977, lat => 41.5501001 },
},
{
"name" => "wellsville",
"date" => $date_str,
"time" => $time_str,
"location" => { lon => -111.9288947, lat => 41.6365147 },
},
);
return \@events;
}
sub event2str {
my $event = shift;
if ( !exists $event->{_is_parsed} ) {
warn "event has not been parsed, just dumping it...";
print Dump($event);
}
my $str =
$event->{name} . " on "
. $event->{datetime} . " ("
. $event->{datetime}->epoch
. " seconds unix-epoch)"
. " timezone: "
. $event->{datetime}->time_zone->name;
if ( exists $event->{location} ) {
if ( ref( $event->{location} ) eq 'HASH' ) {
$str .=
" (lat: "
. $event->{location}->{lat}
. ", lon: "
. $event->{location}->{lon} . ")";
}
else { $str .= "(" . $event->{location} . ")" }
}
return $str;
}
sub parse_event {
my $event = shift;
$debug //= 0;
if ( !exists $event->{date} ) { die "date field is missing from event." }
my $datestr = $event->{date};
die "event does not have a 'name' field, please specify one, anything really."
unless exists $event->{name};
my $timestr = "00:00:01";
if ( exists $event->{time} ) {
$timestr = $event->{time};
print "event2epoch(): setting time to '$timestr' ...\n"
if $debug > 0;
die "time '$timestr' is not valid, it must be in the form 'hh:mm:ss'."
unless $timestr =~ /^\d{2}:\d{2}:\d{2}$/;
}
else { $event->{time} = $timestr }
my $isostr = $datestr . 'T' . $timestr;
my $dt = DateTime::Format::ISO8601->parse_datetime($isostr);
die "failed to parse date '$isostr', check date and time fields."
unless defined $dt;
$event->{datetime} = $dt;
my $tzstr = 'UTC';
if ( exists $event->{timezone} ) {
$tzstr = $event->{timezone};
print
"event2epoch(): found a timezone via 'timezone' field as '$tzstr' (that does not mean it is valid) ...\n"
if $debug > 0;
}
elsif ( exists $event->{location} ) {
my $loc = $event->{location};
if ( ( ref($loc) eq '' ) && ( $loc =~ /^[a-zA-Z]$/ ) ) {
# we have a location string
my @alltzs = DateTime::TimeZone->all_names;
my $tzstr;
for (@alltzs) {
if ( $_ =~ /$loc/i ) { $tzstr = $_; last }
}
die
"event's location can not be converted to a timezone, consider specifying the 'timezone' directly or setting 'location' coordinates with: \[lat,lon\]."
unless $tzstr;
print
"event2epoch(): setting timezone via 'location' name to '$timestr' ...\n"
if $debug > 0;
}
elsif ( ( ref($loc) eq 'HASH' )
&& ( exists $loc->{lat} )
&& ( exists $loc->{lon} ) )
{
# we have a [lat,lon] array for location
require Geo::Location::TimeZone;
my $gltzobj = Geo::Location::TimeZone->new();
$tzstr = $gltzobj->lookup( lat => $loc->{lat}, lon => $loc->{lon} );
if ( !$tzstr ) {
die "timezone lookup from location coordinates lat:"
. $loc->{lat}
. ", lon:"
. $loc->{lon}
. " has failed.";
}
print "event2epoch(): setting timezone via 'location' coordinates lat:"
. $loc->{lat}
. ", lon:"
. $loc->{lon}
. " ...\n"
if $debug > 0;
}
}
if ($tzstr) {
print "event2epoch(): deduced timezone to '$tzstr' and setting it ...\n"
if $debug > 0;
try {
$dt->set_time_zone($tzstr)
}
catch {
die "$_\n failed to set the timezone '$tzstr', is it valid?"
}
}
$event->{_is_parsed} = 1;
$event->{epoch} = $dt->epoch;
return $event->{epoch};
}
sub get_elevation_from_coordinates {
my ( $lat, $lon, $debug ) = @_;
use LWP::UserAgent;
use HTTP::Request;
use Data::Roundtrip;
$debug //= 0;
my $ua = LWP::UserAgent->new( agent =>
'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0', );
my $response;
my $payload =
'latitude='
. $lat
. '&longitude='
. $lon
. '&application_max_assets_mtime=1559625591';
my $payloadlen = length($payload);
# this request was translated from Curl command-line
# by [Corion]'s https://corion.net/curl2lwp.psgi
my $req = HTTP::Request->new(
'POST' =>
'https://www.mapcoordinates.net/admin/component/edit/Vpc_MapCoordinates_Advanced_GoogleMapCoords_Component/Component/json-get-elevation',
[
'Connection' => 'keep-alive',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, x-gzip, deflate, x-bzip2, bzip2',
'Accept-Language' => 'en-US,en;q=0.5',
# 'Host' => 'www.mapcoordinates.net:443',
'Referer' => 'https://www.mapcoordinates.net/en',
'User-Agent' =>
'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0',
'Content-Length' => $payloadlen,
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'DNT' => '1',
'Origin' => 'https://www.mapcoordinates.net',
'X-Requested-With' => 'XMLHttpRequest'
],
$payload
);
die "call to HTTP::Request has failed" unless $req;
if ( $debug > 0 ) {
$logger->debug(
"$0 : $payload\n$0 : sending above payload, of $payloadlen bytes...");
}
$response = $ua->request($req);
die "Error fetching: " . $response->status_line
unless $response->is_success;
my $content = $response->decoded_content;
my $data = Data::Roundtrip::json2perl($content);
die "failed to parse received data:\n$content\n"
unless exists $data->{'elevation'};
return $data->{'elevation'};
}
sub distance {
my ( $lat1, $lon1, $lat2, $lon2, $unit ) = @_;
if ( ( $lat1 == $lat2 ) && ( $lon1 == $lon2 ) ) {
return 0;
}
else {
my $theta = $lon1 - $lon2;
my $dist = sin( deg2rad($lat1) ) * sin( deg2rad($lat2) ) +
cos( deg2rad($lat1) ) * cos( deg2rad($lat2) ) * cos( deg2rad($theta) );
$dist = acos($dist);
$dist = rad2deg($dist);
$dist = $dist * 60 * 1.1515;
if ( $unit eq "K" ) {
$dist = $dist * 1.609344;
}
elsif ( $unit eq "N" ) {
$dist = $dist * 0.8684;
}
return ($dist);
}
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function get the arccos function using arctan function :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub acos {
my ($rad) = @_;
my $ret = atan2( sqrt( 1 - $rad**2 ), $rad );
return $ret;
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts decimal degrees to radians :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub deg2rad {
my ($deg) = @_;
return ( $deg * $pi / 180 );
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts radians to decimal degrees :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub rad2deg {
my ($rad) = @_;
return ( $rad * 180 / $pi );
}
__END__
</c>
</readmore>
<p>Cheers,</p>
11149859
11149877