Skip to content

Commit bed8036

Browse files
committed
Added Python bindings an finished up tests.
1 parent c021d2e commit bed8036

File tree

7 files changed

+35
-7
lines changed

7 files changed

+35
-7
lines changed

core/src/main/scala/org/locationtech/rasterframes/expressions/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ package object expressions {
134134
registry.registerExpression[RenderPNG.RenderCompositePNG]("rf_render_png")
135135
registry.registerExpression[RGBComposite]("rf_rgb_composite")
136136

137+
registry.registerExpression[XZ2Indexer]("rf_spatial_index")
138+
137139
registry.registerExpression[transformers.ReprojectGeometry]("st_reproject")
138140
}
139141
}

core/src/main/scala/org/locationtech/rasterframes/expressions/transformers/XZ2Indexer.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import geotrellis.vector.Extent
2626
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
2727
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
2828
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
29-
import org.apache.spark.sql.catalyst.expressions.{BinaryExpression, Expression}
29+
import org.apache.spark.sql.catalyst.expressions.{BinaryExpression, Expression, ExpressionDescription}
3030
import org.apache.spark.sql.jts.JTSTypes
3131
import org.apache.spark.sql.rf.RasterSourceUDT
3232
import org.apache.spark.sql.types.{DataType, LongType}
@@ -51,6 +51,14 @@ import org.locationtech.rasterframes.expressions.accessors.GetCRS
5151
* i.e. how many times the space will be recursively quartered
5252
* 1-18 is typical.
5353
*/
54+
@ExpressionDescription(
55+
usage = "_FUNC_(geom, crs) - Constructs a XZ2 index in WGS84/EPSG:4326",
56+
arguments = """
57+
Arguments:
58+
* geom - Geometry or item with Geometry: Extent, ProjectedRasterTile, or RasterSource
59+
* crs - the native CRS of the `geom` column
60+
"""
61+
)
5462
case class XZ2Indexer(left: Expression, right: Expression, indexResolution: Short = 18)
5563
extends BinaryExpression with CodegenFallback {
5664

core/src/test/scala/org/locationtech/rasterframes/expressions/XZ2IndexerSpec.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121

2222
package org.locationtech.rasterframes.expressions
2323
import geotrellis.proj4.{CRS, LatLng, WebMercator}
24-
import org.locationtech.rasterframes._
25-
import geotrellis.vector.Extent
26-
import org.locationtech.rasterframes.TestEnvironment
27-
import org.apache.spark.sql.functions.lit
28-
import org.locationtech.rasterframes._
29-
import encoders.serialized_literal
3024
import geotrellis.raster.CellType
25+
import geotrellis.vector.Extent
3126
import org.apache.spark.sql.Encoders
3227
import org.locationtech.geomesa.curve.XZ2SFC
28+
import org.locationtech.rasterframes.{TestEnvironment, _}
29+
import org.locationtech.rasterframes.encoders.serialized_literal
3330
import org.locationtech.rasterframes.ref.{InMemoryRasterSource, RasterSource}
3431
import org.locationtech.rasterframes.tiles.ProjectedRasterTile
3532
import org.scalatest.Inspectors

docs/src/main/paradox/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### 0.8.3
66

77
* Updated `rf_crs` to accept string columns containing CRS specifications. ([#366](https://github.com/locationtech/rasterframes/issues/366))
8+
* Added `rf_spatial_index` function. ([#368](https://github.com/locationtech/rasterframes/issues/368))
89

910
### 0.8.2
1011

pyrasterframes/src/main/python/docs/reference.pymd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ See also GeoMesa [st_envelope](https://www.geomesa.org/documentation/user/spark/
6565

6666
Convert an extent to a Geometry. The extent likely comes from @ref:[`st_extent`](reference.md#st-extent) or @ref:[`rf_extent`](reference.md#rf-extent).
6767

68+
69+
### rf_spatial_index
70+
71+
Long rf_spatial_index(Geometry geom, CRS crs)
72+
Long rf_spatial_index(Extent extent, CRS crs)
73+
Long rf_spatial_index(ProjectedRasterTile proj_raster, CRS crs)
74+
75+
Constructs a XZ2 index in WGS84/EPSG:4326 from either a Geometry, Extent, ProjectedRasterTile and its CRS.
76+
6877
## Tile Metadata and Mutation
6978

7079
Functions to access and change the particulars of a `tile`: its shape and the data type of its cells. See section on @ref:["NoData" handling](nodata-handling.md) for additional discussion of cell types.

pyrasterframes/src/main/python/pyrasterframes/rasterfunctions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@ def rf_geometry(proj_raster_col):
585585
"""Get the extent of a RasterSource or ProjectdRasterTile as a Geometry"""
586586
return _apply_column_function('rf_geometry', proj_raster_col)
587587

588+
589+
def rf_spatial_index(geom_col, crs_col):
590+
"""Constructs a XZ2 index in WGS84 from either a Geometry, Extent, ProjectedRasterTile, or RasterSource and its CRS"""
591+
return _apply_column_function('rf_spatial_index', geom_col, crs_col)
592+
588593
# ------ GeoMesa Functions ------
589594

590595
def st_geomFromGeoHash(*args):

pyrasterframes/src/main/python/tests/VectorTypesTests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@ def test_geojson(self):
156156
geo = self.spark.read.geojson(sample)
157157
geo.show()
158158
self.assertEqual(geo.select('geometry').count(), 8)
159+
160+
def test_spatial_index(self):
161+
df = self.df.select(rf_spatial_index(self.df.poly_geom, rf_crs(lit("EPSG:4326"))).alias('index'))
162+
expected = {22858201775, 38132946267, 38166922588, 38180072113}
163+
indexes = {x[0] for x in df.collect()}
164+
self.assertSetEqual(indexes, expected)

0 commit comments

Comments
 (0)