szehon-ho commented on code in PR #12346: URL: https://github.com/apache/iceberg/pull/12346#discussion_r1984255343
########## api/src/main/java/org/apache/iceberg/types/EdgeInterpolationAlgorithm.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.iceberg.types; + +import java.util.Locale; + +/** The algorithm for interpolating edges. */ +public enum EdgeInterpolationAlgorithm { + /** Edges are interpolated as geodesics on a sphere. */ + SPHERICAL("spherical"), + /** See <a href="https://en.wikipedia.org/wiki/Vincenty%27s_formulae">Vincenty's formulae</a> */ + VINCENTY("vincenty"), + /** + * Thomas, Paul D. Spheroidal geodesics, reference systems, & local geometry. US Naval + * Oceanographic Office, 1970. + */ + THOMAS("thomas"), + /** + * Thomas, Paul D. Mathematical models for navigation systems. US Naval Oceanographic Office, + * 1965. + */ + ANDOYER("andoyer"), + /** + * <a href="https://link.springer.com/content/pdf/10.1007/s00190-012-0578-z.pdf">Karney, Charles + * FF. "Algorithms for geodesics." Journal of Geodesy 87 (2013): 43-55 </a>, and <a + * href="https://geographiclib.sourceforge.io/">GeographicLib</a>. + */ + KARNEY("karney"); + + private final String value; + + EdgeInterpolationAlgorithm(String value) { + this.value = value; + } + + public String value() { + return value; + } + + public static EdgeInterpolationAlgorithm fromName(String algorithmName) { + try { Review Comment: Precondition, check null and throw exception? ########## api/src/main/java/org/apache/iceberg/types/Types.java: ########## @@ -543,6 +561,120 @@ public int hashCode() { } } + public static class GeometryType extends PrimitiveType { + + private final String crs; + + private GeometryType(String crs) { + this.crs = crs; + } + + public static GeometryType get() { + return of(""); + } + + public static GeometryType of(String crs) { + return new GeometryType(crs == null ? "" : crs); + } + + @Override + public TypeID typeId() { + return TypeID.GEOMETRY; + } + + public String crs() { Review Comment: maybe can annotate NotNull? ########## api/src/main/java/org/apache/iceberg/types/EdgeInterpolationAlgorithm.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.iceberg.types; + +import java.util.Locale; + +/** The algorithm for interpolating edges. */ +public enum EdgeInterpolationAlgorithm { + /** Edges are interpolated as geodesics on a sphere. */ + SPHERICAL("spherical"), + /** See <a href="https://en.wikipedia.org/wiki/Vincenty%27s_formulae">Vincenty's formulae</a> */ + VINCENTY("vincenty"), + /** + * Thomas, Paul D. Spheroidal geodesics, reference systems, & local geometry. US Naval + * Oceanographic Office, 1970. + */ + THOMAS("thomas"), + /** + * Thomas, Paul D. Mathematical models for navigation systems. US Naval Oceanographic Office, + * 1965. + */ + ANDOYER("andoyer"), + /** + * <a href="https://link.springer.com/content/pdf/10.1007/s00190-012-0578-z.pdf">Karney, Charles + * FF. "Algorithms for geodesics." Journal of Geodesy 87 (2013): 43-55 </a>, and <a + * href="https://geographiclib.sourceforge.io/">GeographicLib</a>. + */ + KARNEY("karney"); + + private final String value; + + EdgeInterpolationAlgorithm(String value) { + this.value = value; + } + + public String value() { + return value; + } + + public static EdgeInterpolationAlgorithm fromName(String algorithmName) { + try { + return EdgeInterpolationAlgorithm.valueOf(algorithmName.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException( + String.format("Invalid edge interpolation algorithm name: %s", algorithmName), e); Review Comment: Nit: can remove redundant 'name' ########## core/src/main/java/org/apache/iceberg/SchemaParser.java: ########## @@ -277,6 +313,17 @@ private static Types.MapType mapFromJson(JsonNode json) { } } + private static Types.GeometryType geometryFromJson(JsonNode json) { + String crs = JsonUtil.getStringOrNull("crs", json); Review Comment: nit: these (and following) can be replaced by the constants? ########## api/src/main/java/org/apache/iceberg/types/Types.java: ########## @@ -543,6 +561,120 @@ public int hashCode() { } } + public static class GeometryType extends PrimitiveType { + + private final String crs; + + private GeometryType(String crs) { + this.crs = crs; Review Comment: Precondition that crs is not null? ########## api/src/main/java/org/apache/iceberg/types/Types.java: ########## @@ -543,6 +561,120 @@ public int hashCode() { } } + public static class GeometryType extends PrimitiveType { + + private final String crs; + + private GeometryType(String crs) { + this.crs = crs; + } + + public static GeometryType get() { + return of(""); Review Comment: why not , new GeometryType("") ########## api/src/test/java/org/apache/iceberg/types/TestTypes.java: ########## @@ -90,5 +90,42 @@ public void testNestedFieldBuilderIdCheck() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> required("field").ofType(Types.StringType.get()).build()) .withMessage("Id cannot be null"); + + assertThat(Types.fromPrimitiveString("geometry")).isEqualTo(Types.GeometryType.get()); Review Comment: this should be in its own test ########## api/src/main/java/org/apache/iceberg/types/Types.java: ########## @@ -543,6 +561,120 @@ public int hashCode() { } } + public static class GeometryType extends PrimitiveType { + + private final String crs; + + private GeometryType(String crs) { + this.crs = crs; + } + + public static GeometryType get() { + return of(""); + } + + public static GeometryType of(String crs) { + return new GeometryType(crs == null ? "" : crs); + } + + @Override + public TypeID typeId() { + return TypeID.GEOMETRY; + } + + public String crs() { + return crs; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof GeometryType)) { + return false; + } + + GeometryType that = (GeometryType) o; + return crs.equals(that.crs); + } + + @Override + public int hashCode() { + return Objects.hash(GeometryType.class, crs); + } + + @Override + public String toString() { + return String.format("geometry(%s)", crs); + } + } + + public static class GeographyType extends PrimitiveType { + + private final String crs; + private final EdgeInterpolationAlgorithm algorithm; + + private GeographyType(String crs, EdgeInterpolationAlgorithm algorithm) { + this.crs = crs; + this.algorithm = algorithm; + } + + public static GeographyType get() { + return of(""); Review Comment: why not new GeographyType("") ########## core/src/main/java/org/apache/iceberg/util/GeometryUtil.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.iceberg.util; + +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.WKBReader; +import org.locationtech.jts.io.WKBWriter; +import org.locationtech.jts.io.WKTReader; +import org.locationtech.jts.io.WKTWriter; + +public class GeometryUtil { + + private GeometryUtil() {} + + public static byte[] toWKB(Geometry geom) { + WKBWriter wkbWriter = new WKBWriter(getOutputDimension(geom), false); + return wkbWriter.write(geom); + } + + public static String toWKT(Geometry geom) { + WKTWriter wktWriter = new WKTWriter(getOutputDimension(geom)); + return wktWriter.write(geom); + } + + public static Geometry fromWKB(byte[] wkb) { + WKBReader reader = new WKBReader(); + try { + return reader.read(wkb); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to parse WKB", e); + } + } + + public static Geometry fromWKT(String wkt) { + WKTReader reader = new WKTReader(); + try { + return reader.read(wkt); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to parse WKT", e); + } + } + + private static int getOutputDimension(Geometry geom) { + int dimension = 2; Review Comment: nit: make a constant to avoid instantiating it every time? -- 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-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org