Skip to content

Commit

Permalink
[processing] added new example script
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Apr 24, 2014
1 parent 8436027 commit 3ff4d9e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
62 changes: 62 additions & 0 deletions python/plugins/processing/script/scripts/Unique_values_count.py
@@ -0,0 +1,62 @@
##[Example scripts]aster processing=group
##input=raster
##round_values_to_ndigits=number 3
##output_file=output html

from osgeo import gdal
import sys
import math

# load raster
gdalData = gdal.Open(str(input))

# get width and heights of the raster
xsize = gdalData.RasterXSize
ysize = gdalData.RasterYSize

# get number of bands
bands = gdalData.RasterCount

# start writing html output
f = open(output_file, 'a')
f.write('<TABLE>\n<TH>Band Number </TH> <TH>Cell Value </TH> <TH>Count</TH>\n')

# process the raster
for i in xrange(1, bands + 1):
band_i = gdalData.GetRasterBand(i)
raster = band_i.ReadAsArray()

# create dictionary for unique values count
count = {}

# count unique values for the given band
for col in range( xsize ):
for row in range( ysize ):
cell_value = raster[row, col]

# check if cell_value is NaN
if math.isnan(cell_value):
cell_value = 'Null'

# round floats if needed
elif round_values_to_ndigits:
try:
cell_value = round(cell_value, int(round_values_to_ndigits))
except:
cell_value = round(cell_value)

# add cell_value to dictionary
try:
count[cell_value] += 1
except:
count[cell_value] = 1

# print results sorted by cell_value
for key in sorted(count.iterkeys()):
line = "<TD>%s</TD> <TD>%s</TD> <TD>%s</TD>" %(i, key, count[key])
f.write('<TR>'+ line + '</TR>' + '\n')

f.write('</TABLE>')
f.close


@@ -0,0 +1,49 @@
(dp0
S'result'
p1
VHtml file that will have the table with unique cell values and their counts.
p2
sS'ALG_DESC'
p3
VThis script will count unique values in a raster. Multiband rasters are accepted.
p4
sS'ALG_CREATOR'
p5
VYury Ryabov\u000ariabovvv at gmail dot com\u000a2013\u000aGPLv3
p6
sS'output_file'
p7
VProvide path to html file that will be created to represent calculation results.
p8
sS'unique_cells_count'
p9
V
p10
sS'round_to_ndigits'
p11
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'.
p12
sS'round_values_to_ndigits'
p13
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.
p14
sS'ALG_HELP_CREATOR'
p15
VYury Ryabov\u000ariabovvv at gmail dot com\u000a2013\u000a
p16
sS'round_to_digit'
p17
g10
sS'input'
p18
VSingle- or multiband GDAL-supported raster.
p19
sS'round_floats'
p20
V
p21
sS'round_values'
p22
VIf you want to count values rounded to the certain digit - just modify this string (to 'yes' or whatever you like).
p23
s.

0 comments on commit 3ff4d9e

Please sign in to comment.