Commit c04a11c1 authored by Pedro Alfonso Ramirez's avatar Pedro Alfonso Ramirez

Add get RGB sentinel-2 files

parent 7dc7f8fb
#!/bin/python
# Copyright (C) 2018 Adan Salazar <asalazargaribay@gmail.com>
#
#
# This file is part of GeoSentinel
#
#
# GeoSentinel is free software you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GeoSentinel is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with StereoVision. If not, see <http://www.gnu.org/licenses/>.
"""
Example of combination raster in Sentinel files in a specific directory
"""
from argparse import ArgumentParser
from geosentinel.ui_utils import (find_files_Sentinel, find_files_byext_Sentinel,search_band_combination)
from geosentinel.arguments import SENTINEL_COMBINATION_ARGUMENTS
def main():
"""
Get combination raster all Sentinel files in the folder specified by the user.
Please run: python unzip_sentinel_files --help before usage
"""
parser = ArgumentParser(description="Combination raster in Sentinel files in the folder specified by the user",
parents=[SENTINEL_COMBINATION_ARGUMENTS])
args = parser.parse_args()
list_files = find_files_Sentinel(args)
list_files_byext=find_files_byext_Sentinel(list_files,args)
search_band_combination(list_files_byext,args)
if __name__ == "__main__":
main()
......@@ -56,3 +56,14 @@ SENTINEL_UNZIP_ARGUMENTS.add_argument("folder", help="Folder to find Sentinel fi
SENTINEL_UNZIP_ARGUMENTS.add_argument("plattform", help="Sentinel-1 or Sentinel-2")
SENTINEL_UNZIP_ARGUMENTS.add_argument("product_type", help="sentinel product type {all, SM_SLC, SM_GRDF, SM_GRDH, SM_GRDM, IW_SLC, IW_GRDH, IW_GDRM, EW_SLC, EW_GRDH, EW_GRDM, WV_SLC, WV_GRDM, MSIL1C, OPER_PRD }")
SENTINEL_COMBINATION_ARGUMENTS = ArgumentParser(add_help=False)
SENTINEL_COMBINATION_ARGUMENTS.add_argument("folder", help="Folder to find Sentinel files")
SENTINEL_COMBINATION_ARGUMENTS.add_argument("plattform", help="Sentinel-1 or Sentinel-2")
SENTINEL_COMBINATION_ARGUMENTS.add_argument("product_type", help="sentinel product type {all, SM_SLC, SM_GRDF, SM_GRDH, SM_GRDM, IW_SLC, IW_GRDH, IW_GDRM, EW_SLC, EW_GRDH, EW_GRDM, WV_SLC, WV_GRDM, MSIL1C, OPER_PRD }")
SENTINEL_COMBINATION_ARGUMENTS.add_argument("product_output_type", help="sentinel product type {RGB}")
\ No newline at end of file
......@@ -32,7 +32,9 @@ Functions:
* "check_existing_directory" - Check out if the folder exist. Create folder if it does not exist
* "check_existing_productType" - Check args if exist
* "select_products" - # Select products that their intersection area is less than a certain percentage threshold
* "combinations_bands_sentinel" - Get combination bands such product
* "search_band_combination" - Get path and bands for combination such product
* "find_files_byext_Sentinel" - Discover Sentinel files by extension
"""
#from argparse import ArgumentParser
......@@ -44,12 +46,16 @@ from collections import OrderedDict
from osgeo import ogr
from osgeo import gdal
from osgeo.gdalconst import *
from sentinelsat.sentinel import SentinelAPI #read_geojson, geojson_to_wkt
from geosentinel.products import SentinelData
def check_existing_productType(product,flag):
def check_existing_productType(product,args,flag):
if(flag==1):
product_type=['Sentinel-1', 'sentinel-1', 'Sentinel-2', 'sentinel-2']
findProduct = product in product_type
......@@ -57,16 +63,90 @@ def check_existing_productType(product,flag):
print product + " not exist"
sys.exit()
if(flag==2):
product_type=["all", "SM_SLC", "SM_GRDF", "SM_GRDH", "SM_GRDM", "IW_SLC", "IW_GRDH", "IW_GDRM", "EW_SLC", "EW_GRDH", "EW_GRDM", "WV_SLC", "WV_GRDM", "MSIL1C", "OPER_PRD"]
product_type=["all", "SM_SLC", "SM_GRDF", "SM_GRDH", "SM_GRDM", "IW_SLC", "IW_GRDH", "IW_GDRM", "EW_SLC", "EW_GRDH", "EW_GRDM", "WV_SLC", "WV_GRDM", "MSIL1C", "OPER_PRD","RGB"]
findProduct = product in product_type
if not findProduct:
print product + " not exist"
sys.exit()
if(flag==3):
ext="_RGB.tif"
product = product[:-5]+ext
product_type = os.listdir(args.folder)
findProduct = product in product_type
return findProduct
def combinations_bands_sentinel(sentinel_image,args,products):
TotImage=len(products)
#Get Raster
dataset1 = gdal.Open(products[2], GA_ReadOnly)
dataset2 = gdal.Open(products[1], GA_ReadOnly)
dataset3 = gdal.Open(products[0], GA_ReadOnly)
#Get metadata raster
projectionfrom = dataset1.GetProjection()
geotransform = dataset1.GetGeoTransform()
band1 = dataset1.GetRasterBand(1)
xsize = band1.XSize
ysize = band1.YSize
datatype1 = band1.DataType
band2 = dataset2.GetRasterBand(1)
datatype2 = band2.DataType
band3 = dataset3.GetRasterBand(1)
datatype3 = band3.DataType
#Reading the raster values
gtiff = gdal.GetDriverByName('GTiff')
output_file=sentinel_image[:-5]+"_"+args.product_output_type+".tif"
values1 = band1.ReadRaster( 0, 0, xsize, ysize, xsize, ysize, datatype1 )
values2 = band2.ReadRaster( 0, 0, xsize, ysize, xsize, ysize, datatype2 )
values3 = band3.ReadRaster( 0, 0, xsize, ysize, xsize, ysize, datatype3 )
#Combination bands
output_dataset = gtiff.Create(output_file, xsize, ysize, TotImage,GDT_Int16)
output_dataset.SetProjection(projectionfrom)
output_dataset.SetGeoTransform(geotransform)
output_dataset.GetRasterBand(1).WriteRaster( 0, 0, xsize, ysize, values1 )
output_dataset.GetRasterBand(2).WriteRaster( 0, 0, xsize, ysize, values2 )
output_dataset.GetRasterBand(3).WriteRaster( 0, 0, xsize, ysize, values3 )
output_dataset = None
def search_band_combination(list_files_byext,args):
for image in list_files_byext:
sentinel_image=args.folder+"\\"+image
findProduct=check_existing_productType(image,args, 3)
if findProduct==True:
print args.product_output_type+ " was applied to image... "+image
else:
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 args.product_output_type=="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
products.append(product)
print"Applying "+args.product_output_type+" to the image... ",image
combinations_bands_sentinel(sentinel_image,args,products)
def find_files_byext_Sentinel(files,args):
findProduct=check_existing_productType(args.product_output_type,args, 2)
if args.product_output_type == "RGB":
if args.plattform == 'Sentinel-2' or args.plattform == 'sentinel-2':
ext=".SAFE"
files = [f for f in files if f.endswith(ext)]
if (len(files) == 0):
print"Files not found..."
return files
def find_files_Sentinel(args):
check_existing_directory(args.folder, 2)
check_existing_productType(args.plattform, 1)
check_existing_productType(args.product_type, 2)
findProduct=check_existing_productType(args.plattform,args, 1)
findProduct=check_existing_productType(args.product_type,args, 2)
files = os.listdir(args.folder)
if (args.product_type == "all"):
if args.plattform == 'Sentinel-1' or args.plattform == 'sentinel-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