Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_set_logger_handler()
if user is None or password is None:
try:
user, password = requests.utils.get_netrc_auth(url)
except TypeError:
pass
if user is None or password is None:
raise click.UsageError(
"Missing --user and --password. Please see docs "
"for environment variables and .netrc support."
)
api = SentinelAPI(user, password, url)
search_kwargs = {}
if sentinel and not (producttype or instrument):
search_kwargs["platformname"] = "Sentinel-" + sentinel
if instrument and not producttype:
search_kwargs["instrumentshortname"] = instrument
if producttype:
search_kwargs["producttype"] = producttype
if cloud:
if sentinel not in ["2", "3"]:
logger.error("Cloud cover is only supported for Sentinel 2 and 3.")
exit(1)
search_kwargs["cloudcoverpercentage"] = (0, cloud)
# MAIN
##############################################################################
###############################################
# load user credentials for Sentinel Data Hub at ESA
###############################################
# set working direcory
os.chdir(wd)
# read two lines of text with username and password
with open(credentials) as f:
lines = f.readlines()
username = lines[0].strip()
password = lines[1].strip()
f.close()
api = SentinelAPI(username, password, 'https://scihub.copernicus.eu/dhus')
###############################################
# load search area shapefile
###############################################
# get driver to read a shapefile
driver = ogr.GetDriverByName('ESRI Shapefile')
# open it
dataSource = driver.Open(shapefile, 0)
if dataSource is None:
print('Could not open ' + shapefile)
sys.exit(1) #exit with an error code
# get the layer from the shapefile
layer = dataSource.GetLayer()
# MAIN
##############################################################################
###############################################
# load user credentials for Sentinel Data Hub at ESA
###############################################
# set working direcory
os.chdir(wd)
# read two lines of text with username and password
with open(credentials) as f:
lines = f.readlines()
username = lines[0].strip()
password = lines[1].strip()
f.close()
api = SentinelAPI(username, password, 'https://scihub.copernicus.eu/dhus')
###############################################
# load search area shapefile
###############################################
# get driver to read a shapefile
driver = ogr.GetDriverByName('ESRI Shapefile')
# open it
dataSource = driver.Open(shapefile, 0)
if dataSource is None:
print('Could not open ' + shapefile)
sys.exit(1) #exit with an error code
# get the layer from the shapefile
layer = dataSource.GetLayer()
"""
Parameters
----------
geojson_path
cloud
start_date
end_date
conf
Returns
-------
A dicitonary of products
"""
api = SentinelAPI(conf["sen2"]["user"], conf["sen2"]["pass"], 'https://scihub.copernicus.eu/dhus')
footprint = geojson_to_wkt(read_geojson(geojson_path))
products = api.query(footprint,
platformname = 'Sentinel-2',
cloudcoverpercentage = (0, cloud),
date = (start_date, end_date))
return products
elif self.get_inputs_from != None:
self.products = context['task_instance'].xcom_pull(task_ids=self.get_inputs_from, key=XCOM_RETURN_KEY)
print("Downloading request for {} products via XCOM:\n{}".format(len(self.products), self.products))
else:
# exit gracefully if no products are found
log.info('no products to process')
return None
# log warning in case the amount of products exceed the limit
if len(self.products) > self.download_max:
log.warn("Found products ({}) exceeds download limit ({})".format(len(self.products), self.download_max))
# download all files via it's ID
log.info('Starting downloading..')
product_downloaded = {}
api = SentinelAPI(self.dhus_user, self.dhus_pass, self.dhus_url)
for product_id in self.products.keys():
# If download limit reached, stopp and break out
# Else if the file already exists, then try next from search
product_filename=os.path.join(self.download_dir,self.products[product_id].get("title")+".zip")
if len(product_downloaded) >= self.download_max:
log.info("Limit exceeded, stopping download..")
break;
elif os.path.exists(product_filename):
log.info("Product already downloaded. Continuing..")
continue
log.info('Downloading Product..\nuuid: {}\ntitle: {}\nsize: {}'.format(
product_id,
self.products[product_id].get("title"),
self.products[product_id].get("size"))
"""Return the products from a query response as a GeoPandas GeoDataFrame
with the values in their appropriate Python types.
"""
try:
import geopandas as gpd
import shapely.wkt
except ImportError:
raise ImportError(
"to_geodataframe requires the optional dependencies GeoPandas and Shapely."
)
crs = {"init": "epsg:4326"} # WGS84
if len(products) == 0:
return gpd.GeoDataFrame(crs=crs)
df = SentinelAPI.to_dataframe(products)
geometry = [shapely.wkt.loads(fp) for fp in df["footprint"]]
# remove useless columns
df.drop(["footprint", "gmlfootprint"], axis=1, inplace=True)
return gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
def auth_api(self, auth):
self.api = SentinelAPI(auth[0], auth[1], self.api_url)
return self.api
log.info('API User: %s', self.dhus_user)
#log.info('API Password: %s', self.dhus_pass)
log.info('Start Date: %s', self.startdate)
log.info('End Date: %s', self.enddate)
log.info('Filter Max: %s', self.filter_max)
log.info('Order By: %s', self.order_by)
log.info('GeoJSON: %s', self.geojson_bbox)
log.info('Keywords: %s', self.keywords)
log.info('Now is: {}'.format( datetime.now() ))
log.info('6 hours ago was: {}'.format( datetime.now() - timedelta(hours=6)) )
print("Execute DHUS Search.. ")
# search products
api = SentinelAPI(self.dhus_user, self.dhus_pass, self.dhus_url)
try:
footprint = geojson_to_wkt(read_geojson(self.geojson_bbox))
except:
log.error('Cannot open GeoJSON file: {}'.format(self.geojson_bbox))
return False
products = api.query(
area=footprint,
date=(self.startdate, self.enddate),
order_by=self.order_by,
limit=self.filter_max,
**self.keywords
)
log.info("Retrieving {} products:".format(len(products)))
products_summary="\n"
def sen2_download(products_file, conf):
"""This function downloads Sentinel-2 imagery
:param str products_file: The path to the CSV file containing the Sentinel-2 scene IDs.
:param str conf: String object pointing to the config.ini file which includes all required parameters to access
the SciHub and download the data.
"""
# TODO: read in csv file with product IDs
products = pd.read_csv(products_file)
# TODO: Specify download location, file resuming.
api = SentinelAPI(conf["sen2"]["user"], conf["sen2"]["pass"], 'https://scihub.copernicus.eu/dhus')
# TODO: assign correct column to use for product IDs. Currently assuming the the first column is correct.
api.download_all(products, conf["data"]["out_folder"])