Skip to content

Commit

Permalink
Minor API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Mar 6, 2021
1 parent cfdac43 commit f9e7dcd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
2 changes: 1 addition & 1 deletion geom/src/main/java/org/geolatte/geom/LinearRing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
40 changes: 40 additions & 0 deletions geom/src/main/java/org/geolatte/geom/codec/Wkb.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ public static <P extends Position> ByteBuffer toWkb(Geometry<P> geometry) {
return toWkb(geometry, ByteOrder.NDR);
}

/**
* Encodes a <code>Geometry</code> into a WKB representation using the specified byte-order.
* <p>This methods uses the default WKB dialect (Postgis v1.5 EWKB ).</p>
*
* @param geometry The <code>Geometry</code> 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 <code>Geometry</code>.
*/
public static ByteBuffer toWkb(Geometry<?> geometry, ByteOrder byteOrder, Dialect dialect) {
WkbEncoder encoder = newEncoder(dialect);
return encoder.encode(geometry, byteOrder);
}

/**
* Encodes a <code>Geometry</code> into a WKB representation using the NDR (little-endian) byte-order.
*
* <p>This methods uses the default WKB dialect (Postgis v1.5 EWKB ).</p>
*
* @param geometry The <code>Geometry</code> to be encoded as WKB.
* @param dialect the WKB dialect to use
* @return A buffer of bytes that contains the WKB-encoded <code>Geometry</code>.
*/
public static <P extends Position> ByteBuffer toWkb(Geometry<P> geometry, Dialect dialect) {
return toWkb(geometry, ByteOrder.NDR, dialect);
}

/**
* Encodes a <code>Geometry</code> into a WKB representation using the specified byte-order.
* <p>This methods uses the default WKB dialect (Postgis v1.5 EWKB ).</p>
Expand All @@ -127,6 +154,19 @@ public static Geometry<?> fromWkb(ByteBuffer byteBuffer) {
return decoder.decode(byteBuffer);
}

/**
* Decodes a WKB representation in a <code>ByteBuffer</code> to a <code>Geometry</code>.
* <p>This methods uses the default WKB dialect (Postgis v1.5 EWKB ).</p>
*
* @param byteBuffer A buffer of bytes that contains a WKB-encoded <code>Geometry</code>.
* @param dialect the WKB dialect to use
* @return The <code>Geometry</code> that is encoded in the WKB.
*/
public static Geometry<?> fromWkb(ByteBuffer byteBuffer, Dialect dialect) {
WkbDecoder decoder = newDecoder(dialect);
return decoder.decode(byteBuffer);
}

/**
* Creates a <code>WkbDecoder</code> for the specified WKB <code>Dialect</code>.
*
Expand Down
37 changes: 36 additions & 1 deletion geom/src/main/java/org/geolatte/geom/codec/Wkt.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public enum Dialect {
* <p>This method uses the default WKT dialect (Postgis v1.5 EWKT)</p>
*
* @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 <P extends Position> Geometry<P> fromWkt(String wkt, CoordinateReferenceSystem<P> crs) {
WktDecoder decoder = newDecoder();
Expand All @@ -96,6 +97,26 @@ public static Geometry<?> fromWkt(String wkt) {
return decoder.decode(wkt);
}

/**
* Decodes the specified WKT String to a <code>Geometry</code>.
* <p>This method uses the default WKT dialect (Postgis v1.5 EWKT)</p>
*
* @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 <P extends Position> Geometry<P> fromWkt(String wkt, CoordinateReferenceSystem<P> 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 <code>Geometry</code> to a WKT representation.
* <p>This method uses the default WKT dialect (Postgis v1.5 EWKT)</p>
Expand All @@ -108,6 +129,20 @@ public static String toWkt(Geometry<?> geometry) {
return encoder.encode(geometry);
}

/**
* Encodes a <code>Geometry</code> to a WKT representation according to
* a specific dialect.
*
* <p>This method uses the default WKT dialect (Postgis v1.5 EWKT)</p>
*
* @param geometry the <code>Geometry</code> 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 <code>WktDecoder</code> for the specified WKT <code>Dialect</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public boolean isCompound() {
return false;
}

public <Q extends P> CoordinateReferenceSystem<Q> addVerticalSystem(LinearUnit unit, Class<Q> clazz){
return CoordinateReferenceSystems.addVerticalSystem(this, clazz, unit);
}

public <Q extends P> CoordinateReferenceSystem<Q> addLinearSystem(LinearUnit unit, Class<Q> clazz){
return CoordinateReferenceSystems.addLinearSystem(this, clazz, unit);
}

@Override
public boolean equals(Object o) {
Expand Down
32 changes: 21 additions & 11 deletions geom/src/test/java/org/geolatte/test/GeometryApiTest.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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<G2D> pnt = point(WGS84, g(4.33,53.21));
LineString<G2D> lstr = linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19));
Polygon<G2D> pgn = polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21)));

CoordinateReferenceSystem<G3D> wgs84E = WGS84.addVerticalSystem(LinearUnit.METER, G3D.class);
Point<G3D> 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);


}

}

0 comments on commit f9e7dcd

Please sign in to comment.