From f9e7dcde3f30f99adca4fcfb916d15fe454304d4 Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Sat, 6 Mar 2021 14:55:01 +0100 Subject: [PATCH] Minor API improvements --- .../java/org/geolatte/geom/LinearRing.java | 2 +- .../java/org/geolatte/geom/codec/Wkb.java | 40 +++++++++++++++++++ .../java/org/geolatte/geom/codec/Wkt.java | 37 ++++++++++++++++- .../geom/crs/CoordinateReferenceSystem.java | 7 ++++ .../org/geolatte/test/GeometryApiTest.java | 32 ++++++++++----- 5 files changed, 105 insertions(+), 13 deletions(-) diff --git a/geom/src/main/java/org/geolatte/geom/LinearRing.java b/geom/src/main/java/org/geolatte/geom/LinearRing.java index b0967266..892ee910 100644 --- a/geom/src/main/java/org/geolatte/geom/LinearRing.java +++ b/geom/src/main/java/org/geolatte/geom/LinearRing.java @@ -51,7 +51,7 @@ public GeometryType getGeometryType() { } private void checkIsClosed(){ - if (isEmpty() || (isClosed() && getNumPositions() > 3)) return; + if (isEmpty() || (isClosed() && getNumPositions() >= 3)) return; throw new IllegalArgumentException("Cannot create a LinearRing. PointSequence is not closed or contains < 4 points."); } diff --git a/geom/src/main/java/org/geolatte/geom/codec/Wkb.java b/geom/src/main/java/org/geolatte/geom/codec/Wkb.java index cb2a3d58..05c9cdef 100644 --- a/geom/src/main/java/org/geolatte/geom/codec/Wkb.java +++ b/geom/src/main/java/org/geolatte/geom/codec/Wkb.java @@ -102,6 +102,33 @@ public static

ByteBuffer toWkb(Geometry

geometry) { return toWkb(geometry, ByteOrder.NDR); } + /** + * Encodes a Geometry into a WKB representation using the specified byte-order. + *

This methods uses the default WKB dialect (Postgis v1.5 EWKB ).

+ * + * @param geometry The Geometry to be encoded as WKB. + * @param byteOrder The WKB byte order, either {@link ByteOrder#XDR XDR} or {@link ByteOrder#NDR NDR} + * @param dialect the WKB dialect to use + * @return A buffer of bytes that contains the WKB-encoded Geometry. + */ + public static ByteBuffer toWkb(Geometry geometry, ByteOrder byteOrder, Dialect dialect) { + WkbEncoder encoder = newEncoder(dialect); + return encoder.encode(geometry, byteOrder); + } + + /** + * Encodes a Geometry into a WKB representation using the NDR (little-endian) byte-order. + * + *

This methods uses the default WKB dialect (Postgis v1.5 EWKB ).

+ * + * @param geometry The Geometry to be encoded as WKB. + * @param dialect the WKB dialect to use + * @return A buffer of bytes that contains the WKB-encoded Geometry. + */ + public static

ByteBuffer toWkb(Geometry

geometry, Dialect dialect) { + return toWkb(geometry, ByteOrder.NDR, dialect); + } + /** * Encodes a Geometry into a WKB representation using the specified byte-order. *

This methods uses the default WKB dialect (Postgis v1.5 EWKB ).

@@ -127,6 +154,19 @@ public static Geometry fromWkb(ByteBuffer byteBuffer) { return decoder.decode(byteBuffer); } + /** + * Decodes a WKB representation in a ByteBuffer to a Geometry. + *

This methods uses the default WKB dialect (Postgis v1.5 EWKB ).

+ * + * @param byteBuffer A buffer of bytes that contains a WKB-encoded Geometry. + * @param dialect the WKB dialect to use + * @return The Geometry that is encoded in the WKB. + */ + public static Geometry fromWkb(ByteBuffer byteBuffer, Dialect dialect) { + WkbDecoder decoder = newDecoder(dialect); + return decoder.decode(byteBuffer); + } + /** * Creates a WkbDecoder for the specified WKB Dialect. * diff --git a/geom/src/main/java/org/geolatte/geom/codec/Wkt.java b/geom/src/main/java/org/geolatte/geom/codec/Wkt.java index 745e03ca..9dc418fc 100644 --- a/geom/src/main/java/org/geolatte/geom/codec/Wkt.java +++ b/geom/src/main/java/org/geolatte/geom/codec/Wkt.java @@ -84,7 +84,8 @@ public enum Dialect { *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

* * @param wkt the WKT string to decode - * @return The decoded Geometry + * @param crs the Coordinate Reference System for the result + * @return The decoded Geometry in the specified reference system */ public static

Geometry

fromWkt(String wkt, CoordinateReferenceSystem

crs) { WktDecoder decoder = newDecoder(); @@ -96,6 +97,26 @@ public static Geometry fromWkt(String wkt) { return decoder.decode(wkt); } + /** + * Decodes the specified WKT String to a Geometry. + *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

+ * + * @param wkt the WKT string to decode* + * @param crs the Coordinate Reference System for the result + * @param dialect thw WKT Dialect of the WKT String + * @return The decoded Geometry in the specified reference system + * @return The decoded Geometry + */ + public static

Geometry

fromWkt(String wkt, CoordinateReferenceSystem

crs, Dialect dialect) { + WktDecoder decoder = newDecoder(dialect); + return decoder.decode(wkt, crs); + } + + public static Geometry fromWkt(String wkt, Dialect dialect) { + WktDecoder decoder = newDecoder(dialect); + return decoder.decode(wkt); + } + /** * Encodes a Geometry to a WKT representation. *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

@@ -108,6 +129,20 @@ public static String toWkt(Geometry geometry) { return encoder.encode(geometry); } + /** + * Encodes a Geometry to a WKT representation according to + * a specific dialect. + * + *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

+ * + * @param geometry the Geometry to encode + * @return the WKT representation of the given geometry + */ + public static String toWkt(Geometry geometry, Dialect dialect) { + WktEncoder encoder = newEncoder(dialect); + return encoder.encode(geometry); + } + /** * Creates a WktDecoder for the specified WKT Dialect. * diff --git a/geom/src/main/java/org/geolatte/geom/crs/CoordinateReferenceSystem.java b/geom/src/main/java/org/geolatte/geom/crs/CoordinateReferenceSystem.java index 7f183e37..64c61794 100644 --- a/geom/src/main/java/org/geolatte/geom/crs/CoordinateReferenceSystem.java +++ b/geom/src/main/java/org/geolatte/geom/crs/CoordinateReferenceSystem.java @@ -106,6 +106,13 @@ public boolean isCompound() { return false; } + public CoordinateReferenceSystem addVerticalSystem(LinearUnit unit, Class clazz){ + return CoordinateReferenceSystems.addVerticalSystem(this, clazz, unit); + } + + public CoordinateReferenceSystem addLinearSystem(LinearUnit unit, Class clazz){ + return CoordinateReferenceSystems.addLinearSystem(this, clazz, unit); + } @Override public boolean equals(Object o) { diff --git a/geom/src/test/java/org/geolatte/test/GeometryApiTest.java b/geom/src/test/java/org/geolatte/test/GeometryApiTest.java index ec6058af..dd8b4961 100644 --- a/geom/src/test/java/org/geolatte/test/GeometryApiTest.java +++ b/geom/src/test/java/org/geolatte/test/GeometryApiTest.java @@ -1,14 +1,15 @@ package org.geolatte.test; -import org.geolatte.geom.C3DM; +import org.geolatte.geom.*; import org.geolatte.geom.crs.CoordinateReferenceSystem; +import org.geolatte.geom.crs.LinearUnit; import org.junit.Test; -import java.util.Arrays; +import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84; +import static org.geolatte.geom.builder.DSL.*; + +import org.geolatte.geom.codec.Wkt; -import static org.geolatte.geom.crs.CoordinateReferenceSystems.mkCoordinateReferenceSystem; -import static org.geolatte.geom.crs.CoordinateSystemAxisDirection.*; -import static org.geolatte.geom.crs.Unit.METER; import static org.junit.Assert.assertEquals; /** @@ -20,13 +21,22 @@ public class GeometryApiTest { + + @Test - public void createExpandedCrsFromEpsgCode(){ - CoordinateReferenceSystem crs = mkCoordinateReferenceSystem(31370, METER, METER); - assertEquals(C3DM.class, crs.getPositionClass()); - assertEquals(4, crs.getCoordinateDimension()); - assertEquals( Arrays.asList(EAST, NORTH, UP, OTHER), crs.getCoordinateSystem().getAxisDirections()); - } + public void readme_test(){ + Point pnt = point(WGS84, g(4.33,53.21)); + LineString lstr = linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19)); + Polygon pgn = polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))); + CoordinateReferenceSystem wgs84E = WGS84.addVerticalSystem(LinearUnit.METER, G3D.class); + Point pntWithElevation = point(wgs84E, g(4.33, 53.21, 350)); + String wkt = Wkt.toWkt(pnt); + assertEquals("SRID=4326;POINT(4.33 53.21)", wkt); + String sfaWkt = Wkt.toWkt(pntWithElevation, Wkt.Dialect.SFA_1_2_1); + assertEquals("POINT Z (4.33 53.21 350)", sfaWkt); + + + } }