Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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,


In reply to Re^2: creating a webpage to fetch/calculate weather data by Aldebaran
in thread creating a webpage to fetch/calculate weather data by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-16 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found