def processAlgorithm(self, progress): uri = self.getParameterValue(self.INPUT) layer = QGisLayers.getObjectFromUri(uri) outputplot = self.getOutputValue(self.PLOT) outputtable = self.getOutputFromName(self.TABLE) values = raster.scanraster(layer, progress) nbins = self.getParameterValue(self.BINS) #ALERT:this is potentially blocking if the layer is too big plt.close() valueslist = [] for v in values: if v is not None: valueslist.append(v) n, bins, values = plt.hist(valueslist, nbins) fields = [ QgsField("CENTER_VALUE", QVariant.Double), QgsField("NUM_ELEM", QVariant.Double) ] writer = outputtable.getTableWriter(fields) for i in xrange(len(values)): writer.addRecord([str(bins[i]) + "-" + str(bins[i + 1]), n[i]]) plotFilename = outputplot + ".png" lab.savefig(plotFilename) f = open(outputplot, "w") f.write("<img src=\"" + plotFilename + "\"/>") f.close()
def processAlgorithm(self, progress): outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE) uri = self.getParameterValue(self.INPUT) layer = QGisLayers.getObjectFromUri(uri) values = raster.scanraster(layer, progress) n = 0 nodata = 0 mean = 0 M2 = 0 sum = 0 minvalue = None maxvalue = None for v in values: if v is not None: sum += v n = n + 1 delta = v - mean mean = mean + delta/n M2 = M2 + delta*(v - mean) if minvalue is None: minvalue = v maxvalue = v else: minvalue = min(v, minvalue) maxvalue = max(v, maxvalue) else: nodata += 1 variance = M2/(n - 1) stddev = math.sqrt(variance) data = [] data.append("Valid cells: " + unicode(n)) data.append("No-data cells: " + unicode(nodata)) data.append("Minimum value: " + unicode(minvalue)) data.append("Maximum value: " + unicode(maxvalue)) data.append("Sum: " + unicode(sum)) data.append("Mean value: " + unicode(mean)) data.append("Standard deviation: " + unicode(stddev)) self.createHTML(outputFile, data) self.setOutputValue(self.COUNT, n) self.setOutputValue(self.NO_DATA_COUNT, nodata) self.setOutputValue(self.MIN, minvalue) self.setOutputValue(self.MAX, maxvalue) self.setOutputValue(self.SUM, sum) self.setOutputValue(self.MEAN, mean) self.setOutputValue(self.STD_DEV, stddev)
def processAlgorithm(self, progress): uri = self.getParameterValue(self.INPUT) layer = QGisLayers.getObjectFromUri(uri) outputplot = self.getOutputValue(self.PLOT) outputtable = self.getOutputFromName(self.TABLE) values = raster.scanraster(layer, progress) nbins = self.getParameterValue(self.BINS) #ALERT:this is potentially blocking if the layer is too big plt.close() valueslist = [] for v in values: if v is not None: valueslist.append(v) n, bins, values = plt.hist(valueslist, nbins) fields = [QgsField("CENTER_VALUE", QVariant.Double), QgsField("NUM_ELEM", QVariant.Double)] writer = outputtable.getTableWriter(fields) for i in xrange(len(values)): writer.addRecord([str(bins[i]) + "-" + str(bins[i+1]) , n[i]]) plotFilename = outputplot +".png" lab.savefig(plotFilename) f = open(outputplot, "w") f.write("<img src=\"" + plotFilename + "\"/>") f.close()