Skip to content

Commit 3ff4d9e

Browse files
committedApr 24, 2014
[processing] added new example script
1 parent 8436027 commit 3ff4d9e

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
 
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
##[Example scripts]aster processing=group
2+
##input=raster
3+
##round_values_to_ndigits=number 3
4+
##output_file=output html
5+
6+
from osgeo import gdal
7+
import sys
8+
import math
9+
10+
# load raster
11+
gdalData = gdal.Open(str(input))
12+
13+
# get width and heights of the raster
14+
xsize = gdalData.RasterXSize
15+
ysize = gdalData.RasterYSize
16+
17+
# get number of bands
18+
bands = gdalData.RasterCount
19+
20+
# start writing html output
21+
f = open(output_file, 'a')
22+
f.write('<TABLE>\n<TH>Band Number </TH> <TH>Cell Value </TH> <TH>Count</TH>\n')
23+
24+
# process the raster
25+
for i in xrange(1, bands + 1):
26+
band_i = gdalData.GetRasterBand(i)
27+
raster = band_i.ReadAsArray()
28+
29+
# create dictionary for unique values count
30+
count = {}
31+
32+
# count unique values for the given band
33+
for col in range( xsize ):
34+
for row in range( ysize ):
35+
cell_value = raster[row, col]
36+
37+
# check if cell_value is NaN
38+
if math.isnan(cell_value):
39+
cell_value = 'Null'
40+
41+
# round floats if needed
42+
elif round_values_to_ndigits:
43+
try:
44+
cell_value = round(cell_value, int(round_values_to_ndigits))
45+
except:
46+
cell_value = round(cell_value)
47+
48+
# add cell_value to dictionary
49+
try:
50+
count[cell_value] += 1
51+
except:
52+
count[cell_value] = 1
53+
54+
# print results sorted by cell_value
55+
for key in sorted(count.iterkeys()):
56+
line = "<TD>%s</TD> <TD>%s</TD> <TD>%s</TD>" %(i, key, count[key])
57+
f.write('<TR>'+ line + '</TR>' + '\n')
58+
59+
f.write('</TABLE>')
60+
f.close
61+
62+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(dp0
2+
S'result'
3+
p1
4+
VHtml file that will have the table with unique cell values and their counts.
5+
p2
6+
sS'ALG_DESC'
7+
p3
8+
VThis script will count unique values in a raster. Multiband rasters are accepted.
9+
p4
10+
sS'ALG_CREATOR'
11+
p5
12+
VYury Ryabov\u000ariabovvv at gmail dot com\u000a2013\u000aGPLv3
13+
p6
14+
sS'output_file'
15+
p7
16+
VProvide path to html file that will be created to represent calculation results.
17+
p8
18+
sS'unique_cells_count'
19+
p9
20+
V
21+
p10
22+
sS'round_to_ndigits'
23+
p11
24+
VIf you want values in raster to be rounded and 'round floats' parameter was modified, raster values will be rounded to ndigits after decimal point. Here you enter that ndigits value. Negative values in this dield are accepted and cell values will be rounded to ndigits before decimal point. NOTE that value in this field won't affect calculations if 'round floats' option is set to 'no'.
25+
p12
26+
sS'round_values_to_ndigits'
27+
p13
28+
VOptional. If you want values in raster to be rounded before counting, enter ndigits value here. Negative values in this field are accepted and cell values will be rounded to ndigits before decimal point.
29+
p14
30+
sS'ALG_HELP_CREATOR'
31+
p15
32+
VYury Ryabov\u000ariabovvv at gmail dot com\u000a2013\u000a
33+
p16
34+
sS'round_to_digit'
35+
p17
36+
g10
37+
sS'input'
38+
p18
39+
VSingle- or multiband GDAL-supported raster.
40+
p19
41+
sS'round_floats'
42+
p20
43+
V
44+
p21
45+
sS'round_values'
46+
p22
47+
VIf you want to count values rounded to the certain digit - just modify this string (to 'yes' or whatever you like).
48+
p23
49+
s.

0 commit comments

Comments
 (0)
Please sign in to comment.