Update

parent 3841ef2c
...@@ -18,6 +18,8 @@ class APISentinel(object): ...@@ -18,6 +18,8 @@ class APISentinel(object):
Test Case Test Case
>>> sentinel = APISentinel('asalazarg', 'geo135asg') >>> sentinel = APISentinel('asalazarg', 'geo135asg')
>>> dir="make" >>> dir="make"
>>> if not os.path.exists(dir):
>>> os.mkdir(dir)
>>> 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 3
......
# -*- coding: utf-8 -*-
# @package Index Sentinel Files
"""
Example:
$ python -m doctest -v Index.py
"""
""" Class for Sentinel satellites configuration
Test Case
>>> Files = tools('MSIL1C')
>>> dir="C:\Users\Garantias723\Documents\Poncho\Image_Sentinel"
>>> platform='Sentinel-2'
>>> list_files=Files.findFilesSentinel(dir,platform)
>>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
>>> read=readProduct(dir,platform)
>>> for image in list_files:
>>> file = dir + "/" + image
>>> read.ReadfilesSentinel(file)
>>> band_names = product.getBandNames()
>>> print("Bands: %s" % (list(band_names)))
"""
import os,sys
sys.path.append(os.path.join(os.path.expandvars("%userprofile%"),".snap\snap-python"))
import snappy,numpy
from snappy import HashMap,jpy,Product,ProductData,ProductIO,ProductUtils
from tools import (tools)
from readProduct import (readProduct)
class Index(object):
def __init__(self,ResolSize,IndexType):
"""The constructor Initialize Sentinel Data.
Args:
self: The object pointer.
folder (str): destination directory.
Returns:
pointer: The object pointer.
"""
self.resolution=ResolSize
self.Index_Type=IndexType
self.typeNDVI = "NDVI"
self.typeNDWI = "NDWI"
self.typeNBAI = "NBAI"
self.HashMap = jpy.get_type('java.util.HashMap')
def Index(self,product, file):
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
product_name = product.getName()
# input product red & nir bands
#if (self.Index_Type == "NDVI"):
if (self.Index_Type == self.typeNDVI):
print"Entre NDVI..."
type=self.typeNDVI
red_band = product.getBand('B4')
nir_band = product.getBand('B8')
if (self.Index_Type == self.typeNDWI):
print"Entre NDWI..."
type=self.typeNDWI
red_band = product.getBand('B11')
nir_band = product.getBand('B8')
if (self.Index_Type == self.typeNBAI):
print"Entre NBAI..."
type=self.typeNBAI
do_band = product.getBand('B12')
nir_band = product.getBand('B8')
red_band = product.getBand('B2')
do_row = numpy.zeros(width, dtype=numpy.float32)
# output product (ndvi) & new band
# output_product = Product('NDVI', 'NDVI', width, height)
output_product = Product(self.Index_Type, self.Index_Type, width, height)
ProductUtils.copyGeoCoding(product, output_product)
output_band = output_product.addBand(type, ProductData.TYPE_FLOAT32)
# output writer
# Tam = len(image)
fileOut = file[:-4] + '_' + self.Index_Type + '.dim'
output_product_writer = ProductIO.getProductWriter('BEAM-DIMAP')
output_product.setProductWriter(output_product_writer)
# output_product.writeHeader(product_name + '.ndvi.dim')
output_product.writeHeader(fileOut)
# compute & save ndvi line by line
red_row = numpy.zeros(width, dtype=numpy.float32)
nir_row = numpy.zeros(width, dtype=numpy.float32)
if (self.Index_Type == self.typeNDVI) or (self.Index_Type == self.typeNDWI):
print"Entre NDVI o NDWI FOR..."
for y in xrange(height):
red_row = red_band.readPixels(0, y, width, 1, red_row)
nir_row = nir_band.readPixels(0, y, width, 1, nir_row)
ndvwi = (nir_row - red_row) / (nir_row + red_row)
output_band.writePixels(0, y, width, 1, ndvwi)
if (self.Index_Type == self.typeNBAI):
print"Entre NBAI FOR..."
for y in xrange(height):
red_row = red_band.readPixels(0, y, width, 1, red_row)
nir_row = nir_band.readPixels(0, y, width, 1, nir_row)
do_row = do_band.readPixels(0, y, width, 1, do_row)
nbai = (do_row - nir_row / red_row) / (do_row + nir_row / red_row)
output_band.writePixels(0, y, width, 1, nbai)
output_product.closeIO()
#Applying Index Flag=3 Search .dim files
#Find Sentinel Files
Files = tools("all") #IW_SLC,IW_GRDH,MSIL1C
dir="C:\Users\Garantias723\Documents\Poncho\Image_Sentinel"
platform='Sentinel-2'
list_files=Files.findFilesSentinel(dir,platform,3)
print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
#Read Sentinel Files
Read=readProduct(dir,platform)
#Applying Resampling
Ind="NBAI"
Indeex=Index("60",Ind)
for image in list_files:
file = dir + "\\" + image
print"Applying Index "+Ind+"... ", image
product=Read.ReadFilesSentinel(file,2)#The flag 2 ReadFiles .dim
Indeex.Index(product,file)
#NDVI - Normalized Difference Vegetation Index,
#NDWI - Normalized Difference Water Index,
#NBAI - Normalized Built-up Area Index
\ No newline at end of file
# -*- coding: utf-8 -*-
# @package Merge Sentinel Files
"""
Example:
$ python -m doctest -v Merge.py
"""
""" Class for Sentinel satellites configuration
Test Case
"""
import os,sys
#sys.path.append(os.path.join(os.path.expandvars("%userprofile%"),".snap\snap-python"))
from os.path import expanduser
home = expanduser("~")
a=os.path.join(os.path.expandvars(home),".snap/snap-python/")
print"SNAPPY..",a
sys.path.append(a)
import cv2
import snappy,numpy
from snappy import jpy,ProductData,ProgressMonitor,ProductUtils
from tools import (tools)
from readProduct import (readProduct)
jpy = snappy.jpy
Color = jpy.get_type('java.awt.Color')
ColorPoint = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef$Point')
ColorPaletteDef = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef')
ImageInfo = jpy.get_type('org.esa.snap.core.datamodel.ImageInfo')
ImageLegend = jpy.get_type('org.esa.snap.core.datamodel.ImageLegend')
JAI = jpy.get_type('javax.media.jai.JAI')
System = jpy.get_type('java.lang.System')
System.setProperty('com.sun.media.jai.disableMediaLib', 'true')
class Merge(object):
def __init__(self,ResolSize,TypeMerge):
"""The constructor Initialize Sentinel Data.
Args:
self: The object pointer.
folder (str): destination directory.
Returns:
pointer: The object pointer.
"""
self.resolution=ResolSize
self.ext=".jpg"
self.Type_Merge="_"+TypeMerge
self.HashMap = jpy.get_type('java.util.HashMap')
self.looks = jpy.get_type('org.esa.snap.core.datamodel.quicklooks.QuicklookGenerator')
self.File = jpy.get_type('java.io.File')
self.ImageManager = jpy.get_type('org.esa.snap.core.image.ImageManager')
(self.h, self.w) = (None, None)
def merges(self,product, products,file):
##blue = product.getBand('B2')
##green = product.getBand('B3')
##red = product.getBand('B4')
##width = product.getSceneRasterWidth()
##height = product.getSceneRasterHeight()
##RGB = [green,red, blue]#Bueno
#################################################################################
#RES = []
#for i in range(len(RGB)):
# Band = ProductData.createInstance(numpy.zeros((width, height), numpy.uint16))
# RGB[i].readRasterData(0, 0, width, height, Band)
# RES.append(Band)
# Image without georreference
##info = ProductUtils.createImageInfo(RGB, True, ProgressMonitor.NULL)
##image = ProductUtils.createRgbImage(RGB, info, ProgressMonitor.NULL)
# print"Wrinting PNG..."
##salida = file[:-4] + self.Type_Merge + self.ext
##savefile = self.File(salida)
##self.looks.writeImage(image, savefile)
######################################################################################
#salida = file[:-5] + self.Type_Merge+ self.ext
#image_info = ProductUtils.createImageInfo(RGB, True, ProgressMonitor.NULL)
#im = self.ImageManager.getInstance().createColoredBandImage(RGB, image_info, 0)
#JAI.create("filestore", im, salida, "JPEG-LS")
##TotImage = len(products)
##print"IMAGES...",products
#sys.exit()
# Get Raster
#dataset1 = gdal.Open(products[2], GA_ReadOnly)
#dataset2 = gdal.Open(products[1], GA_ReadOnly)
#dataset3 = gdal.Open(products[0], GA_ReadOnly)
##dataset1=cv2.imread(products[2], cv2.IMREAD_UNCHANGED)#B4 R
##dataset2=cv2.imread(products[1], cv2.IMREAD_UNCHANGED)#B3 G
##dataset3=cv2.imread(products[0], cv2.IMREAD_UNCHANGED)#B2 B
##salida = file[:-5] + self.Type_Merge
##image = cv2.merge((dataset3, dataset2, dataset1))
##cv2.imwrite(salida + '.jpg', image)
########################################################################
red = Image.open(products[2]).convert('L')
blue = Image.open(products[0])
green = Image.open(products[1])
out = Image.merge("RGB", (red, green, blue))
out.save("img-out.TIF") #https://gis.stackexchange.com/questions/180534/merge-rgb-band-using-python
#https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python
#https://gist.github.com/glombard/7cd166e311992a828675
#https://pillow.readthedocs.io/en/3.1.x/reference/Image.html
#https://stackoverflow.com/questions/32812547/working-with-jp2-image-on-python-pillow
def search_band_combination(self,sentinel_image, Type_Merge):
Granule = "/GRANULE"
path = sentinel_image + Granule
file = os.listdir(path)
path = path + "/" + file[0]
Img_Data = "/IMG_DATA"
path = path + Img_Data
file = os.listdir(path)
products = []
if Type_Merge == "RGB":
ext1 = "B04.jp2"
ext2 = "B03.jp2"
ext3 = "B02.jp2"
raster = [f for f in file if f.endswith(ext1) or f.endswith(ext2) or f.endswith(ext3)]
for raster in raster:
product = path + "/" + raster
a = os.path.join(os.path.expandvars(home), product)
products.append(a)
#combinations_bands_sentinel(sentinel_image, Type_Merge, products)
return products
#Applying Index Flag=3 Search .dim files
#Find Sentinel Files
Files = tools("all") #IW_SLC,IW_GRDH,MSIL1C
#dir="C:\Users\Garantias723\Documents\Poncho\Image_Sentinel"
dir="make/"
platform='Sentinel-2'
list_files=Files.findFilesSentinel(dir,platform,2)#4 for resample_subset.dim
print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
#Read Sentinel Files
Read=readProduct(dir,platform)
#Applying Resampling
TypeMerge="RGB"
Mergee=Merge("60",TypeMerge)
for image in list_files:
#file = dir + "/" + image
file = dir + image
products=Mergee.search_band_combination(file, TypeMerge)
print"Applying SubSet... ", image
product=Read.ReadFilesSentinel(file,2)#The flag 2 ReadFiles .dim
Mergee.merges(product,products,file)
#Mergee.merges(product, file)
\ No newline at end of file
# -*- coding: utf-8 -*-
# @package Resample Sentinel Files
"""
Example:
$ python -m doctest -v Resampling.py
"""
import os,sys
#sys.path.append(os.path.join(os.path.expandvars("%userprofile%"),".snap\snap-python"))
from os.path import expanduser
home = expanduser("~")
a=os.path.join(os.path.expandvars(home),".snap/snap-python/")
print"SNAPPY..",a
sys.path.append(a)
import snappy
from snappy import HashMap,jpy,Product,ProductData,ProductIO,ProductUtils
from tools import (tools)
from readProduct import (readProduct)
class Resampling(object):
""" Class for Sentinel satellites configuration
Test Case
>>> Files = tools('MSIL1C')
>>> dir="make/"
>>> platform='Sentinel-2'
>>> list_files=Files.findFilesSentinel(dir,platform,2)
>>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
>>> Read=readProduct(dir,platform)
>>> Resam = Resampling("60")
>>> for image in list_files:
>>> file = dir + "/" + image
>>> print"Applying Resampling... ", image
>>> product=Read.ReadFilesSentinel(file,1)
>>> Resam.Resample(product, file)
"""
def __init__(self,ResolSize):
"""The constructor Initialize Sentinel Data.
Args:
self: The object pointer.
ResolSize (str): destination directory.
Returns:
pointer: The object pointer.
"""
self.resolution=ResolSize
self.Resampling = "Resample"
self.method="NEAREST_NEIGHBOUR"
self.HashMap = jpy.get_type('java.util.HashMap')
def Resample(self,product,file):
"""Resample Files Sentinel
Args:
self (pointer): The object pointer.
product (bin): Metadata Sentinel.
file (str): local path to image.
"""
params = self.HashMap()
params.put('targetResolution', self.resolution) # 10,20 or 60m
params.put('demResamplingMethod', self.method)
result = snappy.GPF.createProduct('Resample', params, product)
outputRes = file[:-5] + "_" + self.Resampling + "_" + self.resolution + ".dim"
ProductIO.writeProduct(result, outputRes, "BEAM-DIMAP")
\ No newline at end of file
# -*- coding: utf-8 -*-
# @package SubSet Sentinel Files
"""
Example:
$ python -m doctest -v SubSet.py
"""
import os,sys
from os.path import expanduser
home = expanduser("~")
a=os.path.join(os.path.expandvars(home),".snap/snap-python/")
sys.path.append(a)
import snappy
from snappy import HashMap,jpy,Product,ProductData,ProductIO,ProductUtils,GPF
from tools import (tools)
from readProduct import (readProduct)
class SubSet(object):
""" Class for Sentinel satellites configuration
Test Case
>>> Files = tools('MSIL1C')
>>> dir="make/"
>>> platform='Sentinel-2'
>>> list_files=Files.findFilesSentinel(dir,platform,3)
>>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
>>> Read=readProduct(dir,platform)
>>> widthX="552"
>>> heigthX="0"
>>> widthY="1764"
>>> heigthY="1080"
>>> SubSeet = SubSet("60", widthX, heigthX, widthY, heigthY)
>>> for image in list_files:
>>> file = dir + "/" + image
>>> print"Applying SubSet... ", image
>>> product=Read.ReadFilesSentinel(file,2)
>>> SubSeet.subSet(product, file)
"""
def __init__(self,ResolSize,widthX,heigthX,widthY,heigthY):
"""The constructor Initialize Sentinel Data.
Args:
self: The object pointer.
ResolSize (str): destination directory.
widthX (str): Scene start X
heigthX (str):Scene start Y
widthY (str): Scene end X
heigthY (str):Scene end Y
Returns:
pointer: The object pointer.
"""
self.resolution=ResolSize
self.Width_X=widthX
self.Heigth_X=heigthX
self.Width_Y=widthY
self.Heigth_Y=heigthY
self.Type_file="_SubSet"
self.HashMap = jpy.get_type('java.util.HashMap')
def subSet(self,product, file):
"""subSet Files Sentinel
Args:
self (pointer): The object pointer.
product (bin): Metadata Sentinel.
file (str): local path to image.
"""
params = self.HashMap()
params.put('copyMetadata', True)
params.put('region', "%s,%s,%s,%s" % (self.Width_X, self.Heigth_X, self.Width_Y, self.Heigth_Y))
subset = GPF.createProduct('Subset', params, product)
salida = file[:-4] + self.Type_file + ".dim"
ProductIO.writeProduct(subset, salida, "BEAM-DIMAP")
...@@ -5,7 +5,7 @@ Example: ...@@ -5,7 +5,7 @@ Example:
$ python -m doctest -v UnZip.py $ python -m doctest -v UnZip.py
""" """
import zipfile import zipfile,os
from tools import (tools) from tools import (tools)
class UnZip(object): class UnZip(object):
......
# -*- coding: utf-8 -*-
# @package readProduct Sentinel Files
"""
Example:
$ python -m doctest -v readProduct.py
"""
import os,sys
from os.path import expanduser
home = expanduser("~")
a=os.path.join(os.path.expandvars(home),".snap/snap-python/")
sys.path.append(a)
from snappy import Product,ProductData,ProductIO,ProductUtils
from tools import (tools)
class readProduct(object):
""" Class for Sentinel satellites configuration
Test Case
>>> Files = tools('MSIL1C')
>>> dir="make/"
>>> platform='Sentinel-2'
>>> list_files=Files.findFilesSentinel(dir,platform,2)
>>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
>>> Read=readProduct(dir,platform)
>>> for image in list_files:
>>> file = dir + "/" + image
>>> print"Reading image... ", image
>>> product=Read.ReadFilesSentinel(file,1)
>>> band_names = product.getBandNames()
>>> print("Bands: %s" % (list(band_names)))
"""
def __init__(self,folder,platform):
"""The constructor Initialize Sentinel Data.
Args:
self: The object pointer.
folder (str): destination directory.
platform (str): Sentinel-1 or Sentinel-2.
Returns:
pointer: The object pointer.
"""
self.dir=folder
self.platform=platform
def ReadFilesSentinel(self,file,flag):
"""Read Files Sentinel
Args:
self (pointer): The object pointer.
file (str): Sentinel Product list found.
flag (int): File Type to read .xml or .dim.
Returns:
sentinel (bin): Metadata Sentinel.
"""
# Extract information about the Sentinel product:
if(self.platform=="Sentinel-2"):
if (flag==1):
sentinel = ProductIO.readProduct(file + "/MTD_MSIL1C.xml")
if (flag==2):
sentinel = ProductIO.readProduct(file)
if (self.platform=="Sentinel-1"):
sentinel = ProductIO.readProduct(file + "/manifest.safe")
return sentinel
...@@ -5,7 +5,8 @@ Example: ...@@ -5,7 +5,8 @@ Example:
$ python -m doctest -v tools.py $ python -m doctest -v tools.py
""" """
import os import os,sys
class tools(object): class tools(object):
""" Class for Sentinel satellites configuration """ Class for Sentinel satellites configuration
...@@ -17,13 +18,12 @@ class tools(object): ...@@ -17,13 +18,12 @@ class tools(object):
>>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)' >>> print 'There is/are %d ' % len(list_files) + platform + ' file(s)'
""" """
def __init__(self,type): def __init__(self,type):
"""The constructor Initialize Sentinel Data. """The constructor Initialize Sentinel Data.
Args: Args:
self: The object pointer. self: The object pointer.
type (str): sentinel product type {all, SM_SLC, SM_GRDF, SM_GRDH, SM_GRDM, IW_SLC, IW_GRDH, IW_GDRM, IW_RAW, EW_SLC, EW_GRDH, EW_GRDM, WV_SLC, WV_GRDM, MSIL1C, OPER_PRD } type (str): sentinel product type {all, SM_SLC, SM_GRDF, SM_GRDH, SM_GRDM, IW_SLC, IW_GRDH, IW_GDRM, IW_RAW, EW_SLC, EW_GRDH, EW_GRDM, WV_SLC, WV_GRDM, MSIL1C, OPER_PRD }.
Returns: Returns:
pointer: The object pointer. pointer: The object pointer.
...@@ -35,7 +35,7 @@ class tools(object): ...@@ -35,7 +35,7 @@ class tools(object):
self.namfil4 = 'S2B_' self.namfil4 = 'S2B_'
def findFilesSentinel(self,dir,platform): def findFilesSentinel(self,dir,platform,flag):
"""Find Files Sentinel """Find Files Sentinel
Args: Args:
...@@ -47,29 +47,37 @@ class tools(object): ...@@ -47,29 +47,37 @@ class tools(object):
OrderedDict: Sentinel Product list found. OrderedDict: Sentinel Product list found.
""" """
if(flag==1):
ext = ".zip"
if(flag==2):
ext = ".SAFE"
if(flag==3):
ext="_Resample_60.dim"
if(flag==4):
ext="_Resample_60_SubSet.dim"
files = os.listdir(dir) files = os.listdir(dir)
if (self.product_type == "all"): if (self.product_type == "all"):
if platform == 'Sentinel-1' or platform == 'sentinel-1': if platform == "Sentinel-1":
self.namfil1 nam1=self.namfil1
self.namfil2 nam2=self.namfil2
files = [f for f in files if f.startswith(self.namfil1) or f.startswith(self.namfil2)] #files = [f for f in files if f.startswith(nam1) and f.endswith(ext) or f.startswith(nam2)]
if platform == 'Sentinel-2' or platform == 'sentinel-2': if platform == "Sentinel-2":
self.namfil3 nam1=self.namfil3
self.namfil4 nam2=self.namfil4
files = [f for f in files if f.startswith(self.namfil3) or f.startswith(self.namfil4)] files = [f for f in files if f.startswith(nam1) and f.endswith(ext) or f.startswith(nam2)]
if (len(files) == 0): if (len(files) == 0):
print"Files not found..." print"Files not found..."
sys.exit()
else: else:
if platform=="Sentinel-1" or platform=="sentinel-1": if platform=="Sentinel-1":
self.namfil1 = 'S1A_'+ self.product_type nam1 = self.namfil1 + self.product_type
self.namfil2 = 'S1B_'+ self.product_type nam2 = self.namfil2 + self.product_type
files = [f for f in files if f.startswith(self.namfil1) or f.startswith(self.namfil2)] #files = [f for f in files if f.startswith(nam1) and f.endswith(ext) or f.startswith(nam2)]
if platform=="Sentinel-2" or platform=="sentinel-2": if platform=="Sentinel-2":
self.namfil3 = 'S2A_'+ self.product_type nam1 = self.namfil3 + self.product_type
self.namfil4 = 'S2B_'+ self.product_type nam2 = self.namfil4 + self.product_type
files = [f for f in files if f.startswith(self.namfil3) or f.startswith(self.namfil4)] files = [f for f in files if f.startswith(nam1) and f.endswith(ext) or f.startswith(nam2)]
if (len(files) == 0): if (len(files) == 0):
print"Files not found..." print"Files not found..."
sys.exit()
return files return files
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