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

MultyPolygon

parent 30c6cf08
......@@ -3,7 +3,7 @@
"""
Install:
$ sudo pip install sentinelsat
$ sudo pip3 install sentinelsat
$ sudo apt-get install libgdal-dev
Example:
......
#!/usr/bin/python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, os
import json
########### lee archivo de configuración ################
############ lee archivo de configuración ################
dirname = os.path.dirname(__file__)
configfile = os.path.join(dirname, '../config/config.json')
......@@ -17,11 +17,11 @@ with open(configfile, 'r') as f:
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')
......@@ -39,14 +39,29 @@ def main(argv):
cfg = json.loads(jsonFile)
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
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")
if len(productList)<=0:
print ("No products found with 'Contains' trying with 'Intersects'")
productList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"})
# fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace(productsCodes[cfg['productLevel']], cfg['productLevel']) for k in productList.keys() ]
geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
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() ]
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
......@@ -56,57 +71,55 @@ def main(argv):
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
# rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
# print(rawDir)
text =""
while text != "yes" and text != "no":
text = raw_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)
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))
text =""
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)
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))+" 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__":
main(sys.argv)
......
#!/usr/bin/python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from osgeo import ogr
......@@ -19,8 +19,9 @@ def getWKTPolygonBoundingBox(polygon, wkt=False):
>>> print (g)
-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]
uly = box[3]
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