Skip to content

Commit

Permalink
[SEDONA-303] Port all Sedona Spark function to Sedona Flink -- ST_Min…
Browse files Browse the repository at this point in the history
…imumBoundingRadius (#895)
  • Loading branch information
yyy1000 committed Jul 11, 2023
1 parent f0221db commit b93dfa3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions common/src/main/java/org/apache/sedona/common/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.apache.sedona.common;

import com.google.common.geometry.S2CellId;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.sedona.common.geometryObjects.Circle;
import org.apache.sedona.common.subDivide.GeometrySubDivider;
import org.apache.sedona.common.utils.GeomUtils;
Expand Down Expand Up @@ -536,6 +538,14 @@ public static Geometry minimumBoundingCircle(Geometry geometry, int quadrantSegm
return circle;
}

public static Pair<Geometry, Double> minimumBoundingRadius(Geometry geometry) {
MinimumBoundingCircle minimumBoundingCircle = new MinimumBoundingCircle(geometry);
Coordinate coods = minimumBoundingCircle.getCentre();
double radius = minimumBoundingCircle.getRadius();
Point centre = GEOMETRY_FACTORY.createPoint(coods);
return Pair.of(centre, radius);
}

public static Geometry lineSubString(Geometry geom, double fromFraction, double toFraction) {
double length = geom.getLength();
LengthIndexedLine indexedLine = new LengthIndexedLine(geom);
Expand Down
15 changes: 15 additions & 0 deletions common/src/test/java/org/apache/sedona/common/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,21 @@ public void force3DHybridGeomCollectionDefaultValue() {
assertEquals(wktWriter3D.write(expectedLineString3D), wktWriter3D.write(actualGeometryCollection.getGeometryN(0).getGeometryN(3)));
}

@Test
public void minimumBoundingRadius() {
Point point = GEOMETRY_FACTORY.createPoint(new Coordinate(0, 0));
assertEquals("POINT (0 0)", Functions.minimumBoundingRadius(point).getLeft().toString());
assertEquals(0, Functions.minimumBoundingRadius(point).getRight(), 1e-6);

LineString line = GEOMETRY_FACTORY.createLineString(coordArray(0, 0, 0, 10));
assertEquals("POINT (0 5)", Functions.minimumBoundingRadius(line).getLeft().toString());
assertEquals(5, Functions.minimumBoundingRadius(line).getRight(), 1e-6);

Polygon polygon = GEOMETRY_FACTORY.createPolygon(coordArray(0, 0, 0, 10, 10, 10, 10, 0, 0, 0));
assertEquals("POINT (5 5)", Functions.minimumBoundingRadius(polygon).getLeft().toString());
assertEquals(7.071067, Functions.minimumBoundingRadius(polygon).getRight(), 1e-6);
}

@Test
public void nRingsPolygonOnlyExternal() throws Exception {
Polygon polygon = GEOMETRY_FACTORY.createPolygon(coordArray(1, 0, 1, 1, 2, 1, 2, 0, 1, 0));
Expand Down
12 changes: 12 additions & 0 deletions docs/api/flink/Function.md
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,18 @@ Example:
```sql
SELECT ST_MinimumBoundingCircle(ST_GeomFromText('POLYGON((1 1,0 0, -1 1, 1 1))'))

## ST_MinimumBoundingRadius

Introduction: Returns a struct containing the center point and radius of the smallest circle that contains a geometry.

Format: `ST_MinimumBoundingRadius(geom: geometry)`

Since: `v1.5.0`

Example:
```sql
SELECT ST_MinimumBoundingRadius(ST_GeomFromText('POLYGON((1 1,0 0, -1 1, 1 1))'))
```

## ST_Multi

Expand Down
1 change: 1 addition & 0 deletions flink/src/main/java/org/apache/sedona/flink/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static UserDefinedFunction[] getFuncs() {
new Functions.ST_MakePolygon(),
new Functions.ST_MakeValid(),
new Functions.ST_MinimumBoundingCircle(),
new Functions.ST_MinimumBoundingRadius(),
new Functions.ST_Multi(),
new Functions.ST_StartPoint(),
new Functions.ST_SimplifyPreserveTopology(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.apache.sedona.flink.expressions;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.InputGroup;
import org.apache.flink.table.functions.ScalarFunction;
Expand Down Expand Up @@ -715,6 +716,14 @@ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.j
}
}

public static class ST_MinimumBoundingRadius extends ScalarFunction {
@DataTypeHint(value = "RAW")
public Pair<Geometry, Double> eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
Geometry geom = (Geometry) o;
return org.apache.sedona.common.Functions.minimumBoundingRadius(geom);
}
}

public static class ST_Multi extends ScalarFunction {
@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class)
public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
Expand Down
10 changes: 10 additions & 0 deletions flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.apache.sedona.flink;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.flink.table.api.Table;
import org.apache.sedona.flink.expressions.Functions;
import org.geotools.referencing.CRS;
Expand Down Expand Up @@ -804,6 +805,15 @@ public void testMinimumBoundingCircleWithQuadrantSegments() {
assertEquals(actual, expected);
}

@Test
public void testMinimumBoundingRadius() {
Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('LINESTRING (0 0, 1 0)') AS geom");
table = table.select(call(Functions.ST_MinimumBoundingRadius.class.getSimpleName(), $("geom")));
Pair<Geometry, Double> result = (Pair<Geometry, Double>) first(table).getField(0);
assertEquals("POINT (0.5 0)", result.getLeft().toString());
assertEquals(0.5, result.getRight(), 1e-6);
}

@Test
public void testMulti() {
Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('POINT (0 0)') AS geom");
Expand Down

0 comments on commit b93dfa3

Please sign in to comment.