Commit 99f4c263 authored by Mario Chirinos's avatar Mario Chirinos

MultyPolygon

parent 30c6cf08
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
""" """
Install: Install:
$ sudo pip install sentinelsat $ sudo pip3 install sentinelsat
$ sudo apt-get install libgdal-dev $ sudo apt-get install libgdal-dev
Example: Example:
......
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys, os import sys, os
import json import json
########### lee archivo de configuración ################ ############ lee archivo de configuración ################
dirname = os.path.dirname(__file__) dirname = os.path.dirname(__file__)
configfile = os.path.join(dirname, '../config/config.json') configfile = os.path.join(dirname, '../config/config.json')
...@@ -17,11 +17,11 @@ with open(configfile, 'r') as f: ...@@ -17,11 +17,11 @@ with open(configfile, 'r') as f:
SENTINEL_PATH = config['PATHS']['PATH_GEOSENTINEL'] SENTINEL_PATH = config['PATHS']['PATH_GEOSENTINEL']
########################################################### ###########################################################
sys.path.append(SENTINEL_PATH) sys.path.append(SENTINEL_PATH)
from geosentinel import APISentinel from geosentinel import APISentinel
from geosentinel import polygonToBox from geosentinel import polygonToBox
from osgeo import ogr
from datetime import date from datetime import date
from collections import OrderedDict
#def findSentinelProducts(wkt, startDate, endDate, platform, cloud): #def findSentinelProducts(wkt, startDate, endDate, platform, cloud):
# sentinel = APISentinel.APISentinel('asalazarg', 'geo135asg') # sentinel = APISentinel.APISentinel('asalazarg', 'geo135asg')
...@@ -39,14 +39,29 @@ def main(argv): ...@@ -39,14 +39,29 @@ def main(argv):
cfg = json.loads(jsonFile) cfg = json.loads(jsonFile)
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password']) sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
productList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"}, "Contains") wktList = []
if len(productList)<=0: if geometry.GetGeometryType() == ogr.wkbPolygon:
print ("No products found with 'Contains' trying with 'Intersects'") wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
productList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"}) wktList.append(wkt)
print(wkt)
# fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace(productsCodes[cfg['productLevel']], cfg['productLevel']) for k in productList.keys() ] 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"]+"/" downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
...@@ -56,57 +71,55 @@ def main(argv): ...@@ -56,57 +71,55 @@ def main(argv):
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']]) # rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
# print(rawDir) text =""
while text != "yes" and text != "no":
text ="" text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)")
while text != "yes" and text != "no": if text=="yes":
text = raw_input("Do you want to download this products to "+downloadDir+ " ? (yes, no)") sentinel.downloadProducts(productList,downloadDir)
if text=="yes":
sentinel.downloadProducts(productList,downloadDir)
L2ADir = cfg["productsDir"]+"L2A/"
if cfg["productLevel"]=="L1C":
L2ADir = cfg["productsDir"]+"L2A/" fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
if cfg["productLevel"]=="L1C": L1CDir = cfg["productsDir"]+"L1C/"
fileNamesL2A = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ] linksDir=cfg["projectDir"]+"L1C/"
L1CDir = cfg["productsDir"]+"L1C/" if not os.path.exists(linksDir):
linksDir=cfg["projectDir"]+"L1C/" os.mkdir(linksDir)
if not os.path.exists(linksDir): L1CDirList = os.listdir(L1CDir)
os.mkdir(linksDir)
L1CDirList = os.listdir(L1CDir) #LinkProducts
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
#LinkProducts matchingProducts = set(fileNames).intersection(set(L1CDirList))
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 =""
while text != "yes" and text != "no":
text = raw_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")
text =""
linksDir=cfg["projectDir"]+"L2A/"
while text != "yes" and text != "no":
text = raw_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)
matchingProducts = set(fileNames).intersection(set(dirList))
for f in matchingProducts: for f in matchingProducts:
print (f) print (f)
os.system("ln -s " + L2ADir+f + " " + linksDir+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)) + " Linked to " + linksDir)
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.")
text =""
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")
text =""
linksDir=cfg["projectDir"]+"L2A/"
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)
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)
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv)
......
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
from osgeo import ogr from osgeo import ogr
...@@ -19,8 +19,9 @@ def getWKTPolygonBoundingBox(polygon, wkt=False): ...@@ -19,8 +19,9 @@ def getWKTPolygonBoundingBox(polygon, wkt=False):
>>> print (g) >>> print (g)
-89.96360136135893 21.295577631081912 -89.24078398227726 20.754157792347172 -89.96360136135893 21.295577631081912 -89.24078398227726 20.754157792347172
''' '''
geometry = ogr.CreateGeometryFromWkt(polygon)
box = ogr.CreateGeometryFromWkt(polygon).GetEnvelope() #(minX, maxX, minY, maxY) box = geometry.GetEnvelope() #(minX, maxX, minY, maxY)
ulx = box[0] ulx = box[0]
uly = box[3] uly = box[3]
lrx = box[1] lrx = box[1]
......
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