You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "Kontinuation (via GitHub)" <gi...@apache.org> on 2023/08/18 03:16:46 UTC
[GitHub] [sedona] Kontinuation commented on a diff in pull request #978: [SEDONA-368] Add RS_SummaryStats
Kontinuation commented on code in PR #978:
URL: https://github.com/apache/sedona/pull/978#discussion_r1297936711
##########
common/src/main/java/org/apache/sedona/common/raster/RasterBandAccessors.java:
##########
@@ -78,6 +80,68 @@ public static long getCount(GridCoverage2D raster, int band) {
// return getCount(raster, 1, excludeNoDataValue);
// }
+ public static double[] getSummaryStats(GridCoverage2D rasterGeom, int band, boolean excludeNoDataValue) {
+ ensureBand(rasterGeom, band);
+ Raster raster = rasterGeom.getRenderedImage().getData();
+ int height = RasterAccessors.getHeight(rasterGeom), width = RasterAccessors.getWidth(rasterGeom);
+ double[] pixels = raster.getSamples(0, 0, width, height, band - 1, (double[]) null);
+ double count = 0, sum = 0, mean = 0, stddev = 0, min = Double.MAX_VALUE, max = -Double.MAX_VALUE;
+ if(excludeNoDataValue) {
+ // exclude no data values
+ Double noDataValue = RasterBandAccessors.getBandNoDataValue(rasterGeom, band);
+ for (double pixel: pixels) {
+ if (noDataValue == null || pixel != noDataValue) {
+ count++;
+ sum += pixel;
+ if (pixel < min) {
+ min = pixel;
+ }
+ if (pixel > max) {
+ max = pixel;
+ }
+ }
+ }
+ mean = sum / count;
+ stddev = getStddev(pixels, mean);
+ } else {
+ // include no data values
+ count = pixels.length;
+ for (double pixel: pixels) {
+ sum += pixel;
+ if (pixel < min) {
+ min = pixel;
+ }
+ if (pixel > max) {
+ max = pixel;
+ }
+ }
+ mean = sum / count;
+ stddev = getStddev(pixels, mean);
+ }
+ return new double[]{count, sum, mean, stddev, min, max};
+ }
+
+ public static double[] getSummaryStats(GridCoverage2D raster, int band) {
+ return getSummaryStats(raster, band, true);
+ }
+
+ public static double[] getSummaryStats(GridCoverage2D raster) {
+ return getSummaryStats(raster, 1, true);
+ }
+
+// Adding the function signature when InferredExpression supports function with same arity but different argument types
+// public static double[] getSummaryStats(GridCoverage2D raster, boolean excludeNoDataValue) {
+// return getSummaryStats(raster, 1, excludeNoDataValue);
+// }
+
+ private static double getStddev(double[] pixels, double mean) {
+ double stddev = 0;
+ for(double pixel: pixels){
+ stddev += Math.pow(pixel - mean, 2);
+ }
+ return Math.sqrt(stddev/(pixels.length - 1));
+ }
Review Comment:
Do we need to exclude pixels with nodata value when computing std-dev?
##########
common/src/main/java/org/apache/sedona/common/raster/RasterBandAccessors.java:
##########
@@ -78,6 +80,68 @@ public static long getCount(GridCoverage2D raster, int band) {
// return getCount(raster, 1, excludeNoDataValue);
// }
+ public static double[] getSummaryStats(GridCoverage2D rasterGeom, int band, boolean excludeNoDataValue) {
+ ensureBand(rasterGeom, band);
+ Raster raster = rasterGeom.getRenderedImage().getData();
+ int height = RasterAccessors.getHeight(rasterGeom), width = RasterAccessors.getWidth(rasterGeom);
+ double[] pixels = raster.getSamples(0, 0, width, height, band - 1, (double[]) null);
+ double count = 0, sum = 0, mean = 0, stddev = 0, min = Double.MAX_VALUE, max = -Double.MAX_VALUE;
+ if(excludeNoDataValue) {
+ // exclude no data values
+ Double noDataValue = RasterBandAccessors.getBandNoDataValue(rasterGeom, band);
+ for (double pixel: pixels) {
+ if (noDataValue == null || pixel != noDataValue) {
+ count++;
+ sum += pixel;
+ if (pixel < min) {
+ min = pixel;
+ }
+ if (pixel > max) {
+ max = pixel;
+ }
+ }
+ }
+ mean = sum / count;
Review Comment:
We need to be aware of the cases when all pixels in the band are nodata value. PostGIS returns all statistics except count as `NULL`. I think both NULL and NaN are sane choices.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org