-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_max.py
executable file
·59 lines (49 loc) · 1.78 KB
/
raster_max.py
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
################################################################################
# Script: raster_max.py
# Author: Scott Hatcher
# Date: 2016-09-13
# Purpose: This script takes as input any raster format that can be read by GDAL,
# and reports the index/indices of the max pixel value for all bands
# in that raster.
################################################################################
from osgeo import gdal
import numpy as np
import sys
# Let's GDAL throw Python exceptions
gdal.UseExceptions()
def Usage():
print("""
Usage:
$ python raster_max.py input-raster-path
e.g. python raster_max.py /home/user/test.tif
""")
sys.exit(1)
def main( filepath ):
try:
src_ds = gdal.Open( filepath )
except RuntimeError, e:
print 'Unable to open ' + filepath
print e
sys.exit(1)
print "[Raster band count]: ", src_ds.RasterCount
try:
for band in range( src_ds.RasterCount ):
band += 1
print "[Working on band]: ", band
src_band = src_ds.GetRasterBand(band)
if src_band is None:
continue
src_array = src_band.ReadAsArray().astype(np.float) # Assumed 32-bit float, which may slow it down in some cases
(y_inds, x_inds) = np.nonzero(src_array == np.max(src_array))
print "[Indices of max value for band " + str(band) + " (format: '[(x1,y1),(x2,y2),...]']:\n" + repr(zip(x_inds, y_inds))
except RuntimeError, e:
print 'Failed processing on raster'
print e
sys.exit(1)
if __name__ == '__main__':
if len( sys.argv ) > 2:
print """
[ ERROR ] the script can only accept a single input raster
"""
Usage()
main( str(sys.argv[1]) )