forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.py
executable file
·184 lines (130 loc) · 6.91 KB
/
histogram.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
###############################################################################
# $Id: colortable.py 11065 2007-03-24 09:35:32Z mloskot $
#
# Project: GDAL/OGR Test Suite
# Purpose: Test GetHistogram() and GetDefaultHistogram() handling.
# Author: Frank Warmerdam <[email protected]>
#
###############################################################################
# Copyright (c) 2008, Frank Warmerdam <[email protected]>
# Copyright (c) 2009, Even Rouault <even dot rouault at mines-paris dot org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import os
import sys
import shutil
sys.path.append('../pymod')
from osgeo import gdal
import gdaltest
###############################################################################
# Fetch simple histogram.
def histogram_1():
ds = gdal.Open('data/utmsmall.tif')
hist = ds.GetRasterBand(1).GetHistogram()
exp_hist = [2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 0, 0, 0, 0, 281, 0, 0, 0, 0, 0, 0, 0, 0, 365, 0, 0, 0, 0, 0, 0, 0, 460, 0, 0, 0, 0, 0, 0, 0, 533, 0, 0, 0, 0, 0, 0, 0, 544, 0, 0, 0, 0, 0, 0, 0, 0, 626, 0, 0, 0, 0, 0, 0, 0, 653, 0, 0, 0, 0, 0, 0, 0, 673, 0, 0, 0, 0, 0, 0, 0, 629, 0, 0, 0, 0, 0, 0, 0, 0, 586, 0, 0, 0, 0, 0, 0, 0, 541, 0, 0, 0, 0, 0, 0, 0, 435, 0, 0, 0, 0, 0, 0, 0, 348, 0, 0, 0, 0, 0, 0, 0, 341, 0, 0, 0, 0, 0, 0, 0, 0, 284, 0, 0, 0, 0, 0, 0, 0, 225, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 0, 0, 0, 0, 172, 0, 0, 0, 0, 0, 0, 0, 0, 159, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, 0, 0, 0, 824]
if hist != exp_hist:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
return 'success'
###############################################################################
# Fetch histogram with specified sampling, using keywords.
def histogram_2():
ds = gdal.Open('data/utmsmall.tif')
hist = ds.GetRasterBand(1).GetHistogram(buckets=16, max=255.5, min=-0.5)
exp_hist = [10, 52, 115, 219, 371, 493, 825, 1077, 1279, 1302, 1127, 783, 625, 462, 331, 929]
if hist != exp_hist:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
return 'success'
###############################################################################
# try on a different data type with out of range values included.
def histogram_3():
ds = gdal.Open('data/int32_withneg.grd')
hist = ds.GetRasterBand(1).GetHistogram(buckets=21, max=100, min=-100,
include_out_of_range = 1,
approx_ok = 0)
exp_hist = [0, 0, 0, 0, 0, 1, 0, 1, 1, 3, 3, 2, 0, 5, 3, 4, 0, 1, 1, 2, 3]
if hist != exp_hist:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
return 'success'
###############################################################################
# try on a different data type without out of range values included.
def histogram_4():
ds = gdal.Open('data/int32_withneg.grd')
hist = ds.GetRasterBand(1).GetHistogram(buckets=21, max=100, min=-100,
include_out_of_range = 0,
approx_ok = 0)
exp_hist = [0, 0, 0, 0, 0, 1, 0, 1, 1, 3, 3, 2, 0, 5, 3, 4, 0, 1, 1, 2, 0]
if hist != exp_hist:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
ds = None
try:
os.unlink('data/int32_withneg.grd.aux.xml')
except:
pass
return 'success'
###############################################################################
# Test GetDefaultHistogram() on the file.
def histogram_5():
ds = gdal.Open('data/utmsmall.tif')
hist = ds.GetRasterBand(1).GetDefaultHistogram(force = 1)
exp_hist = (-0.5, 255.5, 256, [2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 0, 0, 0, 0, 281, 0, 0, 0, 0, 0, 0, 0, 0, 365, 0, 0, 0, 0, 0, 0, 0, 460, 0, 0, 0, 0, 0, 0, 0, 533, 0, 0, 0, 0, 0, 0, 0, 544, 0, 0, 0, 0, 0, 0, 0, 0, 626, 0, 0, 0, 0, 0, 0, 0, 653, 0, 0, 0, 0, 0, 0, 0, 673, 0, 0, 0, 0, 0, 0, 0, 629, 0, 0, 0, 0, 0, 0, 0, 0, 586, 0, 0, 0, 0, 0, 0, 0, 541, 0, 0, 0, 0, 0, 0, 0, 435, 0, 0, 0, 0, 0, 0, 0, 348, 0, 0, 0, 0, 0, 0, 0, 341, 0, 0, 0, 0, 0, 0, 0, 0, 284, 0, 0, 0, 0, 0, 0, 0, 225, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 0, 0, 0, 0, 172, 0, 0, 0, 0, 0, 0, 0, 0, 159, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, 0, 0, 0, 824])
if hist != exp_hist:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
ds = None
try:
os.unlink('data/utmsmall.tif.aux.xml')
except:
pass
return 'success'
###############################################################################
# Test GetDefaultHistogram( force = 0 ) on a JPG file (#3304)
def histogram_6():
shutil.copy('../gdrivers/data/albania.jpg', 'tmp/albania.jpg')
ds = gdal.Open('tmp/albania.jpg')
hist = ds.GetRasterBand(1).GetDefaultHistogram(force = 0)
if hist is not None:
gdaltest.post_reason('did not get expected histogram.')
print(hist)
return 'fail'
ds = None
os.unlink('tmp/albania.jpg')
return 'success'
gdaltest_list = [
histogram_1,
histogram_2,
histogram_3,
histogram_4,
histogram_5,
histogram_6
]
if __name__ == '__main__':
gdaltest.setup_run('histogram')
gdaltest.run_tests(gdaltest_list)
gdaltest.summarize()