Skip to content

Instantly share code, notes, and snippets.

@oscarfonts
Created October 11, 2012 12:15
Show Gist options
  • Save oscarfonts/3871938 to your computer and use it in GitHub Desktop.
Save oscarfonts/3871938 to your computer and use it in GitHub Desktop.
GeoServer WPS Script: Raster Classification Statistics
from jarray import array
from geoserver.wps import process
from java.lang import String
from org.geotools.coverage.grid import GridCoverage2D
from java.awt.image import RenderedImage
from javax.media.jai import JAI
from javax.media.jai import ParameterBlockJAI
from javax.media.jai import RenderedOp
from org.jaitools.media.jai.classifiedstats import ClassifiedStats
from org.jaitools.media.jai.classifiedstats import ClassifiedStatsRIF
from org.jaitools.media.jai.classifiedstats import ClassifiedStatsDescriptor
from org.jaitools.numeric import Statistic
from org.geotools.image.jai import Registry
from javax.media.jai import JAI
@process(
title='Raster classification stats',
description='Calculate stats, use a couple of classification layers',
inputs={
'data': (GridCoverage2D, 'Data to perform statistics on'),
'class1': (GridCoverage2D, 'A first classification layer'),
'class2': (GridCoverage2D, 'A second classification layer'),
},
outputs={
'result': (String, 'The statistic results')
}
)
def run(data, class1, class2):
Registry.registerRIF(JAI.getDefaultInstance(), ClassifiedStatsDescriptor(), ClassifiedStatsRIF(), Registry.JAI_TOOLS_PRODUCT)
dataImg = data.getRenderedImage()
class1Img = class1.getRenderedImage()
class2Img = class2.getRenderedImage()
pb = ParameterBlockJAI("ClassifiedStats")
pb.addSource(dataImg)
classifiers = array([class1Img, class2Img], RenderedImage)
pb.setParameter("classifiers", classifiers)
# Possible statistics are: MAX, MEAN, MEDIAN, MIN, RANGE, SDEV, SUM, VARIANCE
statistics = array([Statistic.SUM], Statistic)
pb.setParameter("stats", statistics)
statsOp = JAI.create("ClassifiedStats", pb)
stats = statsOp.getProperty(ClassifiedStatsDescriptor.CLASSIFIED_STATS_PROPERTY)
results = stats.results()
# Render the results
ret = ""
for result in results:
ret += str(class1.getName()) + " " + str(class2.getName()) + " "
for stat in result.get(result.keySet().iterator().next()):
ret += stat.getStatistic().name() + " "
ret += "\n"
for classes in result.keySet():
statsList = result.get(classes)
for k in classes.getKeys():
ret += str(k) + " "
for stat in statsList:
ret += str(stat.getValue()) + " "
ret += "\n"
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment