You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "zhangfengcdt (via GitHub)" <gi...@apache.org> on 2026/04/22 16:15:53 UTC

[PR] [GH-2830] Adds Geography dual-dispatch to ST_Centroid [sedona]

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

   ## Did you read the Contributor Guide?
   
   - Yes, I have read the [Contributor Rules](https://sedona.apache.org/latest/community/rule/) and [Contributor Development Guide](https://sedona.apache.org/latest/community/develop/)
   
   ## Is this PR related to a ticket?
   
   - Yes, and the PR name follows the format `[GH-XXX] my subject`. Closes #<issue_number>
   
   ## What changes were proposed in this PR?
     - Adds Geography dual-dispatch to ST_Centroid — returns a Geography point for Geography inputs (planar centroid, computed in projected lon/lat space, not on the sphere).
     - Follow-up to #2831; L1 tier (delegates to JTS getCentroid() and re-wraps the result via WKBGeography.fromJTS).
   
   ## How was this patch tested?
     - mvn clean test -pl common -am -Dtest=FunctionTest
     - New Spark SQL case in GeographyFunctionTest
     - mvn spotless:apply run before commit; no format violations remain.
   
   ## Did this PR include necessary documentation updates?
   
   - Yes, I have updated the documentation.
   


-- 
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: issues-unsubscribe@sedona.apache.org

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


Re: [PR] [GH-2830] Adds Geography dual-dispatch to ST_Centroid [sedona]

Posted by "zhangfengcdt (via GitHub)" <gi...@apache.org>.
zhangfengcdt commented on PR #2852:
URL: https://github.com/apache/sedona/pull/2852#issuecomment-4337069361

   > @zhangfengcdt Are you sure this should go back to JTS and use planar geometry to get the centroid? Does S2 have something spherical?
   
   Yes, you're right! I switched it to S2, need a bit more work on that though - S2 returns a non-unit weighted vector, so the result is normalized via S2Point.normalize before being wrapped as SinglePointGeography. 


-- 
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: issues-unsubscribe@sedona.apache.org

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


Re: [PR] [GH-2830] Adds Geography dual-dispatch to ST_Centroid [sedona]

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


##########
spark/common/src/test/scala/org/apache/sedona/sql/geography/GeographyFunctionTest.scala:
##########
@@ -87,6 +88,16 @@ class GeographyFunctionTest extends TestBaseScala {
         .first()
       assertEquals(3, row.getInt(0))
     }
+
+    it("ST_Centroid square polygon") {
+      val row = sparkSession
+        .sql("SELECT ST_AsText(ST_Centroid(ST_GeogFromWKT('POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))', 4326))) AS c")
+        .first()
+      val point = new WKTReader().read(row.getString(0)).asInstanceOf[Point]
+      // Planar centroid of unit square from (0,0) to (2,2) is (1,1)

Review Comment:
   The comment says "unit square" but the polygon spans (0,0) to (2,2), i.e., a 2×2 square. Adjusting the wording will avoid confusion when reading the test.



##########
docs/api/sql/geography/Geography-Functions/ST_Centroid.md:
##########
@@ -0,0 +1,42 @@
+<!--
+ 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.
+ -->
+
+# ST_Centroid
+
+Introduction: Returns the planar centroid of a geography object as a Geography point. Note that this computes the centroid in the projected (lon/lat) coordinate space, not on the sphere.
+
+Format:
+
+`ST_Centroid (A: Geography)`
+
+Return type: `Geography`
+
+Since: `v1.9.1`
+
+SQL Example
+
+```sql
+SELECT ST_AsText(ST_Centroid(ST_GeogFromWKT('POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))')));

Review Comment:
   This SQL example uses `ST_AsText(ST_Centroid(...))`, but `ST_Centroid` on a Geography returns a Geography, while `ST_AsText` is the Geometry WKT formatter. Use `ST_AsEWKT(ST_Centroid(...))`, or convert via `ST_AsText(ST_GeogToGeometry(ST_Centroid(...)))`, or just show `SELECT ST_Centroid(...)` output.



##########
spark/common/src/test/scala/org/apache/sedona/sql/geography/GeographyFunctionTest.scala:
##########
@@ -87,6 +88,16 @@ class GeographyFunctionTest extends TestBaseScala {
         .first()
       assertEquals(3, row.getInt(0))
     }
+
+    it("ST_Centroid square polygon") {
+      val row = sparkSession
+        .sql("SELECT ST_AsText(ST_Centroid(ST_GeogFromWKT('POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))', 4326))) AS c")

Review Comment:
   `ST_AsText` expects a Geometry input; here `ST_Centroid(ST_GeogFromWKT(...))` returns a Geography, so this will fail at runtime (child expression is `SerdeAware` and gets cast to `Geometry`). Consider converting first (`ST_AsText(ST_GeogToGeometry(ST_Centroid(...)))`) or asserting on the returned Geography directly (e.g., `row.get(0).asInstanceOf[Geography]`) / using `ST_AsEWKT`.



##########
spark/common/src/test/scala/org/apache/sedona/sql/geography/GeographyFunctionTest.scala:
##########
@@ -23,7 +23,8 @@ import org.apache.sedona.sql.TestBaseScala
 import org.apache.spark.sql.functions.{col, lit}
 import org.apache.spark.sql.sedona_sql.expressions.{st_constructors, st_functions, st_predicates}
 import org.junit.Assert.{assertEquals, assertNotNull, assertTrue}
-import org.locationtech.jts.geom.Geometry
+import org.locationtech.jts.geom.{Geometry, Point}

Review Comment:
   Unused import: `Geometry` is imported but not referenced anywhere in this test file. Removing it will keep the imports clean (and avoids warnings if the build treats them as errors).



-- 
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: issues-unsubscribe@sedona.apache.org

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


Re: [PR] [GH-2830] Adds Geography dual-dispatch to ST_Centroid [sedona]

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


-- 
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: issues-unsubscribe@sedona.apache.org

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


Re: [PR] [GH-2830] Adds Geography dual-dispatch to ST_Centroid [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on PR #2852:
URL: https://github.com/apache/sedona/pull/2852#issuecomment-4330716867

   @zhangfengcdt Are you sure this should go back to JTS and use planar geometry to get the centroid? Does S2 have something spherical?


-- 
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: issues-unsubscribe@sedona.apache.org

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