use of io.arlas.server.core.exceptions.InvalidParameterException in project ARLAS-server by gisaia.
the class StacRESTService method getDateFilter.
protected String getDateFilter(String datetime, CollectionReference collectionReference) throws ArlasException {
if (StringUtil.isNullOrEmpty(datetime)) {
// no date filter
return null;
}
if (collectionReference.params.timestampPath == null) {
throw new ArlasException("No default timestamp path defined for the collection");
}
String dateField = collectionReference.params.timestampPath;
String format = collectionReference.params.customParams.get(CollectionReference.TIMESTAMP_FORMAT);
Object dateFormatted;
if (datetime.startsWith("/")) {
datetime = ".." + datetime;
}
if (datetime.endsWith("/")) {
datetime = datetime + "..";
}
if (datetime.endsWith(".Z")) {
throw new InvalidParameterException("Datetime value is not RFC 3339 compatible (missing fractional seconds: " + datetime);
}
String[] parts = datetime.split("/");
if (parts.length == 1) {
try {
Long millisecondValue = Long.valueOf(OffsetDateTime.parse(datetime).toInstant().toEpochMilli());
dateFormatted = formatDate(millisecondValue, format);
} catch (DateTimeParseException e) {
throw new InvalidParameterException("Datetime value is not RFC 3339 compatible: " + datetime);
}
return StringUtil.concat(dateField, ":", OperatorEnum.eq.name(), ":", String.valueOf(dateFormatted));
} else if (parts.length == 2) {
if (parts[0].equals("..")) {
return StringUtil.concat(dateField, ":", OperatorEnum.lte.name(), ":", getTimestamp(parts[1]));
} else if (parts[1].equals("..")) {
return StringUtil.concat(dateField, ":", OperatorEnum.gte.name(), ":", getTimestamp(parts[0]));
} else {
if (Long.valueOf(OffsetDateTime.parse(parts[0]).toInstant().toEpochMilli()) > Long.valueOf(OffsetDateTime.parse(parts[1]).toInstant().toEpochMilli())) {
throw new InvalidParameterException("Interval dates cannot be the same: " + datetime);
}
return StringUtil.concat(dateField, ":", OperatorEnum.range.name(), ":[", getTimestamp(parts[0]), "<", getTimestamp(parts[1]), "]");
}
} else {
throw new ArlasException("Invalid datetime format for value " + datetime);
}
}
use of io.arlas.server.core.exceptions.InvalidParameterException in project ARLAS-server by gisaia.
the class ParamsParser method getHitsFetcher.
private static HitsFetcher getHitsFetcher(String fetchHitsString) throws ArlasException {
HitsFetcher hitsFetcher = new HitsFetcher();
if (StringUtil.isNullOrEmpty(fetchHitsString)) {
throw new BadRequestException("fetch_hits should not be null nor empty");
}
Matcher matcher = HITS_FETCHER_PATTERN.matcher(fetchHitsString);
if (!matcher.matches()) {
throw new InvalidParameterException("Invalid fetch_hits syntax. It should respect the following pattern : {size*}(+{field1}, -{field2}, {field3}, ...)");
}
hitsFetcher.size = Optional.ofNullable(ParamsParser.tryParseInteger(matcher.group(1))).orElse(1);
hitsFetcher.include = Arrays.asList(matcher.group(3).split(","));
return hitsFetcher;
}
use of io.arlas.server.core.exceptions.InvalidParameterException 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;
}
use of io.arlas.server.core.exceptions.InvalidParameterException in project ARLAS-server by gisaia.
the class ParamsParser method getValidGeometry.
public static String getValidGeometry(String geo) throws ArlasException {
if (CheckParams.isBboxMatch(geo)) {
CheckParams.checkBbox(geo);
return geo;
} else {
Geometry wkt = getValidWKT(geo);
// For the case of Polygon and MultiPolygon, a check of the coordinates orientation is necessary in order to correctly interpret the "desired" polygon
if (wkt.getGeometryType().equals("Polygon") || wkt.getGeometryType().equals("MultiPolygon")) {
List<Polygon> polygonList = new ArrayList<>();
for (int i = 0; i < wkt.getNumGeometries(); i++) {
Polygon subWkt = (Polygon) wkt.getGeometryN(i);
if (Orientation.isCCW(subWkt.getCoordinates())) {
// By convention the passed queryGeometry must be interpreted as CW.
// If the orientation is CCW, we try to build the WKT that goes the other side of the planet.
// If the topology of the resulted geometry is not valid, an exception is thrown
Polygon tmpGeometry = (Polygon) subWkt.copy();
Envelope tmpEnvelope = tmpGeometry.getEnvelopeInternal();
// east is the minX and west is the maxX
double east = tmpEnvelope.getMinX();
double west = tmpEnvelope.getMaxX();
if (west > east && ((east < -180 && west >= -180) || (west > 180 && east <= 180))) {
// It means west > 180 or east < -180
if (west >= 180) {
GeoUtil.translateLongitudesWithCondition(tmpGeometry, 360, false, 180);
} else if (east <= -180) {
GeoUtil.translateLongitudesWithCondition(tmpGeometry, 360, true, -180);
}
} else {
if (west >= 0) {
GeoUtil.translateLongitudesWithCondition(tmpGeometry, 360, false, east);
} else {
GeoUtil.translateLongitudesWithCondition(tmpGeometry, 360, true, west);
}
}
IsValidOp validOp = new IsValidOp(tmpGeometry);
TopologyValidationError err = validOp.getValidationError();
if (err != null) {
throw new InvalidParameterException("A Polygon of the given WKT is right oriented. Unable to reverse the orientation of the polygon : " + err);
}
polygonList.addAll(GeoUtil.splitPolygon((Polygon) GeoUtil.readWKT(tmpGeometry.toString()))._1());
} else {
polygonList.addAll(GeoUtil.splitPolygon(subWkt)._1());
}
}
if (polygonList.size() == 1) {
return polygonList.get(0).toString();
} else {
return new MultiPolygon(polygonList.toArray(new Polygon[] {}), GEOMETRY_FACTORY).toString();
}
} else {
return geo;
}
}
}
use of io.arlas.server.core.exceptions.InvalidParameterException in project ARLAS-server by gisaia.
the class GeoUtil method checkWKT.
public static void checkWKT(String wktString) throws InvalidParameterException {
Envelope affectedBounds = new Envelope(-360, 360, -180, 180);
WKTReader wkt = new WKTReader(geometryFactory);
Geometry geom = null;
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(INVALID_WKT_RANGE);
}
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(INVALID_WKT + ": " + err.getMessage());
}
}
} catch (ParseException ex) {
throw new InvalidParameterException(INVALID_WKT + ": " + ex.getMessage());
}
}
Aggregations