Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
GeoSentinel
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mario Chirinos Colunga
GeoSentinel
Commits
8ae0bb49
Commit
8ae0bb49
authored
Dec 16, 2018
by
Mario Chirinos Colunga
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
3fa28e6d
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
169 additions
and
153 deletions
+169
-153
.rasterWkt.py.swp
geosentinel/.rasterWkt.py.swp
+0
-0
L2ASCLtoJSON.py
geosentinel/L2ASCLtoJSON.py
+15
-3
rasterWkt.py
geosentinel/rasterWkt.py
+50
-123
rasterWkt.pyc
geosentinel/rasterWkt.pyc
+0
-0
L2ASCL_AreaProcessing.sh
tools/L2ASCL_AreaProcessing.sh
+21
-11
mergeImages.sh
tools/mergeImages.sh
+22
-8
mergeImages.sh.save
tools/mergeImages.sh.save
+49
-0
mergeImagesByDirectory.sh
tools/mergeImagesByDirectory.sh
+12
-8
No files found.
geosentinel/.rasterWkt.py.swp
deleted
100644 → 0
View file @
3fa28e6d
File deleted
geosentinel/L2ASCLtoJSON.py
View file @
8ae0bb49
...
@@ -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
):
...
...
geosentinel/rasterWkt.py
View file @
8ae0bb49
...
@@ -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
...
...
geosentinel/rasterWkt.pyc
0 → 100644
View file @
8ae0bb49
File added
tools/L2ASCL_AreaProcessing.sh
View file @
8ae0bb49
This diff is collapsed.
Click to expand it.
tools/mergeImages.sh
View file @
8ae0bb49
#!/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
...
...
tools/mergeImages.sh.save
0 → 100755
View file @
8ae0bb49
#!/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
..
tools/mergeImagesByDirectory.sh
View file @
8ae0bb49
#!/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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment