1

I have spent the better part of a week trying to research how to connect to the USGS Elevation Query Service API via my android app with embarrassing results. The best I have found is from 6-years ago here using now deprecated code. Spoiler - I couldn't get it to work.

Using GPS for elevation is simply too unreliable for my application needs, even after extensive moving averages and low/high pass filtration applied to the data.

Can anyone share their recommendations/code to retrieve elevation via the USGS Elevation Query Service. I can of course provide Latitude and Longitude?

Community
  • 1
  • 1
seekingStillness
  • 4,833
  • 5
  • 38
  • 68

3 Answers3

5

I just came across this page looking for something related and noticed that there is an error in the first response. It says this:

Here is an example call to Las Vegas http://nationalmap.gov/epqs/pqs.php?x=36.1251958&y=-115.3150863&output=json&units=Meters

The problem is that as per their website "If unable to find data at the requested point, this service returns -1000000" and I personally couldn't find any location that they have location (tried Las Vegas and San Francisco).

The example has X and Y reversed. Switch it to

https://nationalmap.gov/epqs/pqs.php?y=36.1251958&x=-115.3150863&output=json&units=Meters

and it works fine. Hope this helps some future searcher!

carl
  • 383
  • 1
  • 5
  • 10
  • x is longitude and y is latitude, x axis running across the equator and y axis running along the poles ... this gets me all the time – bluejay Mar 26 '22 at 16:01
2

a simple google search on "USGS Elevation Query Service API" pointed to http://ned.usgs.gov/epqs/ (different address from the question you linked. And the API seems extremely straight forward.

Here is an example call to Las Vegas http://nationalmap.gov/epqs/pqs.php?x=36.1251958&y=-115.3150863&output=json&units=Meters

The problem is that as per their website "If unable to find data at the requested point, this service returns -1000000" and I personally couldn't find any location that they have location (tried Las Vegas and San Francisco).

Alternatively, Google have an elevation API, that I can only assume it's pretty damn good considering Google Maps. Here you can see the docs https://developers.google.com/maps/documentation/elevation/start and here is an example query https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034

For the actual call itself, suggest is to use OkHttp (http://square.github.io/okhttp/) or Retrofit (which uses OkHttp) to do threading for you), but a basic code is:

OkHttpClient client = new OkHttpClient();

String getElevation(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}

then it's just parse the Json string

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks, up until now I've avoided Google. As I understand it, Google provides a very limited number of calls before charging a fee. But perhaps it is the best option. – seekingStillness Dec 21 '16 at 21:43
  • 1
    I've checked the price and it seems free up to 2500 requests a day, but to be honest, I tried calling the API without any API key and it worked fine, too – Budius Dec 21 '16 at 21:47
  • @Budius how did you made that? Can you please tell me – Prasad Apr 20 '17 at 12:51
  • @Prasad are you asking how did I search on Google or how did I copy-paste a standard OkHttp implementation from their docs? – Budius Apr 21 '17 at 16:20
  • @Budius i'm asking about elevation api without using API KEY – Prasad Apr 24 '17 at 05:56
  • @Prasad I just didn't add it to the request URL. There's one in my answer. – Budius Apr 24 '17 at 20:28
1

You use the one you cited by simply replacing the deprecated httpClient by the Google's recommended HttpUrlConnection. There are a lot of tutorials out there including Google's own sample code explained here.

The_Martian
  • 3,684
  • 5
  • 33
  • 61