Skip to content

Commit

Permalink
GIS #462: added projection function; added spatial relationships func…
Browse files Browse the repository at this point in the history
…tions to SQL
  • Loading branch information
danylokravchenko committed Nov 11, 2023
1 parent ad719b3 commit 92b9d37
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ public Expression implement( RexToLixTranslator translator, RexCall call, List<E
defineMethod( OperatorRegistry.get( OperatorName.ST_CENTROID ), BuiltInMethod.ST_CENTROID.method, NullPolicy.STRICT );
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_WITHINDISTANCE ), BuiltInMethod.ST_WITHINDISTANCE.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 );
defineMethod( OperatorRegistry.get( OperatorName.ST_INTERSECTS ), BuiltInMethod.ST_INTERSECTS.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_CROSSES ), BuiltInMethod.ST_CROSSES.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_WITHIN ), BuiltInMethod.ST_WITHIN.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_CONTAINS ), BuiltInMethod.ST_CONTAINS.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_OVERLAPS ), BuiltInMethod.ST_OVERLAPS.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_COVERS ), BuiltInMethod.ST_COVERS.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_COVEREDBY ), BuiltInMethod.ST_COVEREDBY.method, NullPolicy.STRICT );
defineMethod( OperatorRegistry.get( OperatorName.ST_RELATE ), BuiltInMethod.ST_RELATE.method, NullPolicy.STRICT );
// Yield metric values
defineMethod( OperatorRegistry.get( OperatorName.ST_DISTANCE ), BuiltInMethod.ST_DISTANCE.method, NullPolicy.STRICT );
// on Points
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,65 @@ public enum OperatorName {
*/
ST_BUFFER( Function.class ),

// Spatial relationships

/**
* The <code>ST_WithinDistance</code> operator function: check if two {@link org.polypheny.db.type.entity.spatial.PolyGeometry} are withing the given distance
*/
ST_WITHINDISTANCE( Function.class ),

/**
* The <code>ST_Disjoint</code> operator function: check if two {@link org.polypheny.db.type.entity.spatial.PolyGeometry} are disjoint
*/
ST_DISJOINT( Function.class ),

/**
* The <code>ST_Touches</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} touches another
*/
ST_TOUCHES( Function.class ),

/**
* The <code>ST_Intersects</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} intersects another
*/
ST_INTERSECTS( Function.class ),

/**
* The <code>ST_Crosses</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} crosses another
*/
ST_CROSSES( Function.class ),

/**
* The <code>ST_Within</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} is within another
*/
ST_WITHIN( Function.class ),

/**
* The <code>ST_Contains</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} contains another
*/
ST_CONTAINS( Function.class ),

/**
* The <code>ST_Overlaps</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} overlaps another
*/
ST_OVERLAPS( Function.class ),

/**
* The <code>ST_Covers</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} covers another
*/
ST_COVERS( Function.class ),

/**
* The <code>ST_CoveredBy</code> operator function: check if {@link org.polypheny.db.type.entity.spatial.PolyGeometry} is covered by another
*/
ST_COVEREDBY( Function.class ),

/**
* The <code>ST_Relate</code> operator function: check if two {@link org.polypheny.db.type.entity.spatial.PolyGeometry} are relate
*/
ST_RELATE( Function.class ),

// Yield metric values

/**
* The <code>ST_Distance</code> operator function: compute the distance between two {@link org.polypheny.db.type.entity.spatial.PolyGeometry}
*/
Expand Down
88 changes: 87 additions & 1 deletion core/src/main/java/org/polypheny/db/functions/GeoFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,96 @@ public static PolyGeometry stBuffer( PolyGeometry geometry, PolyNumber distance,
}

/*
* TODO: Spatial relationships
* Spatial relationships
*/


@SuppressWarnings("UnusedDeclaration")
public static PolyBoolean stWithinDistance( PolyGeometry g1, PolyGeometry g2, PolyNumber distance ) {
restrictToSrid( g1, g2 );
try {
return PolyBoolean.of( g1.isWithinDistance( g2, distance.doubleValue() ) );
} catch ( GeometryTopologicalException e ) {
throw toUnchecked( e );
}
}


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


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


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


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


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


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


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


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


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


@SuppressWarnings("UnusedDeclaration")
public static PolyBoolean stRelate( PolyGeometry g1, PolyGeometry g2, PolyString pattern ) {
restrictToSrid( g1, g2 );
try {
return PolyBoolean.of( g1.relate( g2, pattern.value ) );
} catch ( GeometryTopologicalException e ) {
throw toUnchecked( e );
}
}



/*
* Yield metric values
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.polypheny.db.type.entity.spatial.PolyPoint;

/**
* Calculate the spherical distances between various geometries
* Calculate the spherical distances between various geometries on the <strong>PERFECT SPHERE</strong>
*/
public class GeoDistanceFunctions {

Expand All @@ -40,12 +40,30 @@ private GeoDistanceFunctions() {
}


/**
* Check that the distance between two geometries is within the given threshold
*
* @param g1 one geometry
* @param g2 another one
* @param distanceThreshold limit the distance between geometries
* @return <code>TRUE</code> if two geometries are within the given distance
* @throws GeometryTopologicalException if distance cannot be calculated
*/
public static boolean isWithinSphericalDistance( @NotNull PolyGeometry g1, @NotNull PolyGeometry g2, double distanceThreshold ) throws GeometryTopologicalException {
return sphericalDistance( g1, g2 ) <= distanceThreshold;
}


/**
* Calculate the spherical distance between 2 geometries
*
* @param g1 one geometry
* @param g2 another
* @return the distance between geometries
* @throws GeometryTopologicalException if distance cannot be calculated
*/
public static double sphericalDistance( @NotNull PolyGeometry g1, @NotNull PolyGeometry g2 ) throws GeometryTopologicalException {
// distance calculation are dependent on the type of the geometry
if ( g1.isPoint() && g2.isPoint() ) {
return calculateSphericalDistance( g1.asPoint(), g2.asPoint() );
} else if ( g1.isLineString() && g2.isLineString() ) {
Expand Down
Loading

0 comments on commit 92b9d37

Please sign in to comment.