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-427] Add RS_RasterToWorldCoord #1123

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;

import java.awt.image.RenderedImage;
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.Set;

Expand Down Expand Up @@ -106,6 +106,12 @@ public static double getWorldCoordY(GridCoverage2D raster, int colX, int rowY) t
return RasterUtils.getWorldCornerCoordinates(raster, colX, rowY).getY();
}

public static Geometry getWorldCoord(GridCoverage2D raster, int colX, int rowY) throws TransformException {
Point2D worldCoords = RasterUtils.getWorldCornerCoordinates(raster, colX, rowY);
Geometry point = new GeometryFactory().createPoint(new Coordinate(worldCoords.getX(), worldCoords.getY()));
return point;
}

public static String getGeoReference(GridCoverage2D raster) {
return getGeoReference(raster, "GDAL");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,38 @@ public void testWorldCoordYOutOfBounds() throws FactoryException, TransformExcep
assertEquals(expectedY, actualY, 0.1d);
}

@Test
public void testWorldCoord() throws FactoryException, TransformException {
int colX = 1, rowY = 1;
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 5, 10, -123, 54, 5, -10, 0, 0, 4326);

Coordinate actual = RasterAccessors.getWorldCoord(emptyRaster, colX, rowY).getCoordinate();
double expectedX = -123, expectedY = 54;

assertEquals(expectedX, actual.getX(), 0.1d);
assertEquals(expectedY, actual.getY(), 0.1d);

rowY = 2;
actual = RasterAccessors.getWorldCoord(emptyRaster, colX, rowY).getCoordinate();
expectedY = 44;

assertEquals(expectedX, actual.getX(), 0.1d);
assertEquals(expectedY, actual.getY(), 0.1d);
}

@Test
public void testWorldCoordOutOfBounds() throws FactoryException, TransformException{
int colX = 4;
int rowY = 11;
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 5, 10, -123, 54, 5, -10, 0, 0, 4326);

double expectedX = -108, expectedY = -46;
Coordinate actual = RasterAccessors.getWorldCoord(emptyRaster, colX, rowY).getCoordinate();

assertEquals(expectedX, actual.getX(), 0.1d);
assertEquals(expectedY, actual.getY(), 0.1d);
}

@Test
public void testGridCoordLatLon() throws TransformException, FactoryException {
double longitude = -123, latitude = 54;
Expand Down
20 changes: 20 additions & 0 deletions docs/api/sql/Raster-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ Output:
54
```

### RS_RasterToWorldCoord

Introduction: Returns the upper left X and Y coordinates of the given row and column of the given raster geometric units of the geo-referenced raster as a Point geometry. If any out of bounds values are given, the X and Y coordinates of the assumed point considering existing raster pixel size and skew values will be returned.

Format: `RS_RasterToWorldCoord(raster: Raster, colX: Integer, rowY: Integer)`

Since: `v1.5.1`

Spark SQL Example:

```sql
SELECT RS_RasterToWorldCoord(ST_MakeEmptyRaster(1, 5, 10, -123, 54, 5, -10, 0, 0, 4326), 1, 1) from rasters
```

Output:

```
POINT (-123 54)
```

### RS_Rotation

Introduction: Returns the uniform rotation of the raster in radian.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ object Catalog {
function[RS_ConvexHull](),
function[RS_RasterToWorldCoordX](),
function[RS_RasterToWorldCoordY](),
function[RS_RasterToWorldCoord](),
function[RS_Within](),
function[RS_Contains](),
function[RS_WorldToRasterCoord](),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ case class RS_RasterToWorldCoordY(inputExpressions: Seq[Expression]) extends Inf
copy(inputExpressions = newChildren)
}
}

case class RS_RasterToWorldCoord(inputExpressions: Seq[Expression]) extends InferredExpression(RasterAccessors.getWorldCoord _) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
}
}

case class RS_WorldToRasterCoord(inputExpressions: Seq[Expression]) extends InferredExpression(inferrableFunction3(RasterAccessors.getGridCoord), inferrableFunction2(RasterAccessors.getGridCoord)) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,14 @@ class rasteralgebraTest extends TestBaseScala with BeforeAndAfter with GivenWhen
assertEquals(4021262.7487925636, result, 0.5d)
}

it("Passed RS_RasterToWorldCoord with raster") {
var df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
df = df.selectExpr("RS_FromGeoTiff(content) as raster")
val result = df.selectExpr("RS_RasterToWorldCoord(raster, 1, 1)").first().get(0).toString
val expected = "POINT (-13095818 4021262.75)"
assertEquals(expected, result)
}

it("Passed RS_WorldToRasterCoord with raster") {
var df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
df = df.selectExpr("RS_FromGeoTiff(content) as raster")
Expand Down
Loading