Kontinuation commented on code in PR #1011:
URL: https://github.com/apache/sedona/pull/1011#discussion_r1320459614
##########
common/src/test/java/org/apache/sedona/common/raster/RasterConstructorsTest.java:
##########
@@ -55,6 +58,99 @@ public void fromGeoTiff() throws IOException,
FactoryException {
assertEquals(4, gridCoverage2D.getNumSampleDimensions());
}
+ @Test
+ public void testAsRasterWithEmptyRaster() throws FactoryException,
ParseException {
+ // Polygon
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(2, 255,
255, 1, -1, 2, -2, 0, 0, 4326);
+ Geometry geom = Constructors.geomFromWKT("POLYGON((15 15, 18 20, 15
24, 24 25, 15 15))", 0);
+ GridCoverage2D rasterized = RasterConstructors.asRaster(geom, raster,
"d", 3093151, 3d);
+ double[] actual = MapAlgebra.bandAsArray(rasterized, 1);
+ double[] expected = new double[] {3093151.0, 3093151.0, 3093151.0,
0.0, 0.0, 3093151.0, 0.0, 0.0, 0.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0};
+ assertArrayEquals(expected, actual, 0.1d);
+
+ rasterized = RasterConstructors.asRaster(geom, raster, "d");
+ actual = MapAlgebra.bandAsArray(rasterized, 1);
+ expected = new double[] {1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+ assertArrayEquals(expected, actual, 0.1d);
+
+ // MultiPolygon
+ geom = Constructors.geomFromWKT("MULTIPOLYGON (((15 15, 15 55, 35 55,
35 15, 15 15)), ((44 24, 44 64, 64 64, 64 24, 44 24)))", 0);
+ rasterized = RasterConstructors.asRaster(geom, raster, "d", 3093151,
3d);
+ actual = MapAlgebra.bandAsArray(rasterized, 1);
+ expected = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0,
0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151
.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0,
0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0,
0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
0.0, 0.0, 0.0, 0.0, 0.0, 3
093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0
, 3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3
093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0, 3093151.0,
3093151.0, 3093151.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+ assertArrayEquals(expected, actual, 0.1d);
+
+ rasterized = RasterConstructors.asRaster(geom, raster, "d");
+ actual = MapAlgebra.bandAsArray(rasterized, 1);
+ expected = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
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0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+ assertArrayEquals(expected, actual, 0.1d);
Review Comment:
These walls of texts as expected results are very confusing.
I suggest rendering smaller rasters, or picking some representative points
and only testing them.
##########
common/src/main/java/org/apache/sedona/common/raster/RasterConstructors.java:
##########
@@ -45,6 +55,116 @@ public static GridCoverage2D fromGeoTiff(byte[] bytes)
throws IOException {
return geoTiffReader.read(null);
}
+ /**
+ * Returns a raster that is converted from the geometry provided.
+ * @param geom The geometry to convert
+ * @param raster The reference raster
+ * @param pixelType The data type of pixel/cell of resultant raster
+ * @param value The value of the pixel of the resultant raster
+ * @param noDataValue The noDataValue of the resultant raster
+ *
+ * @return Rasterized Geometry
+ * @throws FactoryException
+ */
+ public static GridCoverage2D asRaster(Geometry geom, GridCoverage2D
raster, String pixelType, double value, Double noDataValue) throws
FactoryException {
+
+ DefaultFeatureCollection featureCollection =
getFeatureCollection(geom, raster.getCoordinateReferenceSystem());
+
+ double[] metadata = RasterAccessors.metadata(raster);
+ // The current implementation doesn't support rasters with properties
below
+ // It is not a problem as most rasters don't have these properties
+ // ScaleX < 0
+ if (metadata[4] < 0) {
+ throw new IllegalArgumentException(String.format("ScaleX %f of the
raster is negative, it should be positive", metadata[4]));
+ }
+ // ScaleY > 0
+ if (metadata[5] > 0) {
+ throw new IllegalArgumentException(String.format("ScaleY %f of the
raster is positive. It should be negative.", metadata[5]));
+ }
+ // SkewX should be zero
+ if (metadata[6] != 0) {
+ throw new IllegalArgumentException(String.format("SkewX %d of the
raster is not zero.", metadata[6]));
+ }
+ // SkewY should be zero
+ if (metadata[7] != 0) {
+ throw new IllegalArgumentException(String.format("SkewY %d of the
raster is not zero.", metadata[7]));
+ }
+
+ Envelope2D bound = JTS.getEnvelope2D(geom.getEnvelopeInternal(),
raster.getCoordinateReferenceSystem2D());
+
+ double scaleX = Math.abs(metadata[4]), scaleY = Math.abs(metadata[5]);
+ if (geom.getGeometryType().equalsIgnoreCase(Geometry.TYPENAME_POINT)) {
+ bound = new Envelope2D(bound.getCoordinateReferenceSystem(),
bound.getCenterX() - scaleX * 0.5, bound.getCenterY() - scaleY * 0.5, scaleX,
scaleY);
+ }
+
+ int width = (int) bound.getWidth(), height = (int) bound.getHeight();
+
+ if (geom.getGeometryType().equalsIgnoreCase(Geometry.TYPENAME_POINT)) {
+ width = 1;
+ height = 1;
+ } else {
+ // To preserve scale of reference raster
+ width = (int) (width / scaleX);
+ height = (int) (height / scaleY);
+ }
Review Comment:
There are some special treatments for points. I think it is also needed for
horizontal and vertical line strings. We also need to add tests for these cases.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]