Skip to content

Commit

Permalink
updated R bindings and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sllynn committed Nov 1, 2024
1 parent 0c39139 commit bc1b86f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ generate_singleband_raster_df <- function() {

test_that("mosaic can read single-band GeoTiff", {
sdf <- generate_singleband_raster_df()

row <- first(sdf)
expect_equal(row$length, 1067862L)
expect_equal(row$x_size, 2400)
Expand Down Expand Up @@ -158,14 +158,16 @@ sdf <- createDataFrame(

sdf <- agg(groupBy(sdf), masspoints = collect_list(column("wkt")))
sdf <- withColumn(sdf, "breaklines", expr("array('LINESTRING EMPTY')"))
sdf <- withColumn(sdf, "splitPointFinder", lit("NONENCROACHING"))
sdf <- withColumn(sdf, "origin", st_geomfromwkt(lit("POINT (0.6 1.8)")))
sdf <- withColumn(sdf, "xWidth", lit(12L))
sdf <- withColumn(sdf, "yWidth", lit(6L))
sdf <- withColumn(sdf, "xSize", lit(0.1))
sdf <- withColumn(sdf, "ySize", lit(0.1))
sdf <- withColumn(sdf, "noData", lit(-9999.0))
sdf <- withColumn(sdf, "tile", rst_dtmfromgeoms(
column("masspoints"), column("breaklines"), lit(0.0), lit(0.01),
column("origin"), column("xWidth"), column("yWidth"), column("xSize"), column("ySize"))
)
column("masspoints"), column("breaklines"), lit(0.0), lit(0.01), column("splitPointFinder"),
column("origin"), column("xWidth"), column("yWidth"), column("xSize"), column("ySize"), column("noData")
))
expect_equal(SparkR::count(sdf), 1)
})
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,24 @@ sdf <- createDataFrame(

sdf <- agg(groupBy(sdf), masspoints = collect_list(column("wkt")))
sdf <- withColumn(sdf, "breaklines", expr("array('LINESTRING EMPTY')"))
triangulation_sdf <- withColumn(sdf, "triangles", st_triangulate(column("masspoints"), column("breaklines"), lit(0.0), lit(0.01)))
triangulation_sdf <- withColumn(sdf, "triangles", st_triangulate(column("masspoints"), column("breaklines"), lit(0.0), lit(0.01), lit("NONENCROACHING")))
cache(triangulation_sdf)
expect_equal(SparkR::count(triangulation_sdf), 2)
expected <- c("POLYGON Z((0 2 2, 2 1 0, 1 3 3, 0 2 2))", "POLYGON Z((1 3 3, 2 1 0, 3 2 1, 1 3 3))")
expect_contains(expected, first(triangulation_sdf)$triangles)

interpolation_sdf <- sdf
interpolation_sdf <- withColumn(interpolation_sdf, "origin", st_geomfromwkt(lit("POINT (0.6 1.8)")))
interpolation_sdf <- withColumn(interpolation_sdf, "origin", st_geomfromwkt(lit("POINT (0.55 1.75)")))
interpolation_sdf <- withColumn(interpolation_sdf, "xWidth", lit(12L))
interpolation_sdf <- withColumn(interpolation_sdf, "yWidth", lit(6L))
interpolation_sdf <- withColumn(interpolation_sdf, "xSize", lit(0.1))
interpolation_sdf <- withColumn(interpolation_sdf, "ySize", lit(0.1))
interpolation_sdf <- withColumn(interpolation_sdf, "interpolated", st_interpolateelevation(
column("masspoints"),
column("breaklines"),
lit(0.0),
lit(0.01),
lit(0.01),
lit("NONENCROACHING"),
column("origin"),
column("xWidth"),
column("yWidth"),
Expand All @@ -134,5 +135,5 @@ interpolation_sdf <- withColumn(interpolation_sdf, "interpolated", st_interpolat
))
cache(interpolation_sdf)
expect_equal(SparkR::count(interpolation_sdf), 6 * 12)
expect_contains(collect(interpolation_sdf)$interpolated, "POINT Z(0.6 2 1.8)")
expect_contains(collect(interpolation_sdf)$interpolated, "POINT Z(1.6 1.8 1.2)")
})
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ test_that ("a terrain model can be produced from point geometries", {
breaklines,
as.double(0.0),
as.double(0.01),
"NONENCROACHING",
origin,
xWidth,
yWidth,
xSize,
ySize
ySize,
as.double(-9999.0)
)
)
expect_equal(sdf_nrow(sdf), 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ test_that ("triangulation and interpolation functions behave as intended", {
mutate(breaklines = array("LINESTRING EMPTY"))

triangulation_sdf <- sdf %>%
mutate(triangles = st_triangulate(masspoints, breaklines, as.double(0.00), as.double(0.01)))
mutate(triangles = st_triangulate(masspoints, breaklines, as.double(0.00), as.double(0.01), "NONENCROACHING"))

expect_equal(sdf_nrow(triangulation_sdf), 2)

Expand All @@ -144,16 +144,17 @@ test_that ("triangulation and interpolation functions behave as intended", {

interpolation_sdf <- sdf %>%
mutate(
origin = st_geomfromwkt("POINT (0.6 1.8)"),
origin = st_geomfromwkt("POINT (0.55 1.75)"),
xWidth = 12L,
yWidth = 6L,
xSize = as.double(0.1),
ySize = as.double(0.1),
interpolated = st_interpolateelevation(
masspoints,
breaklines,
as.double(0.0),
as.double(0.01),
as.double(0.01),
"NONENCROACHING",
origin,
xWidth,
yWidth,
Expand All @@ -163,5 +164,5 @@ test_that ("triangulation and interpolation functions behave as intended", {
)
expect_equal(sdf_nrow(interpolation_sdf), 6 * 12)
expect_contains(sdf_collect(interpolation_sdf)$interpolated,
"POINT Z(0.6 2 1.8)")
"POINT Z(1.6 1.8 1.2)")
})
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,6 @@ class MosaicContext(indexSystem: IndexSystem, geometryAPI: GeometryAPI) extends
ColumnAdapter(ST_HasValidCoordinates(geom.expr, crsCode.expr, which.expr, expressionConfig))
def st_interpolateelevation(pointsArray: Column, linesArray: Column, mergetol: Column, snaptol: Column, splitPointFinder: Column, origin: Column, xWidth: Column, yWidth: Column, xSize: Column, ySize: Column): Column =
ColumnAdapter(geometry.ST_InterpolateElevation(pointsArray.expr, linesArray.expr, mergetol.expr, snaptol.expr, splitPointFinder.expr, origin.expr, xWidth.expr, yWidth.expr, xSize.expr, ySize.expr, expressionConfig))
def st_interpolateelevation(pointsArray: Column, linesArray: Column, mergetol: Column, snaptol: Column, origin: Column, xWidth: Column, yWidth: Column, xSize: Column, ySize: Column): Column =
ColumnAdapter(geometry.ST_InterpolateElevation(pointsArray.expr, linesArray.expr, mergetol.expr, snaptol.expr, lit("NONENCROACHING").expr, origin.expr, xWidth.expr, yWidth.expr, xSize.expr, ySize.expr, expressionConfig))
def st_intersection(left: Column, right: Column): Column = ColumnAdapter(ST_Intersection(left.expr, right.expr, expressionConfig))
def st_isvalid(geom: Column): Column = ColumnAdapter(ST_IsValid(geom.expr, expressionConfig))
def st_length(geom: Column): Column = ColumnAdapter(ST_Length(geom.expr, expressionConfig))
Expand All @@ -651,8 +649,6 @@ class MosaicContext(indexSystem: IndexSystem, geometryAPI: GeometryAPI) extends
ColumnAdapter(ST_Translate(geom1.expr, xd.expr, yd.expr, expressionConfig))
def st_triangulate(pointsArray: Column, linesArray: Column, mergeTol: Column, snapTol: Column, splitPointFinder: Column): Column =
ColumnAdapter(ST_Triangulate(pointsArray.expr, linesArray.expr, mergeTol.expr, snapTol.expr, splitPointFinder.expr, expressionConfig))
def st_triangulate(pointsArray: Column, linesArray: Column, mergeTol: Column, snapTol: Column): Column =
ColumnAdapter(ST_Triangulate(pointsArray.expr, linesArray.expr, mergeTol.expr, snapTol.expr, lit("NONENCROACHING").expr, expressionConfig))
def st_x(geom: Column): Column = ColumnAdapter(ST_X(geom.expr, expressionConfig))
def st_y(geom: Column): Column = ColumnAdapter(ST_Y(geom.expr, expressionConfig))
def st_z(geom: Column): Column = ColumnAdapter(ST_Z(geom.expr, expressionConfig))
Expand Down Expand Up @@ -710,8 +706,6 @@ class MosaicContext(indexSystem: IndexSystem, geometryAPI: GeometryAPI) extends
def rst_convolve(raster: Column, kernel: Column): Column = ColumnAdapter(RST_Convolve(raster.expr, kernel.expr, expressionConfig))
def rst_dtmfromgeoms(pointsArray: Column, linesArray: Column, mergeTol: Column, snapTol: Column, splitPointFinder: Column, origin: Column, xWidth: Column, yWidth: Column, xSize: Column, ySize: Column, noData: Column): Column =
ColumnAdapter(RST_DTMFromGeoms(pointsArray.expr, linesArray.expr, mergeTol.expr, snapTol.expr, splitPointFinder.expr, origin.expr, xWidth.expr, yWidth.expr, xSize.expr, ySize.expr, noData.expr, expressionConfig))
def rst_dtmfromgeoms(pointsArray: Column, linesArray: Column, mergeTol: Column, snapTol: Column, origin: Column, xWidth: Column, yWidth: Column, xSize: Column, ySize: Column, noData: Column): Column =
ColumnAdapter(RST_DTMFromGeoms(pointsArray.expr, linesArray.expr, mergeTol.expr, snapTol.expr, lit("NONENCROACHING").expr, origin.expr, xWidth.expr, yWidth.expr, xSize.expr, ySize.expr, noData.expr, expressionConfig))
def rst_pixelcount(raster: Column): Column = ColumnAdapter(RST_PixelCount(raster.expr, lit(false).expr, lit(false).expr, expressionConfig))
def rst_pixelcount(raster: Column, countNoData: Column): Column = ColumnAdapter(RST_PixelCount(raster.expr, countNoData.expr, lit(false).expr, expressionConfig))
def rst_pixelcount(raster: Column, countNoData: Column, countAll: Column): Column = ColumnAdapter(RST_PixelCount(raster.expr, countNoData.expr, countAll.expr, expressionConfig))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.databricks.labs.mosaic.expressions.geometry

import com.databricks.labs.mosaic.core.geometry.api.GeometryAPI
import com.databricks.labs.mosaic.core.index.IndexSystem
import com.databricks.labs.mosaic.core.types.model.TriangulationSplitPointTypeEnum
import com.databricks.labs.mosaic.functions.MosaicContext
import com.databricks.labs.mosaic.functions.MosaicRegistryBehaviors.mosaicContext
import org.apache.spark.sql.functions._
Expand All @@ -22,6 +23,7 @@ trait ST_InterpolateElevationBehaviours extends QueryTest {
val ySize = -1.0
val mergeTolerance = 0.0
val snapTolerance = 0.01
val splitPointFinder = TriangulationSplitPointTypeEnum.NONENCROACHING
val origin = "POINT(348000 462000)"

def simpleInterpolationBehavior(indexSystem: IndexSystem, geometryAPI: GeometryAPI): Unit = {
Expand All @@ -45,18 +47,22 @@ trait ST_InterpolateElevationBehaviours extends QueryTest {
.withColumn("breaklines", array().cast(ArrayType(StringType)))
.withColumn("mergeTolerance", lit(mergeTolerance))
.withColumn("snapTolerance", lit(snapTolerance))
.withColumn("splitPointFinder", lit(splitPointFinder.toString))
.withColumn("origin", st_geomfromwkt(lit(origin)))
.withColumn("grid_size_x", lit(xWidth))
.withColumn("grid_size_y", lit(yWidth))
.withColumn("pixel_size_x", lit(xSize))
.withColumn("pixel_size_y", lit(ySize))
.withColumn("elevation", st_interpolateelevation(
$"masspoints", $"breaklines", $"mergeTolerance", $"snapTolerance",
$"masspoints", $"breaklines",
$"mergeTolerance", $"snapTolerance", $"splitPointFinder",
$"origin", $"grid_size_x", $"grid_size_y",
$"pixel_size_x", $"pixel_size_y"))
.drop(
$"masspoints", $"breaklines", $"mergeTolerance", $"snapTolerance", $"origin",
$"grid_size_x", $"grid_size_y", $"pixel_size_x", $"pixel_size_y"
$"masspoints", $"breaklines",
$"mergeTolerance", $"snapTolerance", $"splitPointFinder",
$"origin", $"grid_size_x", $"grid_size_y",
$"pixel_size_x", $"pixel_size_y"
)
noException should be thrownBy result.collect()
result.count() shouldBe 1000000L
Expand Down Expand Up @@ -93,19 +99,23 @@ trait ST_InterpolateElevationBehaviours extends QueryTest {
.crossJoin(linesDf)
.withColumn("mergeTolerance", lit(mergeTolerance))
.withColumn("snapTolerance", lit(snapTolerance))
.withColumn("splitPointFinder", lit(splitPointFinder.toString))
.withColumn("origin", st_geomfromwkt(lit(origin)))
.withColumn("grid_size_x", lit(xWidth))
.withColumn("grid_size_y", lit(yWidth))
.withColumn("pixel_size_x", lit(xSize))
.withColumn("pixel_size_y", lit(ySize))
.withColumn("interpolated_grid_point", st_interpolateelevation(
$"masspoints", $"breaklines",$"mergeTolerance", $"snapTolerance",
$"masspoints", $"breaklines",
$"mergeTolerance", $"snapTolerance", $"splitPointFinder",
$"origin", $"grid_size_x", $"grid_size_y",
$"pixel_size_x", $"pixel_size_y"))
.withColumn("elevation", st_z($"interpolated_grid_point"))
.drop(
$"masspoints", $"breaklines", $"mergeTolerance", $"snapTolerance", $"origin",
$"grid_size_x", $"grid_size_y", $"pixel_size_x", $"pixel_size_y"
$"masspoints", $"breaklines",
$"mergeTolerance", $"snapTolerance", $"splitPointFinder",
$"origin", $"grid_size_x", $"grid_size_y",
$"pixel_size_x", $"pixel_size_y"
)
.cache()
noException should be thrownBy result.collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.databricks.labs.mosaic.expressions.geometry

import com.databricks.labs.mosaic.core.geometry.api.GeometryAPI
import com.databricks.labs.mosaic.core.index.IndexSystem
import com.databricks.labs.mosaic.core.types.model.TriangulationSplitPointTypeEnum
import com.databricks.labs.mosaic.functions.MosaicContext
import org.apache.spark.sql.QueryTest
import org.apache.spark.sql.functions._
Expand All @@ -18,6 +19,7 @@ trait ST_TriangulateBehaviours extends QueryTest {
val buffer = 50.0
val mergeTolerance = 1e-2
val snapTolerance = 0.01
val splitPointFinder = TriangulationSplitPointTypeEnum.NONENCROACHING

def simpleTriangulateBehavior(indexSystem: IndexSystem, geometryAPI: GeometryAPI): Unit = {

Expand All @@ -38,7 +40,7 @@ trait ST_TriangulateBehaviours extends QueryTest {
.groupBy()
.agg(collect_list($"geom_0").as("masspoints"))
.withColumn("breaklines", array().cast(ArrayType(StringType)))
.withColumn("mesh", st_triangulate($"masspoints", $"breaklines", lit(mergeTolerance), lit(snapTolerance)))
.withColumn("mesh", st_triangulate($"masspoints", $"breaklines", lit(mergeTolerance), lit(snapTolerance), lit(splitPointFinder.toString)))
.drop($"masspoints")
noException should be thrownBy result.collect()
result.count() shouldBe 4453
Expand Down Expand Up @@ -76,7 +78,7 @@ trait ST_TriangulateBehaviours extends QueryTest {
.groupBy()
.agg(collect_list($"geom_0").as("masspoints"))
.crossJoin(linesDf)
.withColumn("mesh", st_triangulate($"masspoints", $"breaklines", lit(mergeTolerance), lit(snapTolerance)))
.withColumn("mesh", st_triangulate($"masspoints", $"breaklines", lit(mergeTolerance), lit(snapTolerance), lit(splitPointFinder.toString)))
.drop($"masspoints", $"breaklines")

noException should be thrownBy result.collect()
Expand Down

0 comments on commit bc1b86f

Please sign in to comment.