Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheinbergon committed Aug 23, 2024
1 parent c1bda1a commit 58c432e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<carrotsearch.version>0.7.0</carrotsearch.version>
<arrow-memory-netty.version>12.0.1</arrow-memory-netty.version>
</properties>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.2-SNAPSHOT</version>
<name>dremio-udf-gis</name>
<description>GIS UDF extensions for Dremio</description>
<url>https://github.com/sheinbergon/dremio-udf-gis</url>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sheinbergon/dremio/udf/gis/STIsSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.dremio.exec.expr.annotations.FunctionTemplate;
import com.dremio.exec.expr.annotations.Output;
import com.dremio.exec.expr.annotations.Param;
import org.locationtech.jts.operation.valid.IsSimpleOp;

@FunctionTemplate(
name = "ST_IsSimple",
Expand All @@ -39,7 +40,7 @@ public void setup() {
public void eval() {
if (org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.isHolderSet(binaryInput)) {
org.locationtech.jts.geom.Geometry geom = org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.toGeometry(binaryInput);
boolean result = geom.isSimple();
boolean result = org.locationtech.jts.operation.valid.IsSimpleOp.isSimple(geom);
org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.setBooleanValue(output, result);
} else {
org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.markHolderNotSet(output);
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/sheinbergon/dremio/udf/gis/STSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.dremio.exec.expr.annotations.FunctionTemplate;
import com.dremio.exec.expr.annotations.Output;
import com.dremio.exec.expr.annotations.Param;
import org.apache.arrow.memory.ArrowBuf;

import javax.inject.Inject;

Expand All @@ -31,25 +30,29 @@
nulls = FunctionTemplate.NullHandling.INTERNAL,
costCategory = FunctionTemplate.FunctionCostCategory.COMPLEX)
public class STSimplify implements SimpleFunction {

@Param
org.apache.arrow.vector.holders.NullableVarBinaryHolder binaryInput;

@Param(constant = true)
@Param
org.apache.arrow.vector.holders.Float8Holder toleranceInput;

@Output
org.apache.arrow.vector.holders.NullableVarBinaryHolder binaryOutput;

@Inject
ArrowBuf buffer;
org.apache.arrow.memory.ArrowBuf buffer;

public void setup() {
}

public void eval() {
if (org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.isHolderSet(binaryInput)) {
double tolerance = toleranceInput.value;
org.locationtech.jts.geom.Geometry geom = org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.toGeometry(binaryInput);
org.locationtech.jts.geom.Geometry simplified = org.locationtech.jts.simplify.DouglasPeuckerSimplifier.simplify(geom, toleranceInput.value);
org.locationtech.jts.simplify.DouglasPeuckerSimplifier simplifier = new org.locationtech.jts.simplify.DouglasPeuckerSimplifier(geom);
simplifier.setDistanceTolerance(tolerance);
org.locationtech.jts.geom.Geometry simplified = simplifier.getResultGeometry();
simplified.setSRID(geom.getSRID());
byte[] bytes = org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.toEWKB(simplified);
buffer = buffer.reallocIfNeeded(bytes.length);
Expand All @@ -58,4 +61,4 @@ public void eval() {
org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.markHolderNotSet(binaryOutput);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sheinbergon.dremio.udf.gis;

import com.dremio.exec.expr.SimpleFunction;
import com.dremio.exec.expr.annotations.FunctionTemplate;
import com.dremio.exec.expr.annotations.Output;
import com.dremio.exec.expr.annotations.Param;

import javax.inject.Inject;

@FunctionTemplate(
name = "ST_SimplifyPreserveTopology",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = FunctionTemplate.NullHandling.INTERNAL,
costCategory = FunctionTemplate.FunctionCostCategory.COMPLEX)
public class STSimplifyPreserveTopology implements SimpleFunction {

@Param
org.apache.arrow.vector.holders.NullableVarBinaryHolder binaryInput;

@Param
org.apache.arrow.vector.holders.Float8Holder toleranceInput;

@Output
org.apache.arrow.vector.holders.NullableVarBinaryHolder binaryOutput;

@Inject
org.apache.arrow.memory.ArrowBuf buffer;

public void setup() {
}

public void eval() {
if (org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.isHolderSet(binaryInput)) {
double tolerance = toleranceInput.value;
org.locationtech.jts.geom.Geometry geom = org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.toGeometry(binaryInput);
org.locationtech.jts.simplify.TopologyPreservingSimplifier simplifier = new org.locationtech.jts.simplify.TopologyPreservingSimplifier(geom);
simplifier.setDistanceTolerance(tolerance);
org.locationtech.jts.geom.Geometry result = simplifier.getResultGeometry();
byte[] bytes = org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.toEWKB(result);
buffer = buffer.reallocIfNeeded(bytes.length);
org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.populate(bytes, buffer, binaryOutput);
} else {
org.sheinbergon.dremio.udf.gis.util.GeometryHelpers.markHolderNotSet(binaryOutput);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.sheinbergon.dremio.udf.gis

import org.apache.arrow.vector.holders.Float8Holder
import org.apache.arrow.vector.holders.NullableVarBinaryHolder
import org.sheinbergon.dremio.udf.gis.spec.GeometryProcessingFunSpec
import org.sheinbergon.dremio.udf.gis.util.allocateBuffer
import org.sheinbergon.dremio.udf.gis.util.reset

internal class STSimplifyPreserveTopologyTests : GeometryProcessingFunSpec<STSimplifyPreserveTopology>() {

init {

beforeEach {
function.toleranceInput.reset()
}

testGeometryProcessing(
name = "Calling ST_SimplifyPreserveTopology on a MULTILINESTRING with a tolerance of 40.0",
wkt = "MULTILINESTRING ((20 180, 20 150, 50 150, 50 100, 110 150, 150 140, 170 120), (20 10, 80 30, 90 120), (90 120, 130 130), (130 130, 130 70, 160 40, 180 60, 180 90, 140 80), (50 40, 70 40, 80 70, 70 60, 60 60, 50 50, 50 40))",
expected = " MULTILINESTRING((20 180,50 100,110 150,170 120),(20 10,90 120),(90 120,130 130),(130 130,130 70,160 40,180 90,140 80),(50 40,70 40,80 70,60 60,50 40))"
) { function.toleranceInput.value = 40.0 }

testNullGeometryProcessing(
"Calling ST_SimplifyPreserveTopology on a NULL input"
)
}

override val function = STSimplifyPreserveTopology().apply {
binaryInput = NullableVarBinaryHolder()
toleranceInput = Float8Holder()
binaryOutput = NullableVarBinaryHolder()
buffer = allocateBuffer()
}

override val STSimplifyPreserveTopology.wkbInput: NullableVarBinaryHolder get() = function.binaryInput
override val STSimplifyPreserveTopology.wkbOutput: NullableVarBinaryHolder get() = function.binaryOutput
}

0 comments on commit 58c432e

Please sign in to comment.