use of com.vividsolutions.jts.operation.valid.IsValidOp in project spatial-portal by AtlasOfLivingAustralia.
the class AreaWKT method validWKT.
public boolean validWKT(String wkt) {
if (wkt.replaceAll(" ", "").isEmpty()) {
invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID));
return false;
}
try {
WKTReader wktReader = new WKTReader();
com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
// NC 20130319: Ensure that the WKT is valid according to the WKT standards.
IsValidOp op = new IsValidOp(g);
if (!op.isValid()) {
// this will fix some issues
g = g.buffer(0);
op = new IsValidOp(g);
}
if (!op.isValid()) {
invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
LOGGER.warn("WKT is invalid." + op.getValidationError().getMessage());
// TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
} else if (g.isRectangle()) {
// NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
// get the new WKT for the rectangle will possibly need to change the order.
com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
String wkt2 = StringConstants.POLYGON + "((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
if (!wkt.equals(wkt2)) {
LOGGER.debug("NEW WKT for Rectangle: " + wkt);
invalidWKT.setValue(CommonData.lang("error_wkt_rectangle_wrong_order"));
displayGeom.setValue(wkt2);
return false;
}
}
return op.isValid();
} catch (ParseException parseException) {
invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + parseException.getMessage());
return false;
}
}
Aggregations