Skip to content

Commit

Permalink
WKT decoder decodes Compound and Vertical coordinate systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Apr 23, 2016
1 parent d2fe1db commit ee01102
Show file tree
Hide file tree
Showing 14 changed files with 31,401 additions and 3,792 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/geolatte/geom/codec/AbstractWktDecoder.java
Expand Up @@ -70,6 +70,28 @@ protected String decodeText() {
throw new WktDecodeException("Expected text token, received " + currentToken.toString());
}

protected int decodeInt() {
if (currentToken instanceof WktNumberToken) {
double num = ((WktNumberToken) currentToken).getNumber();
nextToken();
try {
return (int)num;
} catch (Exception e) {
throw new WktDecodeException("Expected Integer, received " + currentToken.toString());
}
} else if (currentToken instanceof WktTextToken) {
String text = ((WktTextToken) currentToken).getText();
nextToken();
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
throw new WktDecodeException("Expected Integer, received " + currentToken.toString());
}
}
throw new WktDecodeException("Expected text token, received " + currentToken.toString());
}


/**
* Advances the decoding to the next token.
*/
Expand Down
Expand Up @@ -40,7 +40,14 @@ boolean moreTokens() {
return this.currentPos < wkt.length();
}

// this is just temporarily for testing
WktToken nextToken() {
WktToken token = inner();
// System.out.println("NEXT TOKEN: " + token);
return token;
}

private WktToken inner() {
if (!moreTokens()) {
return variant.end();
}
Expand Down
69 changes: 54 additions & 15 deletions src/main/java/org/geolatte/geom/codec/CrsWktDecoder.java
Expand Up @@ -79,13 +79,16 @@ private CoordinateReferenceSystem<? extends Position> decode() {
return decodeGeographicCrs();
} else if (currentToken == CrsWktVariant.GEOCCS) {
return decodeGeocentricCrs();
} else if (currentToken == CrsWktVariant.COMPD_CS) {
return decodeCompoundCrs();
} else if (currentToken == CrsWktVariant.VERT_CS) {
return decodeVertCS();
}
throw new WktDecodeException("Expected Wkt Token PROJCS, GEOGCS or GEOCCS");
throw new WktDecodeException("Expected Wkt Token PROJCS, GEOGCS, GEOCCS or COMPD_CS. Received " + currentToken);
}

/**
* Implementation to decode Geocentric CRS.
* Currently not used in Postgis and also not implemented here!
e * Currently not used in Postgis and also not implemented here!
*
* @throws UnsupportedConversionException Geocentric CRS is currently not implemented
*/
Expand Down Expand Up @@ -139,12 +142,48 @@ private ProjectedCoordinateReferenceSystem decodeProjectedCrs() {
parameters = decodeOptionalParameters();
unit = decodeUnit(true);
}
CrsId crsId = decodeOptionalAuthority(srid);
CoordinateSystemAxis[] twinAxes = decodeOptionalTwinAxis(unit, ProjectedCoordinateReferenceSystem.class);
CrsId crsId = decodeOptionalAuthority(srid);
matchesCloseList();
return new ProjectedCoordinateReferenceSystem(crsId, crsName, geogcs, projection, parameters,
new CartesianCoordinateSystem2D((StraightLineAxis)twinAxes[0], (StraightLineAxis)twinAxes[1]));
}

private <P extends Position> CompoundCoordinateReferenceSystem<P> decodeCompoundCrs() {
String crsName = decodeName();
matchesElementSeparator();
SingleCoordinateReferenceSystem<?> head = (SingleCoordinateReferenceSystem<?>)decode();
matchesElementSeparator();
SingleCoordinateReferenceSystem<?> tail = (SingleCoordinateReferenceSystem<?>)decode();
CrsId cr = decodeOptionalAuthority(srid);
return new CompoundCoordinateReferenceSystem<P>(cr, crsName, head, tail);
}

private VerticalCoordinateReferenceSystem decodeVertCS() {
String crsName = decodeName();
matchesElementSeparator();
VerticalDatum vdatum = decodeVertDatum();
matchesElementSeparator();
LinearUnit unit =(LinearUnit) decodeUnit(true);
matchesElementSeparator();
VerticalStraightLineAxis axis = (VerticalStraightLineAxis)decodeAxis(unit, VerticalCoordinateReferenceSystem.class);
CrsId id = decodeOptionalAuthority();
return new VerticalCoordinateReferenceSystem(id, crsName, vdatum, axis);
}

private VerticalDatum decodeVertDatum() {
if (currentToken != CrsWktVariant.VERT_DATUM) {
throw new WktDecodeException("Expected VERT_DATUM keyword, found " + currentToken.toString());
}
String name = decodeName();
matchesElementSeparator();
int type = decodeInt();
CrsId authority = decodeOptionalAuthority(srid);
matchesCloseList();
return new VerticalDatum(authority, name, type);
}


private List<CrsParameter> decodeOptionalParameters() {
List<CrsParameter> parameters = new ArrayList<CrsParameter>();
CrsParameter parameter = decodeOptionalParameter();
Expand Down Expand Up @@ -257,6 +296,10 @@ private <T extends CoordinateReferenceSystem> CoordinateSystemAxis decodeAxis(Un
return new StraightLineAxis(name, direction, (LinearUnit)unit);
}

if (VerticalCoordinateReferenceSystem.class.isAssignableFrom(crsClass)) {
return new VerticalStraightLineAxis(name, direction, (LinearUnit) unit);
}

throw new IllegalStateException("Can't create default for CrsRegistry of type " + crsClass.getCanonicalName());

}
Expand Down Expand Up @@ -329,26 +372,22 @@ private Ellipsoid decodeSpheroid() {
return new Ellipsoid(crsId, ellipsoidName, semiMajorAxis, inverseFlattening);
}

private CrsId decodeOptionalAuthority() {
return decodeOptionalAuthority(CrsId.UNDEFINED.getCode());
}

private CrsId decodeOptionalAuthority(int srid) {
matchesElementSeparator();
if (currentToken != CrsWktVariant.AUTHORITY) {
return CrsId.valueOf(srid);
return new CrsId("EPSG", srid);
}

nextToken();
matchesOpenList();
String authority = decodeText();
matchesElementSeparator();
String value = decodeText();
int value = decodeInt();
matchesCloseList();
if (authority.equals("EPSG")) {
try {
return new CrsId("EPSG", Integer.parseInt(value));
} catch (NumberFormatException e) {
throw new WktDecodeException("Expected EPSG integer code, received " + value);
}
}
return CrsId.valueOf(srid);
return new CrsId(authority, value);
}

private String decodeName() {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/geolatte/geom/codec/CrsWktVariant.java
Expand Up @@ -37,6 +37,9 @@ class CrsWktVariant extends WktVariant {
public static final WktKeywordToken PROJECTION = new WktKeywordToken("PROJECTION");
public static final WktKeywordToken GEOGCS = new WktKeywordToken("GEOGCS");
public static final WktKeywordToken GEOCCS = new WktKeywordToken("GEOCCS");
public static final WktKeywordToken VERT_CS = new WktKeywordToken("VERT_CS");
public static final WktKeywordToken VERT_DATUM = new WktKeywordToken("VERT_DATUM");
public static final WktKeywordToken COMPD_CS = new WktKeywordToken("COMPD_CS");
public static final WktKeywordToken DATUM = new WktKeywordToken("DATUM");
public static final WktKeywordToken SPHEROID = new WktKeywordToken("SPHEROID");
public static final WktKeywordToken PRIMEM = new WktKeywordToken("PRIMEM");
Expand Down Expand Up @@ -78,6 +81,9 @@ class CrsWktVariant extends WktVariant {
set.add(PARAMETER);
set.add(UNIT);
set.add(TOWGS84);
set.add(COMPD_CS);
set.add(VERT_CS);
set.add(VERT_DATUM);
KEYWORDS = Collections.unmodifiableSet(set);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/geolatte/geom/codec/WktTextToken.java
Expand Up @@ -44,6 +44,10 @@ public WktTextToken(String text) {
public String getText() {
return text;
}

public String toString() {
return text;
}
}


Expand Down
Expand Up @@ -18,9 +18,13 @@ public class CompoundCoordinateReferenceSystem<P extends Position> extends Coord
private final List<SingleCoordinateReferenceSystem<?>> components;


protected CompoundCoordinateReferenceSystem(String name, SingleCoordinateReferenceSystem<?>... components) {
public CompoundCoordinateReferenceSystem(String name, SingleCoordinateReferenceSystem<?>... components) {
this(components[0].getCrsId(), name, components);
}

public CompoundCoordinateReferenceSystem(CrsId crsId, String name, SingleCoordinateReferenceSystem<?>... components) {
//TODO this is problematic: combineCS() result needs to be cast to make CompoundCRS into a CRS<P>!
super(components[0].getCrsId(), name, (CoordinateSystem<P>) combineCS(components));
super(crsId, name, (CoordinateSystem<P>) combineCS(components));
this.components = Arrays.asList(components);
}

Expand Down Expand Up @@ -53,5 +57,12 @@ public boolean isCompound() {
return true;
}

public SingleCoordinateReferenceSystem<?> headCs() {
return components.get(0);
}

public SingleCoordinateReferenceSystem<?> tailCs() {
return components.get(1);
}

}
Expand Up @@ -17,7 +17,7 @@ public class VerticalCoordinateReferenceSystem extends SingleCoordinateReference
* @param crsId the authority and authority c
* @param name
*/
protected VerticalCoordinateReferenceSystem(CrsId crsId, String name, VerticalDatum datum, VerticalStraightLineAxis axis) {
public VerticalCoordinateReferenceSystem(CrsId crsId, String name, VerticalDatum datum, VerticalStraightLineAxis axis) {
super(crsId, name, new OneDimensionCoordinateSystem<V>(axis, V.class));
this.datum = datum;
}
Expand All @@ -26,5 +26,12 @@ public VerticalDatum getDatum() {
return datum;
}

public VerticalStraightLineAxis getVerticalAxis() {
return (VerticalStraightLineAxis)getCoordinateSystem().getAxis(0);
}

public LinearUnit getUnit() {
return getVerticalAxis().getUnit();
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/geolatte/geom/crs/VerticalDatum.java
Expand Up @@ -17,7 +17,7 @@ public class VerticalDatum extends CrsIdentifiable {
* @param crsId
* @param name
*/
protected VerticalDatum(CrsId crsId, String name, int datumtype) {
public VerticalDatum(CrsId crsId, String name, int datumtype) {
super(crsId, name);
this.datumType = datumtype;
}
Expand Down
Expand Up @@ -20,5 +20,9 @@ public VerticalStraightLineAxis(String axisName, CoordinateSystemAxisDirection c
throw new IllegalArgumentException("Only UP and DOWN directions allowed");
}

@Override
public LinearUnit getUnit() {
return (LinearUnit)super.getUnit();
}
}

0 comments on commit ee01102

Please sign in to comment.