Commit db3731c2 authored by Mario Chirinos's avatar Mario Chirinos

git library

parent ff043eca
...@@ -8,13 +8,14 @@ Install: ...@@ -8,13 +8,14 @@ Install:
Example: Example:
$ python -m doctest -v APISentinel.py $ python3 -m doctest -v APISentinel.py
""" """
import os import os
from sentinelsat.sentinel import SentinelAPI from sentinelsat.sentinel import SentinelAPI
from datetime import date from datetime import date
from collections import OrderedDict from collections import OrderedDict
import time
#from osgeo import ogr #from osgeo import ogr
...@@ -23,8 +24,8 @@ class APISentinel(object): ...@@ -23,8 +24,8 @@ class APISentinel(object):
Test Case Test Case
>>> sentinel = APISentinel('asalazarg', 'geo135asg') >>> sentinel = APISentinel('asalazarg', 'geo135asg')
>>> products = sentinel.getProducts("POLYGON((-89.99450683593753 21.279137394108716,-89.13757324218751 21.2996106049456,-89.30236816406251 20.68418377935238,-90.0494384765625 20.715015145512098,-89.99450683593753 21.279137394108716))", ('20151219', date(2015,12,29)), {"platformname":"Sentinel-2"}) >>> products = sentinel.getProducts("POLYGON((-89.99450683593753 21.279137394108716,-89.13757324218751 21.2996106049456,-89.30236816406251 20.68418377935238,-90.0494384765625 20.715015145512098,-89.99450683593753 21.279137394108716))", ('20151219', date(2015,12,29)), {"platformname":"Sentinel-2"})
>>> print len(products) >>> print(len(products))
3 6
""" """
...@@ -93,19 +94,31 @@ class APISentinel(object): ...@@ -93,19 +94,31 @@ class APISentinel(object):
os.chdir(dir) os.chdir(dir)
# self.api.download_all(products) # self.api.download_all(products)
longTermArchiveList=[]
for p in products: for p in products:
print(products[p]['filename']) print(products[p]['filename'])
self.api.download(p) try:
info = self.api.download(p)
if not info["Online"]:
print("Adding producto to long term list")
longTermArchiveList.append(p)
time.sleep(1)
except:
def filterProducts(self, productList): print ("ERROR")
#, user_footprint, threshold):
products = productList.copy()
for p in productList: return longTermArchiveList
if productList[p]['filename'].find("OPER_PRD") != -1:
del products[p]
# def filterProducts(self, productList):
# #, user_footprint, threshold):
return products # products = productList.copy()
# for p in productList:
# if productList[p]['filename'].find("OPER_PRD") != -1:
# del products[p]
# return products
...@@ -4,123 +4,120 @@ ...@@ -4,123 +4,120 @@
import sys, os import sys, os
import json import json
############ lee archivo de configuración ################ if __name__ == "__main__": # Local Run
dirname = os.path.dirname(__file__) import APISentinel
configfile = os.path.join(dirname, '../config/config.json') import polygonToBox
else: # Module Run, When going production - delete if/else
from . import APISentinel
from . import polygonToBox
with open(configfile, 'r') as f:
config = json.load(f)
#print(config['PATHS']['PATH_GEOSENTINEL'])
SENTINEL_PATH = config['PATHS']['PATH_GEOSENTINEL']
###########################################################
sys.path.append(SENTINEL_PATH)
from geosentinel import APISentinel
from geosentinel import polygonToBox
from osgeo import ogr from osgeo import ogr
from datetime import date from datetime import date
from collections import OrderedDict from collections import OrderedDict
#def findSentinelProducts(wkt, startDate, endDate, platform, cloud):
# sentinel = APISentinel.APISentinel('asalazarg', 'geo135asg')
# products = sentinel.getProducts(wkt, (startDate, endDate), {"platformname":platform, "cloudcoverpercentage":"[0 TO "+str(cloud)+"]"})
# return products
#productsCodes = {"L1C":"L1C","L2A":"L1C"}
productType = {"L1C":"S2MSI1C", "L2A":"S2MSI2A"} productType = {"L1C":"S2MSI1C", "L2A":"S2MSI2A"}
def main(argv): def findSentinelProducts(cfg, uinput = False):#main(argv):
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
wktList = []
if geometry.GetGeometryType() == ogr.wkbPolygon:
print("POLYGON")
wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
wktList.append(wkt)
print(wkt)
elif geometry.GetGeometryType() == ogr.wkbMultiPolygon:
print("MULTIPOLYGON")
for i in range(0, geometry.GetGeometryCount()):
g = geometry.GetGeometryRef(i).ExportToWkt()
wkt = polygonToBox.getWKTPolygonBoundingBox(g, True)
print(wkt)
wktList.append(wkt)
if len(sys.argv) != 2: productList = OrderedDict()
print("Usage: " + argv[0] + " <JSON File>") for wkt in wktList:
else: tmpList =sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"}, "Contains")
jsonFile=open(argv[1]).read()
cfg = json.loads(jsonFile)
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password']) if len(productList)<=0:
geometry = ogr.CreateGeometryFromWkt(cfg['wkt']) print ("No products found with 'Contains' trying with 'Intersects'")
tmpList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"})
productList.update(tmpList)
wktList = [] fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
if geometry.GetGeometryType() == ogr.wkbPolygon:
wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
wktList.append(wkt)
print(wkt)
elif geometry.GetGeometryType() == ogr.wkbMultiPolygon:
for i in range(0, geometry.GetGeometryCount()):
g = geometry.GetGeometryRef(i).exportToWkt()
wkt = polygonToBox.getWKTPolygonBoundingBox(g, True)
print(wkt)
wktList.append(wkt)
productList = OrderedDict()
for wkt in wktList:
tmpList =sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"}, "Contains")
if len(productList)<=0:
print ("No products found with 'Contains' trying with 'Intersects'")
tmpList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"})
productList.update(tmpList)
# fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace(productsCodes[cfg['productLevel']], cfg['productLevel']) for k in productList.keys() ]
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/" downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
dirList = os.listdir(downloadDir) dirList = os.listdir(downloadDir)
matchingProducts = set(fileNames).intersection(set(dirList)) matchingProducts = set(fileNames).intersection(set(dirList))
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.") print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
# rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
text =""
text =""
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)") text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)")
if text=="yes": if uinput==False or text=="yes":
sentinel.downloadProducts(productList,downloadDir) longTermArchiveList = sentinel.downloadProducts(productList,downloadDir)
if len(longTermArchiveList)>0:
print( str(len(longTermArchiveList)) + " products are in the long term archive" )
L2ADir = cfg["productsDir"]+"L2A/" with open(cfg["projectDir"]+'longTermArchiv.json', 'w') as outfile:
if cfg["productLevel"]=="L1C": json.dump(longTermArchiveList, outfile)
fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
L1CDir = cfg["productsDir"]+"L1C/" L2ADir = cfg["productsDir"]+"L2A/"
linksDir=cfg["projectDir"]+"L1C/" if cfg["productLevel"]=="L1C":
if not os.path.exists(linksDir): fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
os.mkdir(linksDir) L1CDir = cfg["productsDir"]+"L1C/"
L1CDirList = os.listdir(L1CDir) linksDir=cfg["projectDir"]+"L1C/"
if not os.path.exists(linksDir):
#LinkProducts os.mkdir(linksDir)
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ] L1CDirList = os.listdir(L1CDir)
matchingProducts = set(fileNames).intersection(set(L1CDirList))
for f in matchingProducts: #LinkProducts
print (f) fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
print("ln -s " + L1CDir+f + " " + linksDir+f) matchingProducts = set(fileNames).intersection(set(L1CDirList))
for f in matchingProducts:
os.system("ln -s " + L1CDir+f + " " + linksDir+f) print (f)
print (str(len(matchingProducts)) + " Linked to " + linksDir) print("ln -s " + L1CDir+f + " " + linksDir+f)
os.system("ln -s " + L1CDir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir)
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.") print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.")
text ="" text =""
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to convert this products and save them to "+downloadDir+ " ? (yes, no)") text = input("Do you want to convert this products and save them to "+downloadDir+ " ? (yes, no)")
if text=="yes": if uinput==False or text=="yes":
#print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1") #print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1")
os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1") os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1")
text ="" text =""
linksDir=cfg["projectDir"]+"L2A/" linksDir=cfg["projectDir"]+"L2A/"
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to link this products to "+linksDir+ " ? (yes, no)") text = input("Do you want to link this products to "+linksDir+ " ? (yes, no)")
if text=="yes": if uinput==False or text=="yes":
fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ] fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
dirList = os.listdir(L2ADir) dirList = os.listdir(L2ADir)
matchingProducts = set(fileNames).intersection(set(dirList))
matchingProducts = set(fileNames).intersection(set(dirList)) for f in matchingProducts:
print (f)
os.system("ln -s " + L2ADir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir)
for f in matchingProducts: #===============================================================================
print (f) def main(argv):
os.system("ln -s " + L2ADir+f + " " + linksDir+f) if len(sys.argv) != 2:
print (str(len(matchingProducts)) + " Linked to " + linksDir) print("Usage: " + argv[0] + " <JSON File>")
else:
with open(argv[1]) as jsonFile:
cfg = json.load(jsonFile)
findSentinelProducts(cfg, uinput=True)
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv)
......
...@@ -22,7 +22,7 @@ def getPolygonArray(image, wkt_geom): ...@@ -22,7 +22,7 @@ def getPolygonArray(image, wkt_geom):
driverMEM = gdal.GetDriverByName('MEM') driverMEM = gdal.GetDriverByName('MEM')
driverMemory = ogr.GetDriverByName('Memory') driverMemory = ogr.GetDriverByName('Memory')
driverTiff = gdal.GetDriverByName('GTiff') driverTiff = gdal.GetDriverByName('GTiff')
driverShape = ogr.GetDriverByName("ESRI Shapefile") driverShape = ogr.GetDriverByName("ESRI Shapefile")
#RASTER #RASTER
target_ds = driverMEM.Create('raster.tif', cols, rows, 1, gdal.GDT_Byte) #MEM target_ds = driverMEM.Create('raster.tif', cols, rows, 1, gdal.GDT_Byte) #MEM
...@@ -33,7 +33,7 @@ def getPolygonArray(image, wkt_geom): ...@@ -33,7 +33,7 @@ def getPolygonArray(image, wkt_geom):
target_ds.GetRasterBand(1).FlushCache() target_ds.GetRasterBand(1).FlushCache()
#VECTOR #VECTOR
rast_ogr_ds = driverMemory.CreateDataSource( 'myshape.shp' ) rast_ogr_ds = driverMemory.CreateDataSource( 'myshape.shp' )
rast_mem_lyr = rast_ogr_ds.CreateLayer( 'POLYGON', sr4326, ogr.wkbPolygon) rast_mem_lyr = rast_ogr_ds.CreateLayer( 'POLYGON', sr4326, ogr.wkbPolygon)
feat = ogr.Feature( rast_mem_lyr.GetLayerDefn() ) feat = ogr.Feature( rast_mem_lyr.GetLayerDefn() )
feat.SetGeometry( ogr.CreateGeometryFromWkt(wkt_geom) ) feat.SetGeometry( ogr.CreateGeometryFromWkt(wkt_geom) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment