Skip to content

Commit

Permalink
[SEDONA-331] Add RS_Height and RS_Width (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
iGN5117 authored Jul 28, 2023
1 parent e386928 commit c57e962
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public static int numBands(GridCoverage2D raster) {
return raster.getNumSampleDimensions();
}

public static int getWidth(GridCoverage2D raster) {
return raster.getGridGeometry().getGridRange().getSpan(0);
}

public static int getHeight(GridCoverage2D raster) {
return raster.getGridGeometry().getGridRange().getSpan(1);
}



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 @@ -43,6 +43,12 @@ public void testNumBands() {
assertEquals(1, RasterAccessors.numBands(oneBandRaster));
assertEquals(4, RasterAccessors.numBands(multiBandRaster));
}
@Test
public void testWidthAndHeight() throws FactoryException {
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 10, 20, 0, 0, 8);
assertEquals(20, RasterAccessors.getHeight(emptyRaster));
assertEquals(10, RasterAccessors.getWidth(emptyRaster));
}

@Test
public void testSrid() throws FactoryException {
Expand Down
38 changes: 38 additions & 0 deletions docs/api/sql/Raster-operators.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
## Raster Accessors

### RS_Height

Introduction: Returns the height of the raster.

Format: `RS_Height(raster: Raster)`

Since: `1.5.0`

Spark SQL example:
```sql
SELECT RS_Height(raster) FROM rasters
```

Output:
```
512
```

### RS_Width

Introduction: Returns the width of the raster.

Format: `RS_Width(raster: Raster)`

Since: `1.5.0`

Spark SQL example:
```sql
SELECT RS_Width(raster) FROM rasters
```

Output:
```
517
```

## Raster based operators

### RS_Envelope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ object Catalog {
function[RS_Values](1),
function[RS_Intersects](),
function[RS_AsGeoTiff](),
function[RS_AsArcGrid]()
function[RS_AsArcGrid](),
function[RS_Width](),
function[RS_Height]()
)

val aggregateExpressions: Seq[Aggregator[Geometry, Geometry, Geometry]] = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ case class RS_Metadata(inputExpressions: Seq[Expression]) extends InferredExpres
copy(inputExpressions = newChildren)
}
}

case class RS_Width(inputExpressions: Seq[Expression]) extends InferredExpression(RasterAccessors.getWidth _) {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ class rasteralgebraTest extends TestBaseScala with BeforeAndAfter with GivenWhen
assert(result == 1)
}

it("Passed RS_Width with raster") {
val df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
val result = df.selectExpr("RS_Width(RS_FromGeoTiff(content))").first().getInt(0)
assertEquals(512, result)
}

it("Passed RS_Height with raster") {
val df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
val result = df.selectExpr("RS_Height(RS_FromGeoTiff(content))").first().getInt(0)
assertEquals(517, result)
}

it("Passed RS_SetSRID should handle null values") {
val result = sparkSession.sql("select RS_SetSRID(null, 0)").first().get(0)
assert(result == null)
Expand Down

0 comments on commit c57e962

Please sign in to comment.