jiayuasu commented on code in PR #3062:
URL: https://github.com/apache/sedona/pull/3062#discussion_r3444805728


##########
flink/src/main/java/org/apache/sedona/flink/expressions/geography/GeographyConstructors.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.flink.expressions;
+
+import org.apache.flink.table.annotation.DataTypeHint;
+import org.apache.flink.table.functions.ScalarFunction;
+import org.apache.sedona.common.S2Geography.Geography;
+import org.apache.sedona.common.geography.Constructors;
+import org.apache.sedona.flink.GeographyTypeSerializer;

Review Comment:
   Moved GeographyConstructors into the 
org.apache.sedona.flink.expressions.geography sub-package, so the simple name 
no longer collides with expressions.Constructors (the geometry constructors) 
and the import is unambiguous. Catalog.java imports the sub-package explicitly 
since the wildcard import is not recursive.



##########
flink/src/test/java/org/apache/sedona/flink/GeographyConstructorTest.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.flink;
+
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.table.api.Expressions.call;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Collections;
+import java.util.List;
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.types.Row;
+import org.apache.sedona.common.S2Geography.Geography;
+import org.apache.sedona.common.geography.Constructors;
+import org.apache.sedona.flink.expressions.GeographyConstructors;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.io.WKBWriter;
+import org.locationtech.jts.io.WKTReader;
+
+public class GeographyConstructorTest extends TestBase {
+
+  @BeforeClass
+  public static void onceExecutedBeforeAll() {
+    initialize();
+  }
+
+  /** Build a single-row, single-column source table. */
+  private Table sourceTable(String colName, Object value, TypeInformation<?> 
colType) {
+    List<Row> data = Collections.singletonList(Row.of(value));
+    RowTypeInfo typeInfo =
+        new RowTypeInfo(new TypeInformation<?>[] {colType}, new String[] 
{colName});
+    DataStream<Row> ds = env.fromCollection(data).returns(typeInfo);
+    return tableEnv.fromDataStream(ds);
+  }
+
+  private Geography evalGeog(Table source, String func, String inputCol) {
+    Table out = source.select(call(func, $(inputCol)).as("geog"));
+    return first(out).getFieldAs("geog");
+  }
+
+  @Test
+  public void testGeogFromWKT() throws Exception {
+    Table src = sourceTable("wkt", "POINT (1 2)", 
BasicTypeInfo.STRING_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), "wkt");
+    assertEquals(Constructors.geogFromWKT("POINT (1 2)", 0).toEWKT(), 
result.toEWKT());
+  }
+
+  @Test
+  public void testGeogFromWKTWithSrid() throws Exception {
+    Table src = sourceTable("wkt", "POINT (1 2)", 
BasicTypeInfo.STRING_TYPE_INFO);
+    Table out =
+        src.select(
+            call(
+                    GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(),
+                    $("wkt"),
+                    org.apache.flink.table.api.Expressions.lit(4326))
+                .as("geog"));
+    Geography result = first(out).getFieldAs("geog");
+    assertEquals(4326, result.getSRID());
+    assertEquals(Constructors.geogFromWKT("POINT (1 2)", 4326).toEWKT(), 
result.toEWKT());
+  }
+
+  @Test
+  public void testGeogFromText() throws Exception {
+    Table src = sourceTable("wkt", "LINESTRING (0 0, 1 1, 2 2)", 
BasicTypeInfo.STRING_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogFromText.class.getSimpleName(), "wkt");
+    assertEquals(
+        Constructors.geogFromWKT("LINESTRING (0 0, 1 1, 2 2)", 0).toEWKT(), 
result.toEWKT());
+  }
+
+  @Test
+  public void testGeogFromEWKT() throws Exception {
+    Table src = sourceTable("ewkt", "SRID=4326;POINT (1 2)", 
BasicTypeInfo.STRING_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogFromEWKT.class.getSimpleName(), "ewkt");
+    assertEquals(4326, result.getSRID());
+    assertEquals(Constructors.geogFromEWKT("SRID=4326;POINT (1 2)").toEWKT(), 
result.toEWKT());
+  }
+
+  @Test
+  public void testGeogCollFromText() throws Exception {
+    String wkt = "GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (0 0, 1 1))";
+    Table src = sourceTable("wkt", wkt, BasicTypeInfo.STRING_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogCollFromText.class.getSimpleName(), "wkt");
+    assertEquals(Constructors.geogCollFromText(wkt, 0).toEWKT(), 
result.toEWKT());
+  }
+
+  @Test
+  public void testGeogFromWKB() throws Exception {
+    Geometry point = new WKTReader().read("POINT (1 2)");
+    byte[] wkb = new WKBWriter().write(point);
+    Table src = sourceTable("wkb", wkb, 
PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogFromWKB.class.getSimpleName(), "wkb");
+    assertEquals(Constructors.geogFromWKB(wkb, 0).toEWKT(), result.toEWKT());
+  }
+
+  @Test
+  public void testGeogFromEWKB() throws Exception {
+    Geometry point = new WKTReader().read("POINT (3 4)");
+    byte[] wkb = new WKBWriter().write(point);
+    Table src = sourceTable("wkb", wkb, 
PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO);
+    Geography result =
+        evalGeog(src, 
GeographyConstructors.ST_GeogFromEWKB.class.getSimpleName(), "wkb");
+    assertEquals(Constructors.geogFromWKB(wkb).toEWKT(), result.toEWKT());
+  }

Review Comment:
   Updated testGeogFromEWKB to write EWKB carrying the SRID (new WKBWriter(2, 
true) after setSRID(4326)) and to assert result.getSRID() == 4326, so it now 
validates SRID extraction end-to-end.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to