Get elevation (altitude at ground level) using USGS Elevation Query Service API

2.1k views Asked by At

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?

3

There are 3 answers

6
Budius On BEST ANSWER

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

1
The_Martian On

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.

1
carl On

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!