You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "furqaankhan (via GitHub)" <gi...@apache.org> on 2023/08/18 12:36:20 UTC

[GitHub] [sedona] furqaankhan commented on a diff in pull request #978: [SEDONA-368] Add RS_SummaryStats

furqaankhan commented on code in PR #978:
URL: https://github.com/apache/sedona/pull/978#discussion_r1298401557


##########
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:
   I think so as nodata is not always 0, it can be -9999. Refer: https://desktop.arcgis.com/en/arcmap/latest/manage-data/raster-and-images/nodata-in-raster-datasets.htm#:~:text=NoData%20is%20stored%20as%20a,common%20value%20for%20storing%20NoData.



-- 
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