use of org.locationtech.jts.operation.valid.RepeatedPointTester in project ARLAS-server by gisaia.
the class ParamsParser method getValidWKT.
public static Geometry getValidWKT(String wktString) throws InvalidParameterException {
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
Envelope affectedBounds = new Envelope(-360, 360, -180, 180);
WKTReader wkt = new WKTReader(geometryFactory);
Geometry geom;
try {
geom = wkt.read(wktString);
List<Coordinate> filteredCoord = Arrays.stream(geom.getCoordinates()).filter(coordinate -> affectedBounds.contains(coordinate)).collect(Collectors.toList());
if (filteredCoord.size() != geom.getCoordinates().length) {
throw new InvalidParameterException("Coordinates must be contained in the Envelope -360, 360, -180, 180");
}
RepeatedPointTester tester = new RepeatedPointTester();
for (int i = 0; i < geom.getNumGeometries(); i++) {
IsValidOp validOp = new IsValidOp(geom.getGeometryN(i));
TopologyValidationError err = validOp.getValidationError();
if (err != null) {
throw new InvalidParameterException(GeoUtil.INVALID_WKT + ": " + err.getMessage());
}
if (tester.hasRepeatedPoint(geom.getGeometryN(i))) {
throw new InvalidParameterException(GeoUtil.INVALID_WKT + ": duplicate consecutive points detected in " + geom.getGeometryN(i).toText());
}
}
} catch (org.locationtech.jts.io.ParseException ex) {
throw new InvalidParameterException("Invalid WKT: " + ex.getMessage());
}
return geom;
}
Aggregations