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-331] Add RS_Height and RS_Width #928

Merged
merged 3 commits into from
Jul 28, 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 @@ -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