Commit 8ae0bb49 authored by Mario Chirinos Colunga's avatar Mario Chirinos Colunga 💬

update

parent 3fa28e6d
...@@ -7,18 +7,30 @@ import gdal ...@@ -7,18 +7,30 @@ import gdal
import sys import sys
import json import json
from collections import Counter from collections import Counter
sys.path.append('/home/mario/git/GeoSentinel')
from geosentinel import rasterWkt
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def L2ASCLtoDict(filename, wkt): def L2ASCLtoDict(filename, wkt):
''' '''
L2ASCLtoDict L2ASCLtoDict
''' '''
intput = gdal.Open(filename) inputImage = gdal.Open(filename)
rows, cols, geotransform = intput.RasterYSize, intput.RasterXSize, intput.GetGeoTransform() rows, cols, geotransform = inputImage.RasterYSize, inputImage.RasterXSize, inputImage.GetGeoTransform()
data = intput.GetRasterBand(1).ReadAsArray(0,0,cols,rows) data = inputImage.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
# count = Counter([])
# for r in data:
# count = count + Counter(r)
# sclDict = {str(k):v for k,v in dict(count).items()}
# print ("data")
# print (sclDict)
data = np.bitwise_and(data, rasterWkt.getPolygonArray(inputImage, wkt))
count = Counter([]) count = Counter([])
for r in data: for r in data:
count = count + Counter(r) count = count + Counter(r)
sclDict = {str(k):v for k,v in dict(count).items()} sclDict = {str(k):v for k,v in dict(count).items()}
# print ("data & POLY")
# print (sclDict)
return sclDict return sclDict
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def main(argv): def main(argv):
......
...@@ -6,86 +6,55 @@ import osgeo.osr as osr ...@@ -6,86 +6,55 @@ import osgeo.osr as osr
import numpy as np import numpy as np
import gdal import gdal
import sys import sys
#------------------------------------------------------------------------------- from collections import Counter
def createLayer(wkt, layerName="wkt"):
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
layer = data_source.CreateLayer(layerName, srs, ogr.wkbPolygon)
# Create the point from the Well Known Text
geometry = ogr.CreateGeometryFromWkt(wkt)
# create the feature
feature = ogr.Feature(layer.GetLayerDefn())
# Set the feature geometry using the point
feature.SetGeometry(geometry)
# Create the feature in the layer (shapefile)
layer.CreateFeature(feature)
# Dereference the feature
feature = None
return layer
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def getPolygonArray(image, wkt_geom): def getPolygonArray(image, wkt_geom):
rows, cols, geotransform = image.RasterYSize, image.RasterXSize, image.GetGeoTransform() rows = image.RasterYSize
print(rows, cols, geotransform) cols = image.RasterXSize
sr = osr.SpatialReference(wkt=image.GetProjection()) geotransform = image.GetGeoTransform()
# sr.ImportFromWkt(image.GetProjection()) projection = image.GetProjection()
sr = osr.SpatialReference(wkt=projection)
# shapefile
# driverShp = ogr.GetDriverByName("ESRI Shapefile") sr4326 = osr.SpatialReference()
# data_source = driverShp.CreateDataSource("myShape.shp") sr4326.ImportFromEPSG(4326)
# srs = osr.SpatialReference() driverMEM = gdal.GetDriverByName('MEM')
# srs.ImportFromEPSG(4326) driverMemory = ogr.GetDriverByName('Memory')
# layer = data_source.CreateLayer("wkt", sr, ogr.wkbPolygon) driverTiff = gdal.GetDriverByName('GTiff')
# geometry = ogr.CreateGeometryFromWkt(wkt_geom) driverShape = ogr.GetDriverByName("ESRI Shapefile")
# feature = ogr.Feature(layer.GetLayerDefn())
#RASTER
# feature.SetStyleString("PEN(c:#FF0000,w:5px);") target_ds = driverMEM.Create('raster.tif', cols, rows, 1, gdal.GDT_Byte) #MEM
# feature.SetGeometry(geometry) target_ds.GetRasterBand(1).SetNoDataValue(0)
# layer.CreateFeature(feature) target_ds.GetRasterBand(1).WriteArray(np.zeros((rows, cols), dtype=np.uint8))
target_ds.SetProjection(projection)
# data_source = None target_ds.SetGeoTransform(geotransform)
target_ds.GetRasterBand(1).FlushCache()
rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('out')
rast_mem_lyr = rast_ogr_ds.CreateLayer("wkt", sr, ogr.wkbPolygon) #VECTOR
# rast_mem_lyr = rast_ogr_ds.CreateLayer( 'poly', srs=sr ) 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 = ogr.Feature( rast_mem_lyr.GetLayerDefn() )
feat.SetGeometry(ogr.CreateGeometryFromWkt(wkt_geom)) feat.SetGeometry( ogr.CreateGeometryFromWkt(wkt_geom) )
# feat.SetGeometryDirectly( ogr.Geometry(wkt = wkt_geom) )
rast_mem_lyr.CreateFeature( feat ) rast_mem_lyr.CreateFeature( feat )
# Run the algorithm.
# err = gdal.RasterizeLayer( target_ds, [3,2,1], rast_mem_lyr, burn_values = [200,220,240] )
#Set up polygon raster
driverTiff = gdal.GetDriverByName('GTiff')
polygonRaster = driverTiff.Create("polygonTmp.tif", cols, rows, 1, gdal.GDT_Byte)
polygonRaster.GetRasterBand(1).SetNoDataValue(-99)
polygonRaster.GetRasterBand(1).WriteArray(np.zeros((rows, cols)))
polygonRaster.GetRasterBand(1).FlushCache()
polygonRaster.SetProjection(image.GetProjectionRef())
polygonRaster.SetGeoTransform(image.GetGeoTransform())
err = gdal.RasterizeLayer(target_ds, [1], rast_mem_lyr, burn_values=[255])
#Burn Polygon
err = gdal.RasterizeLayer(polygonRaster, [1], rast_mem_lyr, burn_values=[255])
if err != 0: if err != 0:
print(err) print(err)
gdaltest.post_reason( 'got non-zero result code from RasterizeLayer' ) gdaltest.post_reason( 'got non-zero result code from RasterizeLayer' )
return 'fail' return 'fail'
polygonRaster.GetRasterBand(1).FlushCache() target_ds.GetRasterBand(1).FlushCache()
np_polygon = target_ds.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
np_polygon = polygonRaster.GetRasterBand(1).ReadAsArray(0,0,cols,rows) count = Counter([])
# for r in np_polygon:
rast_ogr_ds = None # count = count + Counter(r)
# data_source = None # colorDict = {str(k):v for k,v in dict(count).items()}
polygonRaster = None # print(colorDict)
target_ds = None
return np_polygon return np_polygon
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
...@@ -93,74 +62,32 @@ def rasterWkt(wkt, inputfile, outputfile): ...@@ -93,74 +62,32 @@ def rasterWkt(wkt, inputfile, outputfile):
''' '''
Draw WKT Polygon into a Image Draw WKT Polygon into a Image
''' '''
print (wkt)
# return
print (inputfile)
print (outputfile)
inputImage = gdal.Open(inputfile) inputImage = gdal.Open(inputfile)
rows, cols, geotransform = inputImage.RasterYSize, inputImage.RasterXSize, inputImage.GetGeoTransform() rows = inputImage.RasterYSize
cols = inputImage.RasterXSize
geotransform = inputImage.GetGeoTransform()
projection = inputImage.GetProjection()
sr = osr.SpatialReference() sr = osr.SpatialReference()
sr.ImportFromWkt(inputImage.GetProjection()) sr.ImportFromWkt(projection)
# Read the input bands as numpy arrays.
print("driver")
driverTiff = gdal.GetDriverByName('GTiff') driverTiff = gdal.GetDriverByName('GTiff')
output = driverTiff.Create(outputfile, cols, rows, inputImage.RasterCount, gdal.GDT_Byte) output = driverTiff.Create(outputfile, cols, rows, inputImage.RasterCount, gdal.GDT_Byte)
# polygonRaster = driverTiff.Create("polygonTmp.tif", cols, rows, 1, gdal.GDT_Byte)
# #Set up polygon raster
# polygonRaster.GetRasterBand(1).SetNoDataValue(-99)
# polygonRaster.GetRasterBand(1).WriteArray(np.zeros((rows, cols)))
# polygonRaster.GetRasterBand(1).FlushCache()
# polygonRaster.SetGeoTransform(geotransform)
# polygonRaster.SetGeoTransform(geotransform)
# polygonRaster.SetProjection(sr.ExportToWkt())
# print("Shapefile")
# # shapefile
# driver = ogr.GetDriverByName("ESRI Shapefile")
# data_source = driver.CreateDataSource("myShape.shp")
# print("Burn Polygon")
# #Burn Polygon
# srs = osr.SpatialReference()
# srs.ImportFromEPSG(4326)
# layer = data_source.CreateLayer("wkt", srs, ogr.wkbPolygon)
# geometry = ogr.CreateGeometryFromWkt(wkt)
# feature = ogr.Feature(layer.GetLayerDefn())
# print("features")
# feature.SetStyleString("PEN(c:#FF0000,w:5px);")
# feature.SetGeometry(geometry)
# layer.CreateFeature(feature)
# gdal.RasterizeLayer(polygonRaster, [1], layer, burn_values=[255])
# polygonRaster.GetRasterBand(1).FlushCache()
## Draw output image
# np_polygon = polygonRaster.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
np_polygon = getPolygonArray(inputImage, wkt) np_polygon = getPolygonArray(inputImage, wkt)
print("Setup")
for b in range(1, output.RasterCount+1): for b in range(1, output.RasterCount+1):
np_input = inputImage.GetRasterBand(b).ReadAsArray(0,0,cols,rows) np_input = inputImage.GetRasterBand(b).ReadAsArray(0,0,cols,rows)
output.GetRasterBand(b).SetNoDataValue(-99) output.GetRasterBand(b).SetNoDataValue(0)
output.GetRasterBand(b).WriteArray(np_polygon) output.GetRasterBand(b).WriteArray(np.bitwise_and(np_polygon, np_input))
output.GetRasterBand(b).FlushCache() output.GetRasterBand(b).FlushCache()
# output.SetGeoTransform(geotransform) output.SetGeoTransform(geotransform)
# output.SetProjection(sr.ExportToWkt()) output.SetProjection(projection)
output.SetProjection(inputImage.GetProjectionRef())
output.SetGeoTransform(inputImage.GetGeoTransform())
print("end")
#Close files #Close files
# feature = None polygonRaster = None
# data_source = None
# polygonRaster = None
output = None output = None
inputImage = None inputImage = None
......
This diff is collapsed.
#!/bin/sh #!/bin/sh
DIR=$1 #Output filename INDIR=$1 #Output filename
POLYGON=$2 #Crop Window OUTDIR=$2 #Output filename
POLYGON=$3 #Crop Window
DIRNAME=$(echo $INDIR | cut -d"/" -f1)
echo "INDIR:" $INDIR
echo "OUTDIR:" $OUTDIR
#echo $POLYGON
cd $INDIR
#if [ ! -d $OUTDIR ]; then
# mkdir $OUTDIR
#fi
#BOX=$(polygonToBox.py "$POLYGON") #BOX=$(polygonToBox.py "$POLYGON")
BOX=$POLYGON BOX=$POLYGON
##------------------------------------------------------------------------------ ##------------------------------------------------------------------------------
cd $DIR
DIRNAME=$(echo $DIR | cut -d"/" -f1)
echo $DIRNAME echo $DIRNAME
for f in $(ls); do echo ${f}; done;
#exit
##------------------------------------------------------------------------------ ##------------------------------------------------------------------------------
MERGEDIMAGE=$DIRNAME"_TCI_60m_merged.tif" MERGEDIMAGE=$DIRNAME"_TCI_60m_merged.tif"
if [ ! -e $MERGEDIMAGE ]; then if [ ! -e $MERGEDIMAGE ]; then
...@@ -17,12 +30,13 @@ else ...@@ -17,12 +30,13 @@ else
echo PASSING $MERGEDIMAGE FOUND echo PASSING $MERGEDIMAGE FOUND
fi fi
CROPEDIMAGE=../merge_out/$DIRNAME"_TCI_60m.tif" CROPEDIMAGE=$OUTDIR$DIRNAME"_TCI_60m.tif"
if [ ! -e $CROPEDIMAGE ]; then if [ ! -e $CROPEDIMAGE ]; then
gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of JPEG $MERGEDIMAGE $CROPEDIMAGE gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of GTiff $MERGEDIMAGE $CROPEDIMAGE
else else
echo PASSING $CROPEDIMAGE FOUND echo PASSING $CROPEDIMAGE FOUND
fi fi
##------------------------------------------------------------------------------ ##------------------------------------------------------------------------------
MERGEDIMAGE=$DIRNAME"_SCL_60m_merged.tif" MERGEDIMAGE=$DIRNAME"_SCL_60m_merged.tif"
if [ ! -e $MERGEDIMAGE ]; then if [ ! -e $MERGEDIMAGE ]; then
...@@ -32,9 +46,9 @@ else ...@@ -32,9 +46,9 @@ else
echo PASSING $MERGEDIMAGE FOUND echo PASSING $MERGEDIMAGE FOUND
fi fi
CROPEDIMAGE=../merge_out/$DIRNAME"_SCL_60m.tif" CROPEDIMAGE=$OUTDIR$DIRNAME"_SCL_60m.tif"
if [ ! -e $CROPEDIMAGE ]; then if [ ! -e $CROPEDIMAGE ]; then
gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of JPEG $MERGEDIMAGE $CROPEDIMAGE gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of GTiff $MERGEDIMAGE $CROPEDIMAGE
else else
echo PASSING $CROPEDIMAGE FOUND echo PASSING $CROPEDIMAGE FOUND
fi fi
......
#!/bin/sh
DIR=$1 #Output filename
POLYGON=$2 #Crop Window
echo $DIR
echo $POLYGON
#BOX=$(polygonToBox.py "$POLYGON")
BOX=$POLYGON
##------------------------------------------------------------------------------
cd $DIR
DIRNAME=$(echo $DIR | cut -d"/" -f1)
echo $DIRNAME
##------------------------------------------------------------------------------
MERGEDIMAGE=$DIRNAME"_TCI_60m_merged.tif"
if [ ! -e $MERGEDIMAGE ]; then
echo "gdal_merge.py -o "$MERGEDIMAGE $(ls *TCI_10m.jp2)
gdal_merge.py -o $MERGEDIMAGE $(ls *TCI_10m.jp2)
else
echo PASSING $MERGEDIMAGE FOUND
fi
CROPEDIMAGE=../merge_out/$DIRNAME"_TCI_60m.jp2"
if [ ! -e $CROPEDIMAGE ]; then
gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of JPEG $MERGEDIMAGE $CROPEDIMAGE
else
echo PASSING $CROPEDIMAGE FOUND
fi
##------------------------------------------------------------------------------
MERGEDIMAGE=$DIRNAME"_SCL_60m_merged.tif"
if [ ! -e $MERGEDIMAGE ]; then
echo "gdal_merge.py -o "$MERGEDIMAGE $(ls *SCL_60m.jp2)
gdal_merge.py -o $MERGEDIMAGE $(ls *SCL_60m.jp2)
else
echo PASSING $MERGEDIMAGE FOUND
fi
CROPEDIMAGE=../merge_out/$DIRNAME"_SCL_60m.jp2"
if [ ! -e $CROPEDIMAGE ]; then
gdal_translate -projwin $BOX -projwin_srs WGS84 -ot Byte -of JPEG $MERGEDIMAGE $CROPEDIMAGE
else
echo PASSING $CROPEDIMAGE FOUND
fi
cd ..
#!/bin/sh #!/bin/sh
JP2DIR=$1 #JP2 Directory JP2DIR=$1 #JP2 Directory
POLYGON=$2 #Crop Window OUTDIR=$2
JOBS=${3:-1} POLYGON=$3 #Crop Window
JOBS=${4:-1}
DIRNAME=$(echo $JP2DIR | cut -d"/" -f1)
#echo "POLYGON"
#echo $POLYGON
BOX=$(polygonToBox.py "$POLYGON") BOX=$(polygonToBox.py "$POLYGON")
if [ "$#" -le 1 ]; then if [ "$#" -le 1 ]; then
echo "Usage " $0 " <Parent Directory with subdirectories> <POLYGON> [jobs]" echo "Usage " $0 " <Parent Directory with subdirectories> <POLYGON> [jobs]"
exit exit
fi fi
echo "cd " $JP2DIR
cd $JP2DIR cd $JP2DIR
mkdir merge_out for f in $(ls); do echo ${f}; done;
ls -d */ | parallel -q --jobs $JOBS mergeImages.sh {} "$BOX"
ls -d */ | parallel -q --jobs $JOBS mergeImages.sh {} $OUTDIR "$BOX"
#ls -d */ | parallel -q --jobs $JOBS test.sh {} ../../{} "$BOX"
find . -name *.xml -type f -delete
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