Skip to content

Commit

Permalink
GIS #462: added ST_Equals
Browse files Browse the repository at this point in the history
  • Loading branch information
danylokravchenko committed Jan 16, 2024
1 parent 7699188 commit 7c72382
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public Expression implement( RexToLixTranslator translator, RexCall call, List<E
defineMethod( OperatorRegistry.get( OperatorName.ST_REVERSE ), BuiltInMethod.ST_REVERSE.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_BUFFER ), BuiltInMethod.ST_BUFFER.method, NullPolicy.STRICT );
// Spatial relationships
defineMethod( OperatorRegistry.get( OperatorName.ST_EQUALS ), BuiltInMethod.ST_EQUALS.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_DWITHIN ), BuiltInMethod.ST_DWITHIN.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_DISJOINT ), BuiltInMethod.ST_DISJOINT.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_TOUCHES ), BuiltInMethod.ST_TOUCHES.method, NullPolicy.STRICT );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,11 @@ public enum OperatorName {

// Spatial relationships

/**
* The <code>ST_Equals</code> operator function: check that two {@link org.polypheny.db.type.entity.spatial.PolyGeometry} are equals
*/
ST_EQUALS( Function.class ),

/**
* The <code>ST_DWithin</code> operator function: check if two {@link org.polypheny.db.type.entity.spatial.PolyGeometry} are withing the given distance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ public static PolyGeometry stBuffer( PolyGeometry geometry, PolyNumber distance,
*/


@SuppressWarnings("UnusedDeclaration")
public static PolyBoolean stEquals( PolyGeometry g1, PolyGeometry g2 ) {
restrictToSrid( g1, g2 );
return PolyBoolean.of( g1.equals( g2 ) );
}


@SuppressWarnings("UnusedDeclaration")
public static PolyBoolean stDWithin( PolyGeometry g1, PolyGeometry g2, PolyNumber distance ) {
restrictToSrid( g1, g2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,17 @@ private void readGeometry( int srid, GeometryReaderFunction readerFunction ) thr


protected PolyGeometryType getPolyGeometryType() {
switch ( jtsGeometry.getGeometryType() ) {
case Geometry.TYPENAME_POINT:
return PolyGeometryType.POINT;
case Geometry.TYPENAME_LINESTRING:
return PolyGeometryType.LINESTRING;
case Geometry.TYPENAME_LINEARRING:
return PolyGeometryType.LINEARRING;
case Geometry.TYPENAME_POLYGON:
return PolyGeometryType.POLYGON;
case Geometry.TYPENAME_GEOMETRYCOLLECTION:
return PolyGeometryType.GEOMETRYCOLLECTION;
case Geometry.TYPENAME_MULTIPOINT:
return PolyGeometryType.MULTIPOINT;
case Geometry.TYPENAME_MULTILINESTRING:
return PolyGeometryType.MULTILINESTRING;
case Geometry.TYPENAME_MULTIPOLYGON:
return PolyGeometryType.MULTIPOLYGON;
default:
throw new NotImplementedException( "value" );
}
return switch ( jtsGeometry.getGeometryType() ) {
case Geometry.TYPENAME_POINT -> PolyGeometryType.POINT;
case Geometry.TYPENAME_LINESTRING -> PolyGeometryType.LINESTRING;
case Geometry.TYPENAME_LINEARRING -> PolyGeometryType.LINEARRING;
case Geometry.TYPENAME_POLYGON -> PolyGeometryType.POLYGON;
case Geometry.TYPENAME_GEOMETRYCOLLECTION -> PolyGeometryType.GEOMETRYCOLLECTION;
case Geometry.TYPENAME_MULTIPOINT -> PolyGeometryType.MULTIPOINT;
case Geometry.TYPENAME_MULTILINESTRING -> PolyGeometryType.MULTILINESTRING;
case Geometry.TYPENAME_MULTIPOLYGON -> PolyGeometryType.MULTIPOLYGON;
default -> throw new NotImplementedException( "value" );
};
}


Expand Down Expand Up @@ -845,10 +836,9 @@ public PolyGeometry symDifference( @NotNull PolyGeometry g ) throws GeometryTopo
*/
@Override
public boolean equals( Object o ) {
if ( !(o instanceof PolyGeometry) ) {
if ( !(o instanceof PolyGeometry that) ) {
return false;
}
PolyGeometry that = (PolyGeometry) o;
return geometryType.equals( that.geometryType ) && jtsGeometry.equals( that.jtsGeometry ) && Objects.equals( SRID, that.SRID );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ public enum BuiltInMethod {
ST_REVERSE( GeoFunctions.class, "stReverse", PolyGeometry.class ),
ST_BUFFER( GeoFunctions.class, "stBuffer", PolyGeometry.class, PolyNumber.class ),
// Spatial relationships
ST_EQUALS( GeoFunctions.class, "stEquals", PolyGeometry.class, PolyGeometry.class ),
ST_DWITHIN( GeoFunctions.class, "stDWithin", PolyGeometry.class, PolyGeometry.class, PolyNumber.class ),
ST_DISJOINT( GeoFunctions.class, "stDisjoint", PolyGeometry.class, PolyGeometry.class ),
ST_TOUCHES( GeoFunctions.class, "stTouches", PolyGeometry.class, PolyGeometry.class ),
Expand Down
13 changes: 11 additions & 2 deletions dbms/src/test/java/org/polypheny/db/sql/fun/GeoFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private static void addTestData() throws SQLException {
try ( JdbcConnection jdbcConnection = new JdbcConnection( false ) ) {
Connection connection = jdbcConnection.getConnection();
try ( Statement statement = connection.createStatement() ) {
// statement.executeUpdate( "CREATE TABLE TEST_GIS(ID INTEGER NOT NULL, WKT VARCHAR, PRIMARY KEY (ID))" );
// statement.executeUpdate( "INSERT INTO TEST_GIS VALUES (1, SUBSTRING('POINT (7.852923 47.998949)', 1, 3))" );
statement.executeUpdate( "CREATE TABLE TEST_GIS(ID INTEGER NOT NULL, WKT GEOMETRY, PRIMARY KEY (ID))" );
statement.executeUpdate( "INSERT INTO TEST_GIS VALUES (1, ST_GeomFromText('POINT (7.852923 47.998949)', 4326))" );
statement.executeUpdate( "INSERT INTO TEST_GIS VALUES (2, ST_GeomFromText('POINT (9.289382 48.741588)', 4326))" );
Expand Down Expand Up @@ -236,6 +234,17 @@ public void spatialRelationsFunctions() throws SQLException {
try ( TestHelper.JdbcConnection polyphenyDbConnection = new TestHelper.JdbcConnection( true ) ) {
Connection connection = polyphenyDbConnection.getConnection();
try ( Statement statement = connection.createStatement() ) {
// check that two geo are equal
TestHelper.checkResultSet(
statement.executeQuery( "SELECT ST_Equals(ST_GeomFromText('POINT (7.852923 47.998949)', 4326), ST_GeomFromText('POINT (9.289382 48.741588)', 4326))" ),
ImmutableList.of(
new Object[]{ false }
) );
TestHelper.checkResultSet(
statement.executeQuery( "SELECT ST_Equals(ST_GeomFromText('POINT (7.852923 47.998949)', 4326), ST_GeomFromText('POINT (7.852923 47.998949)', 4326))" ),
ImmutableList.of(
new Object[]{ true }
) );
// check that geo are within the distance
TestHelper.checkResultSet(
statement.executeQuery( "SELECT ST_DWithin(ST_GeomFromText('POINT (7.852923 47.998949)', 4326), ST_GeomFromText('POINT (9.289382 48.741588)', 4326), 135000)" ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,16 @@ public void unparse( SqlWriter writer, SqlCall call, int leftPrec, int rightPrec

// Spatial relationships

register(
OperatorName.ST_EQUALS,
new SqlFunction(
"ST_EQUALS",
Kind.GEO,
ReturnTypes.BOOLEAN,
InferTypes.GEOMETRY,
OperandTypes.GEOMETRY_GEOMETRY,
FunctionCategory.GEOMETRY ) );

register(
OperatorName.ST_DWITHIN,
new SqlFunction(
Expand Down

0 comments on commit 7c72382

Please sign in to comment.