Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEDONA-428] Add RS_ZonalStats & RS_ZonalStatsAll #1124

Merged
merged 12 commits into from
Nov 22, 2023

Conversation

furqaankhan
Copy link
Contributor

Did you read the Contributor Guide?

Is this PR related to a JIRA ticket?

What changes were proposed in this PR?

  • Add RS_ZonalStats
  • Add RS_ZonalStatsAll
  • Refactor asRaster implementation to have the ability to keep the reference raster's extent.

How was this patch tested?

  • Passed new and old tests

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation update.


for (int k = 0; k < rasterPixelData.length; k++) {

if (rasterizedPixelData[k] == 0 || excludeNoData && noDataValue != null && rasterPixelData[k] == noDataValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does 0 come from? Why skip it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GridCoverage2D rasterizedGeom = RasterConstructors.asRasterWithRasterExtent(roi, raster, datatype, 150, null);

If you look at that line. The output from asRaster is that it will set 150 to the pixels that are under the geometry and will set 0 to all the other pixels. So I am skipping the 0 is because those pixels are not under the geometry.

@jiayuasu
Copy link
Member

@Kontinuation All raster functions that take a raster and a geometry as an input, we must perform implicit CRS transformation. What do you think?

@Kontinuation
Copy link
Member

@Kontinuation All raster functions that take a raster and a geometry as an input, we must perform implicit CRS transformation. What do you think?

I think performing implicit CRS transformation could be better. Functions such as RS_Value, RS_Values may also need CRS transformation. The geometry needs to be transformed to the CRS of the raster. However, such transformations may be impossible or produce incorrect results due to the valid region of CRS. We have to assume that the geometry specified in the query is always able to be properly transformed to the raster's CRS.

@@ -26,6 +26,7 @@
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Envelope2D;
import org.geotools.geometry.jts.JTS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this PR involve changes related to AsRaster? Is this a API breaking change?

If this change is not related to zonal statistics. It must be placed in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't affect the current RS_AsRaster function. This refactoring was done to remove the pixel coordinate translation and use the output from the AsRasterWithRasterExtent function instead. We talked about it in our tag-up last Thursday to improve performance.

It is related to Zonal statistics.

String datatype = RasterBandAccessors.getBandType(raster, band);
Double noDataValue = RasterBandAccessors.getBandNoDataValue(raster, band);
// Adding an arbitrary value '150' for the pixels that are under the geometry.
GridCoverage2D rasterizedGeom = RasterConstructors.asRasterWithRasterExtent(roi, raster, datatype, 150, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add 150 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Pixels with a value of 0 in the rasterizedPixelData are skipped during computation,
// as they fall outside the geometry specified by 'roi'.
// The region of interest defined by 'roi' contains pixel values of 150,
// as initialized when constructing the raster via RasterConstructors.asRasterWithRasterExtent.
if (rasterizedPixelData[k] == 0 || excludeNoData && noDataValue != null && rasterPixelData[k] == noDataValue) {

It is just a placeholder value to remove the pixels that aren't under the given geometry roi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@furqaankhan Will it collide with existing values in a raster? If not, we are good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No no. It just to determine which values to use for compute from the provided raster.

!!!note
The following conditions will throw an `IllegalArgumentException` if they are not met:

- The `raster` and `zone` geometry should be in the same CRS, refer to [RS_SRID](#rs_srid) for raster, and [ST_SRID](../Function/#st_srid) for geometry to get the spatial reference identifier.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what happens if either side does not have CRS or different CRS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, will change.

@jiayuasu jiayuasu merged commit 1030d0b into apache:master Nov 22, 2023
36 of 40 checks passed
@furqaankhan furqaankhan deleted the rs-zonal-sum branch July 16, 2024 01:39
Kontinuation pushed a commit to Kontinuation/sedona that referenced this pull request Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants