Search in sources :

Example 1 with InvalidParameterException

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);
    }
}
Also used : ArlasException(io.arlas.server.core.exceptions.ArlasException) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) DateTimeParseException(java.time.format.DateTimeParseException) GeoJsonObject(org.geojson.GeoJsonObject)

Example 2 with InvalidParameterException

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;
}
Also used : InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) Matcher(java.util.regex.Matcher) BadRequestException(io.arlas.server.core.exceptions.BadRequestException)

Example 3 with InvalidParameterException

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;
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) WKTReader(org.locationtech.jts.io.WKTReader) StringUtils(org.apache.commons.lang3.StringUtils) GEO_AGGREGATION_TYPE_ENUMS(io.arlas.server.core.utils.CheckParams.GEO_AGGREGATION_TYPE_ENUMS) RepeatedPointTester(org.locationtech.jts.operation.valid.RepeatedPointTester) ArlasException(io.arlas.server.core.exceptions.ArlasException) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) IntParam(io.dropwizard.jersey.params.IntParam) GeoPoint(org.elasticsearch.common.geo.GeoPoint) CollectionReferenceManager(io.arlas.server.core.managers.CollectionReferenceManager) org.locationtech.jts.geom(org.locationtech.jts.geom) BadRequestException(io.arlas.server.core.exceptions.BadRequestException) DateTimeFormat(org.joda.time.format.DateTimeFormat) CollectionReference(io.arlas.server.core.model.CollectionReference) io.arlas.server.core.model.request(io.arlas.server.core.model.request) FieldType(io.arlas.server.core.model.response.FieldType) IsValidOp(org.locationtech.jts.operation.valid.IsValidOp) TopologyValidationError(org.locationtech.jts.operation.valid.TopologyValidationError) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DateTimeParseException(java.time.format.DateTimeParseException) Orientation(org.locationtech.jts.algorithm.Orientation) Pattern(java.util.regex.Pattern) FluidSearchService(io.arlas.server.core.services.FluidSearchService) io.arlas.server.core.model.enumerations(io.arlas.server.core.model.enumerations) WKTReader(org.locationtech.jts.io.WKTReader) GeoPoint(org.elasticsearch.common.geo.GeoPoint) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) IsValidOp(org.locationtech.jts.operation.valid.IsValidOp) TopologyValidationError(org.locationtech.jts.operation.valid.TopologyValidationError) RepeatedPointTester(org.locationtech.jts.operation.valid.RepeatedPointTester)

Example 4 with InvalidParameterException

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;
        }
    }
}
Also used : InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) IsValidOp(org.locationtech.jts.operation.valid.IsValidOp) TopologyValidationError(org.locationtech.jts.operation.valid.TopologyValidationError) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 5 with InvalidParameterException

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());
    }
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) IsValidOp(org.locationtech.jts.operation.valid.IsValidOp) Arrays(java.util.Arrays) WKTReader(org.locationtech.jts.io.WKTReader) TopologyValidationError(org.locationtech.jts.operation.valid.TopologyValidationError) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) GeoJsonObject(org.geojson.GeoJsonObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader) List(java.util.List) ArlasException(io.arlas.server.core.exceptions.ArlasException) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) ParseException(org.locationtech.jts.io.ParseException) Orientation(org.locationtech.jts.algorithm.Orientation) org.locationtech.jts.geom(org.locationtech.jts.geom) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) IsValidOp(org.locationtech.jts.operation.valid.IsValidOp) TopologyValidationError(org.locationtech.jts.operation.valid.TopologyValidationError) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader)

Aggregations

InvalidParameterException (io.arlas.server.core.exceptions.InvalidParameterException)12 ArlasException (io.arlas.server.core.exceptions.ArlasException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 DateTimeParseException (java.time.format.DateTimeParseException)4 Matcher (java.util.regex.Matcher)4 WKTReader (org.locationtech.jts.io.WKTReader)4 IsValidOp (org.locationtech.jts.operation.valid.IsValidOp)4 TopologyValidationError (org.locationtech.jts.operation.valid.TopologyValidationError)4 BadRequestException (io.arlas.server.core.exceptions.BadRequestException)3 FieldType (io.arlas.server.core.model.response.FieldType)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)3 Orientation (org.locationtech.jts.algorithm.Orientation)3 org.locationtech.jts.geom (org.locationtech.jts.geom)3 CollectionReferenceManager (io.arlas.server.core.managers.CollectionReferenceManager)2 CollectionReference (io.arlas.server.core.model.CollectionReference)2 io.arlas.server.core.model.enumerations (io.arlas.server.core.model.enumerations)2 io.arlas.server.core.model.request (io.arlas.server.core.model.request)2