1. One of the Atlanta.pm members was raving at the last meeting about how useful Mojo is for web munging (scraping, parsing, and re-publishing as an internal "API"). I have no personal experience with Mojo, but I trust his ravings.
Then Mojo it is. Thanks so much for your post, Util, it helped me get farther in a lot of ways. I was able to get this to work on the command line, but I couldn't get Corion's curl converter to recognize a -S option, nor could I see it from man curl. The command seems to give the same output without it:
$ curl -s 'https://nationalmap.gov/epqs/pqs.php?units=Meters&output=js
+on&x=-83.65&y=41.37' | jq '.USGS_Elevation_Point_Query_Service.Elevat
+ion_Query.Elevation'
212.77
$
Unfortunately, I couldn't get this value in the perl script which I'm posting below. (Partial credit is great for me.)
NOAA has the elevation of the 2.5km grid polygon, which will be lower granularity, but might (I'm just guessing) be helpful for water, where the elevation varies:
I was thrilled to see this solution at the same server where I had been fishing! Furthermore, your example along with the jq command to see where the good data is in the haystack was exactly what I needed to get some numbers out of this. First I'll post the log then the source.
2020/06/23 15:26:07 INFO ./5.2.elev.pl
2020/06/23 15:26:07 INFO {
"USGS_Elevation_Point_Query_Service" => {
"Elevation_Query" => {
"Data_Source" => "3DEP 1/3 arc-second",
"Elevation" => -1000000,
"Units" => "Meters",
"x" => "41.37",
"y" => "-83.65"
}
}
}
2020/06/23 15:26:07 INFO 41.37 -83.65 0.208
2020/06/23 15:26:07 INFO ==============
2020/06/23 15:26:07 INFO https://api.weather.gov/points/41.37,-83.65
2020/06/23 15:26:08 INFO CLE
2020/06/23 15:26:08 INFO 16 52
2020/06/23 15:26:08 INFO ==============
2020/06/23 15:26:08 INFO https://api.weather.gov/gridpoints/CLE/16,52
2020/06/23 15:26:08 INFO ==============
2020/06/23 15:26:08 INFO 213.0552 wmoUnit:m
Source:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use Log::Log4perl;
use Mojo::UserAgent;
use open ':std', OUT => ':utf8';
use Mojo::Util qw(dumper);
# get rid of old log
my $file = '/home/hogan/Documents/hogan/logs/4.log4perl.txt';
unlink $file or warn "Could not unlink $file: $!";
my $log_conf4 = "/home/hogan/Documents/hogan/logs/conf_files/4.conf";
Log::Log4perl::init($log_conf4); #info
my $logger = Log::Log4perl->get_logger();
$logger->info("$0");
# weston, OH 41.37,-83.65
my $lat = 41.37;
my $long = -83.65;
# the API docs says you must identify yourself, please make this somet
+hing legit
my $name = '(example.com, contact@example.com)';
my $url2 =
"https://nationalmap.gov/epqs/pqs.php?output=json&units=Meters&x=$lat&
+y=$long";
my $ub = Mojo::UserAgent->new;
$ub->transactor->name($name);
# get JSON response
my $qv = $ub->get($url2)->res->json;
$logger->info( dumper $qv);
my $elev = .208; # 676 ft = .206 +.02 for observer #km
$logger->info("$lat $long $elev");
my $url1 = "https://api.weather.gov/points/$lat,$long";
$logger->info("==============");
$logger->info($url1);
my $ua = Mojo::UserAgent->new;
$ua->transactor->name($name);
# get JSON response
my $pv = $ua->get($url1)->res->json->{properties};
my $station = $pv->{'cwa'};
my $gridX = $pv->{'gridX'};
my $gridY = $pv->{'gridY'};
$logger->info("$station");
$logger->info("$gridX $gridY");
$logger->info("==============");
## new transaction
## find elevation
my $uc = Mojo::UserAgent->new;
$uc->transactor->name($name);
my $url3 = "https://api.weather.gov/gridpoints/$station/$gridX,$gridY"
+;
$logger->info( $url3 );
my $json = $uc->get($url3)->res->json;
#my $props = $json->{properties};
my $elev2 = $json->{properties}->{elevation}->{value};
my $unit = $json->{properties}->{elevation}->{unitCode};
$logger->info("==============");
#$logger->info(dumper($props));
$logger->info( $elev2 . ' '. $unit);
__END__
It takes me time fiddling with these things with perl and jq in order to get my head around them.
Q1) How do I get a valid value for the usgs query in lexical perl?
Thanks again for your post,