Python – Search Images in USGS Earth Explorer Using Python API

apilandsatpython

Is it possible to search for images in USGS Earth Explorer using Python (API) script?

I tried the example here:

from usgs import api

# Set the EarthExplorer catalog
node = 'EE'# this indicates earth explorer website

# Set the Hyperion and Landsat 8 dataset
hyperion_dataset = 'EO1_HYP_PUB'
landsat8_dataset = 'LANDSAT_8'

# Set the scene ids
hyperion_scene_id = 'EO1H1820422014302110K2_SG1_01'
landsat8_scene_id = 'LC80290462015135LGN00'

# Submit requests to USGS servers
api.metadata(hyperion_dataset, node, [hyperion_scene_id])
api.metadata(landsat8_dataset, node, [landsat8_scene_id])

But I got:

USGSError                                 Traceback (most recent call last)
<ipython-input-4-dab0c21371c4> in <module>()
     11 
     12 # Submit requests to USGS servers
---> 13 api.metadata(hyperion_dataset, node, [hyperion_scene_id])
     14 api.metadata(landsat8_dataset, node, [landsat8_scene_id])
     15 

C:\Users\Ran\Anaconda2\lib\site-packages\usgs\api.pyc in metadata(dataset, node, entityids, extended, api_key)
    235     response = r.json()
    236 
--> 237     _check_for_usgs_error(response)
    238 
    239     if extended:

C:\Users\Ran\Anaconda2\lib\site-packages\usgs\api.pyc in _check_for_usgs_error(data)
     33     error = data['error']
     34 
---> 35     raise USGSError('%s: %s' % (error_code, error))
     36 
     37 

USGSError: AUTH_UNAUTHORIZED: Forbidden - Valid API Key required for access to 'metadata' method.

I don't understand where my password and user name should go?

I also tried this according to this:

import landsatxplore.api

# Initialize a new API instance and get an access key
api = landsatxplore.api.API('user', 'password')

# Request
scenes = api.search(
    dataset='LANDSAT_8',
    latitude=19.53,
    longitude=-1.53,
    start_date='2015-01-01',
    end_date='2016-01-01',
    max_cloud_cover=10)

print('{} scenes found.'.format(len(scenes)))

for scene in scenes:
    print(scene['acquisitionDate'])

api.logout()

But I got:

---------------------------------------------------------------------------
EarthExplorerError                        Traceback (most recent call last)
<ipython-input-6-a48259d60bd9> in <module>()
     11     start_date='2015-01-01',
     12     end_date='2016-01-01',
---> 13     max_cloud_cover=10)
     14 
     15 print('{} scenes found.'.format(len(scenes)))

C:\Users\Ran\Anaconda2\lib\site-packages\landsatxplore\api.pyc in search(self, dataset, latitude, longitude, bbox, max_cloud_cover, start_date, end_date, months, max_results)
    111             params.update(months=months)
    112 
--> 113         response = self.request('search', **params)
    114         return response['results']
    115 

C:\Users\Ran\Anaconda2\lib\site-packages\landsatxplore\api.pyc in request(self, request_code, **kwargs)
     39         response = requests.get(url, params=params).json()
     40         if response['error']:
---> 41             raise EarthExplorerError('EE: {}'.format(response['error']))
     42         else:
     43             return response['data']

EarthExplorerError: EE: Dataset is currently offline.

Do they have an official API for Python?
I would like to search (and download) images from the Earth Explorer database (not only Landsats) using Python, is it possible?

Best Answer

The documentation states you need to supply an API key to your request:

usgs.api.metadata(dataset, node, entityids, extended=False, api_key=None)

Reference: http://kapadia.github.io/usgs/reference/api.html#examples

It is also stated:

All requests require an account with USGS’s EROS service. The account must also have Machine to Machine privileges.

With the link pointing to https://ers.cr.usgs.gov/register/ to register your account. I didn't go through the process of creating an account, but I'd jump to the conclusion that after making an account it'd provide your an API key you could use in your request.

Related Question