Skip to content

Commit

Permalink
[SEDONA-336] Add RS_UpperLeftX and RS_UpperLeftY (apache#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
furqaankhan authored and Kontinuation committed Aug 9, 2023
1 parent e0f1e70 commit 352cdbf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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 @@ -82,7 +91,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 @@ -83,6 +83,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 @@ -211,6 +211,8 @@ object Catalog {
function[RS_AsArcGrid](),
function[RS_Width](),
function[RS_Height](),
function[RS_UpperLeftX](),
function[RS_UpperLeftY](),
function[RS_ScaleX](),
function[RS_ScaleY](),
function[RS_BandPath]()
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 @@ -451,6 +451,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

0 comments on commit 352cdbf

Please sign in to comment.