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-336] Add RS_UpperLeftX and RS_UpperLeftY #935

Merged
merged 13 commits into from
Aug 1, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public static int getHeight(GridCoverage2D raster) {
return raster.getGridGeometry().getGridRange().getSpan(1);
}

public static double getUpperLeftX(GridCoverage2D raster) {
Envelope2D envelope2D = raster.getEnvelope2D();
return envelope2D.getMinX();
}

public static double getUpperLeftY(GridCoverage2D raster) {
Envelope2D envelope2D = raster.getEnvelope2D();
return envelope2D.getMaxY();
}

public static double getScaleX(GridCoverage2D raster) {
return getAffineTransform(raster).getScaleX();
Expand All @@ -81,7 +90,6 @@ private static AffineTransform2D getAffineTransform(GridCoverage2D raster) throw
return (AffineTransform2D) crsTransform;
}


public static Geometry envelope(GridCoverage2D raster) throws FactoryException {
Envelope2D envelope2D = raster.getEnvelope2D();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ public void testSrid() throws FactoryException {
assertEquals(4326, RasterAccessors.srid(multiBandRaster));
}

@Test
public void testUpperLeftX() throws FactoryException {
GridCoverage2D gridCoverage2D = RasterConstructors.makeEmptyRaster(1, 3, 4, 1,2, 5);
double upperLeftX = RasterAccessors.getUpperLeftX(gridCoverage2D);
assertEquals(1, upperLeftX, 0.1d);

gridCoverage2D = RasterConstructors.makeEmptyRaster(10, 7, 8, 5, 6, 9);
upperLeftX = RasterAccessors.getUpperLeftX(gridCoverage2D);
assertEquals(5, upperLeftX, 0.1d);
}

@Test
public void testUpperLeftY() throws FactoryException {
GridCoverage2D gridCoverage2D = RasterConstructors.makeEmptyRaster(1, 3, 4, 1,2, 5);
double upperLeftY = RasterAccessors.getUpperLeftY(gridCoverage2D);
assertEquals(2, upperLeftY, 0.1d);

gridCoverage2D = RasterConstructors.makeEmptyRaster(10, 7, 8, 5, 6, 9);
upperLeftY = RasterAccessors.getUpperLeftY(gridCoverage2D);
assertEquals(6, upperLeftY, 0.1d);
}

@Test
public void testScaleX() throws UnsupportedOperationException, FactoryException {
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(2, 10, 15, 0, 0, 1, 2, 0, 0, 0);
Expand Down
40 changes: 40 additions & 0 deletions docs/api/sql/Raster-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ Output:
-2
```

### RS_UpperLeftX

Introduction: Returns the X coordinate of the upper-left corner of the raster.

Format: `RS_UpperLeftX(raster: Raster)`

Since: `v1.5.0`

Spark SQL Example:

```sql
SELECT RS_UpperLeftX(raster) FROM rasters
```

Output:

```
5
```

### RS_UpperLeftY

Introduction: Returns the Y coordinate of the upper-left corner of the raster.

Format: `RS_UpperLeftY(raster: Raster)`

Since: `v1.5.0`

Spark SQL Example:

```sql
SELECT RS_UpperLeftY(raster) FROM rasters
```

Output:

```
6
```

### RS_Width

Introduction: Returns the width of the raster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ object Catalog {
function[RS_AsArcGrid](),
function[RS_Width](),
function[RS_Height](),
function[RS_UpperLeftX](),
function[RS_UpperLeftY](),
function[RS_ScaleX](),
function[RS_ScaleY]()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ case class RS_Width(inputExpressions: Seq[Expression]) extends InferredExpressio
}
}

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

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

case class RS_Height(inputExpressions: Seq[Expression]) extends InferredExpression(RasterAccessors.getHeight _) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ class rasteralgebraTest extends TestBaseScala with BeforeAndAfter with GivenWhen
assertEquals(rasterDf.count(), binaryDf.count())
}

it("Passed RS_UpperLeftX"){
val df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
val result = df.selectExpr("RS_UpperLeftX(RS_FromGeoTiff(content))").first().getDouble(0)
val expected: Double = -1.3095817809482181E7
assertEquals(expected, result, 1e-12)
}

it("Passed RS_UpperLeftY") {
val df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
val result = df.selectExpr("RS_UpperLeftY(RS_FromGeoTiff(content))").first().getDouble(0)
val expected: Double = 4021262.7487925636
assertEquals(expected, result, 1e-8)
}

it("Passed RS_Metadata") {
val df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
val result = df.selectExpr("RS_Metadata(RS_FromGeoTiff(content))").first().getSeq(0)
Expand Down
Loading