Skip to content

Commit 3adc6dd

Browse files
Iss14 (#30)
* Fixing the issue with subsetting LandSat GeoTIFF files Subsetting of LandSat GeoTIFF files failed because the projection of LandSat files is different from lat/lon values usually given as input. Therefore, a function was added to transform the lat/lon values to the projection of LandSat data (derived automatically) Furthermore, subsetting function was adjusted to accept the transformed lat/lon values as local variables. Reported-by: Kasra Keshavarz <[email protected]> Signed-off-by: Kasra Keshavarz <[email protected]> * bumping version to 0.1.4 * removing the duplicate line for specifying longitude limits variable * Correcting extent extraction from LandSat GeoTIFFs Since converting the extents of input Shapefiles to the Landsat's projection value resulted in spatially accurate GeoTIFFs, another solution was implemented to: 1) reproject the input ESRI Shapefile into the projection of the Landsat rasters and 2) extract the extents from the reprojected ESRI Shapefile. This results in accurate spatial subset of the Landsat landcover values. Furthermore, the `extract_extent_shapefile()` function has changed in a way that could be provided with the source projection where the extents are desired. If not provided, the source projection of the ESRI Shapefile input data is considered to extract the extents. Signed-off-by: Kasra Keshavarz <[email protected]> Reported-by: Kasra Keshavarz <[email protected]> --------- Signed-off-by: Kasra Keshavarz <[email protected]>
1 parent b4fe1df commit 3adc6dd

File tree

2 files changed

+102
-20
lines changed

2 files changed

+102
-20
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.3
1+
0.1.4

landsat/landsat.sh

+101-19
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ sort_comma_delimited () { IFS=',' read -ra arr <<< "$*"; echo ${arr[*]} | tr " "
165165
# log date format
166166
logDate () { echo "($(date +"%Y-%m-%d %H:%M:%S")) "; }
167167

168-
169168
#######################################
170169
# subset GeoTIFFs
171170
#
@@ -215,7 +214,6 @@ subset_geotiff () {
215214
> /dev/null;
216215
}
217216

218-
219217
#######################################
220218
# Extract ESRI Shapefile extents
221219
#
@@ -226,8 +224,10 @@ subset_geotiff () {
226224
# lat/lon system
227225
#
228226
# Arguments:
229-
# shapefilePath: Unix-style path to
230-
# an ESRI Shapefile
227+
# shapefilePath: path to the ESRI
228+
# Shapefile
229+
# destProj4: destination projection,
230+
# (optional)
231231
#
232232
# Outputs:
233233
# one mosaiced (merged) GeoTIFF under
@@ -242,26 +242,102 @@ extract_shapefile_extents () {
242242

243243
# reading arguments
244244
local shapefilePath=$1
245+
local destProj4=$2
245246

246-
# extract the shapefile extent
247-
IFS=' ' read -ra shapefileExtents <<< "$(ogrinfo -so -al "$shapefilePath" | sed 's/[),(]//g' | grep Extent)"
247+
# extract PROJ.4 string for $shapefilePath
248+
sourceProj4=$(ogrinfo -al -so $shapefilePath | grep -e "PROJ.4" 2>/dev/null)
249+
250+
# if $sourceProj4 is missing, assign EPSG:4326 as default value and warn
251+
if [[ -z $sourceProj4 ]]; then
252+
sourceProj4="EPSG:4326"
253+
echo "$(logDate)$(basename $0): WARNING! Assuming EPSG:4326 for the" \
254+
"input ESRI Shapefile to extract the extents"
255+
fi
256+
257+
# if $destProj4 provided, reproject and extract extent in the new
258+
# projection
259+
if [[ -n $destProj4 && "$destProj4" != "$sourceProj4" ]]; then
260+
# temporary shapefile's path
261+
tempShapefile="${cache}/temp_reprojected.shp"
262+
263+
# reproject ESRI shapefile to $destProj4
264+
ogr2ogr -f "ESRI Shapefile" ${tempShapefile} ${shapefilePath} -s_srs \
265+
"$sourceProj4" -t_srs "$destProj4"
248266

249-
# transform the extents in case they are not in EPSG:4326
250-
IFS=':' read -ra sourceProj4 <<< "$(gdalsrsinfo $shapefilePath | grep -e "PROJ.4")" # source Proj4 value
251-
if [[ -n $sourceProj4 ]]; then
252-
:
253-
else
254-
echo "$(logDate)$(basename $0): WARNING! Assuming WSG84 CRS for the input ESRI Shapefile"
255-
sourceProj4=("PROJ.4" " +proj=longlat +datum=WGS84 +no_defs") # made an array for compatibility with the following statements
267+
# assign the path of the projected file as the $shapefilePath
268+
shapefilePath="${tempShapefile}"
256269
fi
257270

271+
# extract the shapefile extent
272+
IFS=' ' read -ra shapefileExtents <<< "$(ogrinfo -so -al "$shapefilePath" | sed 's/[),(]//g' | grep Extent)"
273+
258274
# transform limits and assigning to relevant variables
259-
IFS=' ' read -ra leftBottomLims <<< $(echo "${shapefileExtents[@]:1:2}" | gdaltransform -s_srs "${sourceProj4[1]}" -t_srs EPSG:4326 -output_xy)
260-
IFS=' ' read -ra rightTopLims <<< $(echo "${shapefileExtents[@]:4:5}" | gdaltransform -s_srs "${sourceProj4[1]}" -t_srs EPSG:4326 -output_xy)
275+
IFS=' ' read -ra lowerLeftLims <<< $(echo "${shapefileExtents[@]:1:2}")
276+
IFS=' ' read -ra upperRightLims <<< $(echo "${shapefileExtents[@]:4:5}")
261277

262278
# define $latLims and $lonLims from $shapefileExtents
263-
lonLims="${leftBottomLims[0]},${rightTopLims[0]}"
264-
latLims="${leftBottomLims[1]},${rightTopLims[1]}"
279+
lonLims="${lowerLeftLims[0]},${upperRightLims[0]}"
280+
latLims="${lowerLeftLims[1]},${upperRightLims[1]}"
281+
}
282+
283+
#######################################
284+
# Transform projection based on source
285+
# Proj4 string
286+
#
287+
# Globals:
288+
# None
289+
#
290+
# Arguments:
291+
# sourceProj4: string describing
292+
# source projection
293+
# coords: comma-separated coordinate
294+
# values
295+
# coordsDelim: delimited in the
296+
# $coords variable
297+
# transformDelim: delimtied in the
298+
# transformed values
299+
# destEPSG: EPSG value of the
300+
# destination projection
301+
# (default 'EPSG:4326')
302+
#
303+
# Outputs:
304+
# comma-delimited $coords in the
305+
# $destEPSG format
306+
#######################################
307+
transform_coords () {
308+
# local variables
309+
local sourceProj4="$1"
310+
local coords="$2"
311+
local coordsDelim="$3"
312+
local transformDelim="$4"
313+
local destEPSG="$5"
314+
# local variables assigned in the function
315+
local coordsBlankDel
316+
local coordsTrans
317+
318+
# if $destEPSG not provided, use EPSG:4326 by default
319+
if [[ -z $destEPSG ]]; then
320+
destEPSG='EPSG:4326'
321+
fi
322+
323+
# if $coordsDelim not provided, use comma ',' by default
324+
if [[ -z $coordsDelim ]]; then
325+
coordsDelim=','
326+
fi
327+
328+
# substituting comma with a blank space
329+
coordsBlankDel=$(echo "$coords" | tr "${coordsDelim}" ' ')
330+
331+
# transforming coords
332+
coordsTrans=$(echo "$coordsBlankDel" | gdaltransform -s_srs "${sourceProj4}" -t_srs "${destEPSG}" -output_xy)
333+
334+
# subtitute blank space with $transformDelim value
335+
if [[ -n $transformDelim ]]; then
336+
coordsTrans=$(echo "$coordsTrans" | tr ' ' $transformDelim)
337+
fi
338+
339+
# echo-ing $coordsTrans variable
340+
echo "${coordsTrans}"
265341
}
266342

267343

@@ -345,18 +421,24 @@ for tif in $cache/*.tif; do
345421
tiffs+=($(basename "${tif}"))
346422
done
347423

424+
# extracting raster projection PROJ.4 string value
425+
tempTif="${cache}/${tiffs[0]}"
426+
rasterProj4="$(gdalsrsinfo "${tempTif}" | grep -e "PROJ.4" | cut -d ':' -f2)"
427+
348428
# if shapefile is provided extract the extents from it
349429
if [[ -n $shapefile ]]; then
350430
# create latLims and lonLims variables specifying the limits of the ESRI Shapefile
351-
extract_shapefile_extents "${shapefile}"
431+
extract_shapefile_extents "${shapefile}" "${rasterProj4}"
352432
fi
353433

354434
# subset and produce stats if needed
355435
if [[ "${printGeotiff,,}" == "true" ]]; then
436+
# echo message
356437
echo "$(logDate)$(basename $0): subsetting GeoTIFFs under $outputDir"
438+
357439
for tif in "${tiffs[@]}"; do
358440
# subset based on lat and lon values
359-
subset_geotiff "${cache}/${tif}" "${outputDir}/${prefix}${tif}"
441+
subset_geotiff "${cache}/${tif}" "${outputDir}/${prefix}${tif}" "$latLims" "$lonLims"
360442
done
361443
fi
362444

0 commit comments

Comments
 (0)