-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Enable spatial join for spherical geography ST_Distance #14137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rschlussel
merged 4 commits into
prestodb:master
from
jagill:enable_spherical_geometry_joins
Mar 25, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd80362
Extract some spherical geometry functions to helper class
jagill e0789f1
Enable spatial join for spherical geography ST_Distance
jagill 1588c69
Extract spherical geography functions to SphericalGeoFunctions
jagill b79965b
Add partitioned spherical spatial joins
jagill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...spatial-toolkit/src/main/java/com/facebook/presto/geospatial/SphericalGeographyUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Licensed 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 | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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 com.facebook.presto.geospatial; | ||
|
|
||
| import com.esri.core.geometry.Point; | ||
| import com.esri.core.geometry.ogc.OGCGeometry; | ||
| import com.facebook.presto.spi.PrestoException; | ||
| import com.google.common.base.Joiner; | ||
|
|
||
| import java.util.EnumSet; | ||
| import java.util.Set; | ||
|
|
||
| import static com.facebook.presto.geospatial.GeometryType.POINT; | ||
| import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT; | ||
| import static java.lang.Math.atan2; | ||
| import static java.lang.Math.cos; | ||
| import static java.lang.Math.sin; | ||
| import static java.lang.Math.sqrt; | ||
| import static java.lang.Math.toRadians; | ||
| import static java.lang.String.format; | ||
|
|
||
| public class SphericalGeographyUtils | ||
| { | ||
| public static final double EARTH_RADIUS_KM = 6371.01; | ||
| public static final double EARTH_RADIUS_M = EARTH_RADIUS_KM * 1000.0; | ||
| private static final float MIN_LATITUDE = -90; | ||
| private static final float MAX_LATITUDE = 90; | ||
| private static final float MIN_LONGITUDE = -180; | ||
| private static final float MAX_LONGITUDE = 180; | ||
| private static final Joiner OR_JOINER = Joiner.on(" or "); | ||
| private static final Set<GeometryType> ALLOWED_SPHERICAL_DISTANCE_TYPES = EnumSet.of(POINT); | ||
|
|
||
| private SphericalGeographyUtils() {} | ||
|
|
||
| public static void checkLatitude(double latitude) | ||
| { | ||
| if (Double.isNaN(latitude) || Double.isInfinite(latitude) || latitude < MIN_LATITUDE || latitude > MAX_LATITUDE) { | ||
| throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Latitude must be between -90 and 90"); | ||
| } | ||
| } | ||
|
|
||
| public static void checkLongitude(double longitude) | ||
| { | ||
| if (Double.isNaN(longitude) || Double.isInfinite(longitude) || longitude < MIN_LONGITUDE || longitude > MAX_LONGITUDE) { | ||
| throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Longitude must be between -180 and 180"); | ||
| } | ||
| } | ||
|
|
||
| public static Double sphericalDistance(OGCGeometry leftGeometry, OGCGeometry rightGeometry) | ||
| { | ||
| if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| validateSphericalType("ST_Distance", leftGeometry, ALLOWED_SPHERICAL_DISTANCE_TYPES); | ||
| validateSphericalType("ST_Distance", rightGeometry, ALLOWED_SPHERICAL_DISTANCE_TYPES); | ||
| Point leftPoint = (Point) leftGeometry.getEsriGeometry(); | ||
| Point rightPoint = (Point) rightGeometry.getEsriGeometry(); | ||
|
|
||
| // greatCircleDistance returns distance in KM. | ||
| return greatCircleDistance(leftPoint.getY(), leftPoint.getX(), rightPoint.getY(), rightPoint.getX()) * 1000; | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the distance between two points on Earth. | ||
| * | ||
| * This assumes a spherical Earth, and uses the Vincenty formula. | ||
| * (https://en.wikipedia.org/wiki/Great-circle_distance) | ||
| */ | ||
| public static double greatCircleDistance( | ||
| double latitude1, | ||
| double longitude1, | ||
| double latitude2, | ||
| double longitude2) | ||
| { | ||
| checkLatitude(latitude1); | ||
| checkLongitude(longitude1); | ||
| checkLatitude(latitude2); | ||
| checkLongitude(longitude2); | ||
|
|
||
| double radianLatitude1 = toRadians(latitude1); | ||
| double radianLatitude2 = toRadians(latitude2); | ||
|
|
||
| double sin1 = sin(radianLatitude1); | ||
| double cos1 = cos(radianLatitude1); | ||
| double sin2 = sin(radianLatitude2); | ||
| double cos2 = cos(radianLatitude2); | ||
|
|
||
| double deltaLongitude = toRadians(longitude1) - toRadians(longitude2); | ||
| double cosDeltaLongitude = cos(deltaLongitude); | ||
|
|
||
| double t1 = cos2 * sin(deltaLongitude); | ||
| double t2 = cos1 * sin2 - sin1 * cos2 * cosDeltaLongitude; | ||
| double t3 = sin1 * sin2 + cos1 * cos2 * cosDeltaLongitude; | ||
| return atan2(sqrt(t1 * t1 + t2 * t2), t3) * EARTH_RADIUS_KM; | ||
| } | ||
|
|
||
| public static void validateSphericalType(String function, OGCGeometry geometry, Set<GeometryType> validTypes) | ||
| { | ||
| GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType()); | ||
| if (!validTypes.contains(type)) { | ||
| throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("When applied to SphericalGeography inputs, %s only supports %s. Input type is: %s", function, OR_JOINER.join(validTypes), type)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.