def get_landsat_scenes(wlon, nlat, elon, slat, startdate, enddate, cloud, formal_name: str): """List landsat scenes from USGS.""" credentials = get_credentials()['landsat'] api = API(credentials['username'], credentials['password']) landsat_folder_id = EE_FOLDER.get(formal_name) if landsat_folder_id is None: raise ValueError( 'Invalid Landsat product name. Expected one of {}'.format( EE_FOLDER.keys())) # Request scenes_result = api.search(dataset=formal_name, bbox=(slat, wlon, nlat, elon), start_date=startdate, end_date=enddate, max_cloud_cover=cloud or 100, max_results=50000) scenes_output = {} for scene in scenes_result: if scene['displayId'].endswith('RT'): logging.warning('Skipping Real Time {}'.format(scene['displayId'])) continue copy_scene = dict() copy_scene['sceneid'] = scene['displayId'] copy_scene['scene_id'] = scene['entityId'] copy_scene['cloud'] = int(scene['cloudCover']) copy_scene['date'] = scene['acquisitionDate'] xmin, ymin, xmax, ymax = scene['sceneBounds'].split(',') copy_scene['wlon'] = float(xmin) copy_scene['slat'] = float(ymin) copy_scene['elon'] = float(xmax) copy_scene['nlat'] = float(ymax) copy_scene['link'] = EE_DOWNLOAD_URL.format(folder=landsat_folder_id, sid=scene['entityId']) pathrow = scene['displayId'].split('_')[2] copy_scene['path'] = pathrow[:3] copy_scene['row'] = pathrow[3:] scenes_output[scene['displayId']] = copy_scene return scenes_output
def get_landsat_scenes(wlon, nlat, elon, slat, startdate, enddate, cloud, limit): """List landsat scenes from USGS.""" credentials = get_credentials()['landsat'] api = API(credentials['username'], credentials['password']) # Request scenes_result = api.search(dataset='LANDSAT_8_C1', bbox=(slat, wlon, nlat, elon), start_date=startdate, end_date=enddate, max_cloud_cover=cloud or 100, max_results=10000) scenes_output = {} for scene in scenes_result: if scene['displayId'].endswith('RT'): logging.warning('Skipping Real Time {}'.format(scene['displayId'])) continue copy_scene = dict() copy_scene['sceneid'] = scene['displayId'] copy_scene['scene_id'] = scene['entityId'] copy_scene['cloud'] = int(scene['cloudCover']) copy_scene['date'] = scene['acquisitionDate'] xmin, ymin, xmax, ymax = scene['sceneBounds'].split(',') copy_scene['wlon'] = float(xmin) copy_scene['slat'] = float(ymin) copy_scene['elon'] = float(xmax) copy_scene['nlat'] = float(ymax) copy_scene[ 'link'] = 'https://earthexplorer.usgs.gov/download/12864/{}/STANDARD/EE'.format( scene['entityId']) pathrow = scene['displayId'].split('_')[2] copy_scene['path'] = pathrow[:3] copy_scene['row'] = pathrow[3:] scenes_output[scene['displayId']] = copy_scene return scenes_output
def search( username, password, dataset, location, bbox, clouds, start, end, output, limit ): """Search for scenes.""" api = API(username, password) where = {"dataset": dataset} if location: latitude, longitude = location where.update(latitude=latitude, longitude=longitude) if bbox: where.update(bbox=bbox) if clouds: where.update(max_cloud_cover=clouds) if start: where.update(start_date=start) if end: where.update(end_date=end) if limit: where.update(max_results=limit) results = api.search(**where) api.logout() if not results: return if output == "entity_id": for scene in results: click.echo(scene["entity_id"]) if output == "display_id": for scene in results: click.echo(scene["display_id"]) if output == "json": dump = json.dumps(results, indent=True) click.echo(dump) if output == "csv": with StringIO("tmp.csv") as f: w = csv.DictWriter(f, results[0].keys()) w.writeheader() w.writerows(results) click.echo(f.getvalue())
class QCConnectLandsat: def __init__(self, username, password, archive, backup_archive): """Connect API. Raise ProcessorFailedError on failure :param str username: username :param str password: password :param str archive: not used by Landsat implementation :param str backup_archive: not used by Landsat implementation """ from landsatxplore.api import API as LandsatAPI from landsatxplore.exceptions import EarthExplorerError try: self.api = LandsatAPI( username, password ) except EarthExplorerError as e: raise ProcessorFailedError( self, "Unable to connect API: {}".format(e), set_status=False ) except json.JSONDecodeError as e: raise ProcessorFailedError( self, "Landsat server is down. It raised a JSON exception: " "{}".format(e), set_status=False ) def __del__(self): if not hasattr(self, "api"): return from landsatxplore.exceptions import EarthExplorerError try: self.api.logout() except EarthExplorerError as e: Logger.error("Landsat server is down. {}".format(e)) def query(self, footprint, kwargs): """Query API. :return: result """ from landsatxplore.exceptions import EarthExplorerError kwargs['bbox'] = wkt2bbox(footprint, switch_axis=True) kwargs['max_results'] = 500 del kwargs['producttype'] # used only for testing try: items = self.api.search(**kwargs) except EarthExplorerError as e: raise ProcessorFailedError( self, "Landsat server is down. " "{}".format(e), set_status=False ) dict_items = {} for item in items: selected = False if self.filter_by_tiles: for tile in self.filter_by_tiles: if str(tile) in item['entityId']: selected = True break else: selected = True if selected: dict_items[item['entityId']] = item # used tests only dict_items[item['entityId']]['producttype'] = item['displayId'].split('_')[1] dict_items[item['entityId']]['beginposition'] = \ datetime.strptime(item['startTime'], '%Y-%m-%d') else: Logger.info("IP {} skipped by tile filter".format(item['entityId'])) return dict_items
dataset = "landsat_8_c1" # https://pypi.org/project/landsatxplore/ # Landsat 7 ETM+ Collection 1 Level 1 landsat_etm_c1 # Landsat 7 ETM+ Collection 2 Level 1 landsat_etm_c2_l1 # Landsat 7 ETM+ Collection 2 Level 2 landsat_etm_c2_l2 # Landsat 8 Collection 1 Level 1 landsat_8_c1 # Landsat 8 Collection 2 Level 1 landsat_ot_c2_l1 # Landsat 8 Collection 2 Level 2 landsat_ot_c2_l2 # Initialize a new API instance and search for Landsat TM scenes print("Searching from " + str(start_date) + " to " + str(end_date)) password = keyring.get_password("EarthExplorer", username) api = API(username, password) scenes = api.search(dataset=dataset, latitude=latitude, longitude=longitude, start_date=str(start_date), end_date=(str(end_date)), max_cloud_cover=10) print(f"{len(scenes)} scenes found:") for scene in scenes: print(scene['acquisition_date']) # Initialize nvdi_array nvdi_array = np.zeros((len(scenes), YSize, XSize)) nvdi_array = nvdi_array.astype('float32') print(nvdi_array.shape) # Iterate through list of scenes for i in range(len(scenes)): scene = scenes[i] landsat_product_id = scene['landsat_product_id']
latitude, longitude = sys.argv[1], sys.argv[2] start_date = sys.argv[3] end_date = None months=None max_cloud_cover=None path = "/mnt/c/Users/avaro/Desktop/LST/" + site_id + "/" user = "******" pw = "pepsiav123pepsiav123" api = api(user,pw) # scenes = api.search( dataset, latitude=latitude, longitude=longitude, bbox=None, start_date=start_date, end_date=end_date, max_cloud_cover=max_cloud_cover, months=months, max_results=2) scenes = api.search( dataset, latitude=latitude, longitude=longitude,max_cloud_cover=max_cloud_cover, months=months) print('{} scenes found.'.format(len(scenes))) api.logout() #init downloader downloader = ee(user, pw) # entity id of latest print(scenes[0]['entityId']) # download & persist downloader.download(scenes[0]['entityId'],path) #close session downloader.logout()