You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sedona.apache.org by "jiayuasu (via GitHub)" <gi...@apache.org> on 2023/05/20 09:11:20 UTC

[GitHub] [sedona] jiayuasu opened a new pull request, #835: [SEDONA-281] Support geodesic / geography functions

jiayuasu opened a new pull request, #835:
URL: https://github.com/apache/sedona/pull/835

   
   ## Did you read the Contributor Guide?
   
   - Yes, I have read [Contributor Rules](https://sedona.apache.org/latest-snapshot/community/rule/) and [Contributor Development Guide](https://sedona.apache.org/latest-snapshot/community/develop/)
   
   ## Is this PR related to a JIRA ticket?
   
   - Yes, the URL of the associated JIRA ticket is https://issues.apache.org/jira/browse/SEDONA-281. The PR name follows the format `[SEDONA-281] my subject`.
   
   
   ## What changes were proposed in this PR?
   
   ### ST_AreaSpheroid
   
   Return the geodesic area of A using WGS84 spheroid. Unit is meter. Works better for large geometries (country level) compared to `ST_Area` + `ST_Transform`. It is equivalent to PostGIS `ST_Area(geography, use_spheroid=true)` function and produces nearly identical results.
   
   ### ST_LengthSpheroid
   
   Return the geodesic perimeter of A using WGS84 spheroid. Unit is meter. Works better for large geometries (country level) compared to `ST_Length` + `ST_Transform`. It is equivalent to PostGIS `ST_Length(geography, use_spheroid=true)` and `ST_LengthSpheroid` function and produces nearly identical results.
   
   ### ST_DistanceSphere
   
   Return the haversine / great-circle distance of A using a given earth radius (default radius: 6378137.0). Unit is meter. Works better for large geometries (country level) compared to `ST_Distance` + `ST_Transform`. It is equivalent to PostGIS `ST_Distance(geography, use_spheroid=false)` and `ST_DistanceSphere` function and produces nearly identical results. It provides faster but less accurate result compared to `ST_DistanceSpheroid`.
   
   ### ST_DistanceSpheroid
   
   Return the geodesic distance of A using WGS84 spheroid. Unit is meter. Works better for large geometries (country level) compared to `ST_Distance` + `ST_Transform`. It is equivalent to PostGIS `ST_Distance(geography, use_spheroid=true)` and `ST_DistanceSpheroid` function and produces nearly identical results. It provides slower but more accurate result compared to `ST_DistanceSphere`.
   
   ## How was this patch tested?
   
   Added many unit tests.
   
   ## Did this PR include necessary documentation updates?
   
   - Yes, I am adding a new API. I am using the [current SNAPSHOT version number](https://github.com/apache/sedona/blob/master/pom.xml#L29) in since `vX.Y.Z` format.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [sedona] jiayuasu merged pull request #835: [SEDONA-281] Support geodesic / geography functions

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu merged PR #835:
URL: https://github.com/apache/sedona/pull/835


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [sedona] Kontinuation commented on a diff in pull request #835: [SEDONA-281] Support geodesic / geography functions

Posted by "Kontinuation (via GitHub)" <gi...@apache.org>.
Kontinuation commented on code in PR #835:
URL: https://github.com/apache/sedona/pull/835#discussion_r1209188035


##########
common/src/main/java/org/apache/sedona/common/sphere/Haversine.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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
+ *
+ *   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 org.apache.sedona.common.sphere;
+
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
+
+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;
+
+public class Haversine
+{
+    /**
+     * Calculate the distance between two points on the earth using the "haversine" formula.
+     * This is also known as the great-circle distance
+     * This will produce almost identical result to PostGIS ST_DistanceSphere and
+     * ST_Distance(useSpheroid=false)
+     * @param geom1 The first geometry. Each coordinate is in lat/lon order
+     * @param geom2 The second geometry. Each coordinate is in lat/lon order
+     * @return
+     */
+    public static double distance(Geometry geom1, Geometry geom2, double AVG_EARTH_RADIUS)
+    {
+        Coordinate coordinate1 = geom1.getGeometryType().equals("Point")? geom1.getCoordinate():geom1.getCentroid().getCoordinate();
+        Coordinate coordinate2 = geom2.getGeometryType().equals("Point")? geom2.getCoordinate():geom2.getCentroid().getCoordinate();
+        // Calculate the distance between the two points
+        double lat1 = coordinate1.getX();
+        double lon1 = coordinate1.getY();
+        double lat2 = coordinate2.getX();
+        double lon2 = coordinate2.getY();
+        double latDistance = toRadians(lat2 - lat1);
+        double lngDistance = toRadians(lon2 - lon1);
+        double a = sin(latDistance / 2) * sin(latDistance / 2)
+                + cos(toRadians(lat1)) * cos(toRadians(lat2))
+                * sin(lngDistance / 2) * sin(lngDistance / 2);
+        double c = 2 * atan2(sqrt(a), sqrt(1 - a));
+        return AVG_EARTH_RADIUS * c * 1.0;
+    }
+
+    // Calculate the distance between two points on the earth using the "haversine" formula.
+    // The radius of the earth is 6371.0 km
+    public static double distance(Geometry geom1, Geometry geom2)
+    {
+        return distance(geom1, geom2, 6378137.0);

Review Comment:
   I suggest using `6371008` as the default radius according to [PostGIS ST_DistanceSphere](https://postgis.net/docs/ST_DistanceSphere.html) and https://en.wikipedia.org/wiki/Earth_radius 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org