Commit db3731c2 authored by Mario Chirinos's avatar Mario Chirinos

git library

parent ff043eca
......@@ -8,13 +8,14 @@ Install:
Example:
$ python -m doctest -v APISentinel.py
$ python3 -m doctest -v APISentinel.py
"""
import os
from sentinelsat.sentinel import SentinelAPI
from datetime import date
from collections import OrderedDict
import time
#from osgeo import ogr
......@@ -23,8 +24,8 @@ class APISentinel(object):
Test Case
>>> 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"})
>>> print len(products)
3
>>> print(len(products))
6
"""
......@@ -93,19 +94,31 @@ class APISentinel(object):
os.chdir(dir)
# self.api.download_all(products)
longTermArchiveList=[]
for p in products:
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):
#, user_footprint, threshold):
print ("ERROR")
products = productList.copy()
for p in productList:
if productList[p]['filename'].find("OPER_PRD") != -1:
del products[p]
return longTermArchiveList
# 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 @@
import sys, os
import json
############ lee archivo de configuración ################
dirname = os.path.dirname(__file__)
configfile = os.path.join(dirname, '../config/config.json')
if __name__ == "__main__": # Local Run
import APISentinel
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 datetime import date
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"}
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:
print("Usage: " + argv[0] + " <JSON File>")
else:
jsonFile=open(argv[1]).read()
cfg = json.loads(jsonFile)
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")
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
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)
wktList = []
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() ]
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
dirList = os.listdir(downloadDir)
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
dirList = os.listdir(downloadDir)
matchingProducts = set(fileNames).intersection(set(dirList))
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
# rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
matchingProducts = set(fileNames).intersection(set(dirList))
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
text =""
text =""
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)")
if text=="yes":
sentinel.downloadProducts(productList,downloadDir)
L2ADir = cfg["productsDir"]+"L2A/"
if cfg["productLevel"]=="L1C":
fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
L1CDir = cfg["productsDir"]+"L1C/"
linksDir=cfg["projectDir"]+"L1C/"
if not os.path.exists(linksDir):
os.mkdir(linksDir)
L1CDirList = os.listdir(L1CDir)
#LinkProducts
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
matchingProducts = set(fileNames).intersection(set(L1CDirList))
for f in matchingProducts:
print (f)
print("ln -s " + L1CDir+f + " " + linksDir+f)
os.system("ln -s " + L1CDir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir)
if uinput==False or text=="yes":
longTermArchiveList = sentinel.downloadProducts(productList,downloadDir)
if len(longTermArchiveList)>0:
print( str(len(longTermArchiveList)) + " products are in the long term archive" )
with open(cfg["projectDir"]+'longTermArchiv.json', 'w') as outfile:
json.dump(longTermArchiveList, outfile)
L2ADir = cfg["productsDir"]+"L2A/"
if cfg["productLevel"]=="L1C":
fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
L1CDir = cfg["productsDir"]+"L1C/"
linksDir=cfg["projectDir"]+"L1C/"
if not os.path.exists(linksDir):
os.mkdir(linksDir)
L1CDirList = os.listdir(L1CDir)
#LinkProducts
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
matchingProducts = set(fileNames).intersection(set(L1CDirList))
for f in matchingProducts:
print (f)
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.")
text =""
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.")
text =""
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to convert this products and save them to "+downloadDir+ " ? (yes, no)")
if text=="yes":
#print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1")
os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1")
if uinput==False or text=="yes":
#print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1")
os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1")
text =""
linksDir=cfg["projectDir"]+"L2A/"
text =""
linksDir=cfg["projectDir"]+"L2A/"
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to link this products to "+linksDir+ " ? (yes, no)")
if text=="yes":
fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
dirList = os.listdir(L2ADir)
if uinput==False or text=="yes":
fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
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)
os.system("ln -s " + L2ADir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir)
#===============================================================================
def main(argv):
if len(sys.argv) != 2:
print("Usage: " + argv[0] + " <JSON File>")
else:
with open(argv[1]) as jsonFile:
cfg = json.load(jsonFile)
findSentinelProducts(cfg, uinput=True)
if __name__ == "__main__":
main(sys.argv)
......
......@@ -22,7 +22,7 @@ def getPolygonArray(image, wkt_geom):
driverMEM = gdal.GetDriverByName('MEM')
driverMemory = ogr.GetDriverByName('Memory')
driverTiff = gdal.GetDriverByName('GTiff')
driverShape = ogr.GetDriverByName("ESRI Shapefile")
driverShape = ogr.GetDriverByName("ESRI Shapefile")
#RASTER
target_ds = driverMEM.Create('raster.tif', cols, rows, 1, gdal.GDT_Byte) #MEM
......@@ -33,7 +33,7 @@ def getPolygonArray(image, wkt_geom):
target_ds.GetRasterBand(1).FlushCache()
#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)
feat = ogr.Feature( rast_mem_lyr.GetLayerDefn() )
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