forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
footprint.sh
executable file
·112 lines (112 loc) · 3.04 KB
/
footprint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#
# ***************************************************************************
# Name : Footprint
# Description : Create footprint of image with GeoJSON format
#
# Arguments:
# $1: Image
#
# Dependencies : gdal 1.10.1(gdal_calc.py, gdal_sieve.py, gdal_edit.py and gdal_polygonize.py, ogr2ogr)
#
# ***************************************************************************
# begin : 2015-03-02 (yyyy-mm-dd)
# copyright : (C) 2015 by Luiz Motta
# email : motta dot luiz at gmail.com
# ***************************************************************************
#
# Revisions
#
# 2016-03-31:
# - Create multipolygon, added a field of total of parts
#
# ***************************************************************************
#
# Example:
# footprint.sh LC8_229-066_20140724_LGN00_r6g5b4.tif
#
# ***************************************************************************
# * *
# * This program 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 2 of the License, or *
# * (at your option) any later version. *
# * *
# ***************************************************************************
#
msg_error(){
local name_script=$(basename $0)
echo "Usage: $name_script <image> <outsize>" >&2
echo "<image> is the image for calculate footprint" >&2
exit 1
}
#
totalargs=1
#
if [ $# -ne $totalargs ] ; then
msg_error
exit 1
fi
#
in_img=$1
#
if [ ! -f "$in_img" ]; then
echo "The file '$in_img' not exist" >&2
exit 1
fi
#
dir_img=$(dirname $in_img)
basename_img=$(basename $in_img)
name_img=${basename_img%.*}
#
footprint_geojson=$dir_img"/"$name_img".geojson"
zero_one_img=$dir_img"/"$name_img"_0x1.tif"
sieve_img=$dir_img"/"$name_img"_0x1_sieve.tif"
#
if [ -f $footprint_geojson ]; then
rm $footprint_geojson
fi
# Processing
printf "'$name_img'...1"
gdal_calc.py -A $in_img --A_band 1 --type Byte --calc "A>0" --outfile $zero_one_img >/dev/null
#
code=$?
if [ "$code" != 0 ]
then
exit $code
fi
#
printf ".2"
gdal_sieve.py -q -st 100 -8 $zero_one_img -nomask $sieve_img
code=$?
if [ "$code" != 0 ]
then
exit $code
fi
#
gdal_edit.py -a_nodata 0 $sieve_img
code=$?
if [ "$code" != 0 ]
then
exit $code
fi
#
printf ".3"
gdal_polygonize.py $sieve_img -q -b 1 -f "GeoJSON" $footprint_geojson
code=$?
if [ "$code" != 0 ]
then
exit $code
fi
#
tmp_file=$footprint_geojson".tmp"
ogr2ogr -dialect SQLITE -sql "SELECT DN, ST_Union( geometry ), COUNT( 1 ) as total_part FROM OGRGeoJSON WHERE DN = 1" -f "GeoJSON" $tmp_file $footprint_geojson
mv -f $tmp_file $footprint_geojson
#
ssed="s|\"DN\": 1,|\"path\": \"$dir_img\", \"image\": \"$basename_img\",|"
sed -i "$ssed" "$footprint_geojson"
printf "\rCreated: %-10s\n" $footprint_geojson
# Cleanup
rm $zero_one_img $sieve_img
#
exit 0