Search in sources :

Example 6 with IsValidOp

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;
    }
}
Also used : IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) ParseException(com.vividsolutions.jts.io.ParseException) WKTReader(com.vividsolutions.jts.io.WKTReader)

Aggregations

ParseException (com.vividsolutions.jts.io.ParseException)6 WKTReader (com.vividsolutions.jts.io.WKTReader)6 IsValidOp (com.vividsolutions.jts.operation.valid.IsValidOp)6 MapLayer (au.org.emii.portal.menu.MapLayer)3 Geometry (com.vividsolutions.jts.geom.Geometry)3 UserDataDTO (au.org.ala.spatial.dto.UserDataDTO)2 MapComposer (au.org.emii.portal.composer.MapComposer)1 com.vividsolutions.jts.geom (com.vividsolutions.jts.geom)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 AffineTransformation (com.vividsolutions.jts.geom.util.AffineTransformation)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1