Search in sources :

Example 1 with AggregationTypeEnum

use of io.arlas.server.core.model.enumerations.AggregationTypeEnum in project ARLAS-server by gisaia.

the class GeoAggregateRESTService method geohashgeoaggregate.

@Timed
@Path("{collection}/_geoaggregate/{geohash}")
@GET
@Produces(UTF8JSON)
@Consumes(UTF8JSON)
@ApiOperation(value = "GeoAggregate on a geohash", produces = UTF8JSON, notes = Documentation.GEOHASH_GEOAGGREGATION_OPERATION, consumes = UTF8JSON, response = FeatureCollection.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful operation", response = FeatureCollection.class, responseContainer = "FeatureCollection"), @ApiResponse(code = 500, message = "Arlas Server Error.", response = Error.class), @ApiResponse(code = 400, message = "Bad request.", response = Error.class), @ApiResponse(code = 501, message = "Not implemented functionality.", response = Error.class) })
public Response geohashgeoaggregate(// --------------------------------------------------------
@ApiParam(name = "collection", value = "collection", required = true) @PathParam(value = "collection") String collection, @ApiParam(name = "geohash", value = "geohash", required = true) @PathParam(value = "geohash") String geohash, // --------------------------------------------------------
@ApiParam(name = "agg", value = Documentation.GEOAGGREGATION_PARAM_AGG) @QueryParam(value = "agg") List<String> agg, // --------------------------------------------------------
@ApiParam(name = "f", value = Documentation.FILTER_PARAM_F, allowMultiple = true) @QueryParam(value = "f") List<String> f, @ApiParam(name = "q", value = Documentation.FILTER_PARAM_Q, allowMultiple = true) @QueryParam(value = "q") List<String> q, @ApiParam(name = "dateformat", value = Documentation.FILTER_DATE_FORMAT) @QueryParam(value = "dateformat") String dateformat, @ApiParam(hidden = true) @HeaderParam(value = "partition-filter") String partitionFilter, @ApiParam(hidden = true) @HeaderParam(value = "Column-Filter") Optional<String> columnFilter, // --------------------------------------------------------
@ApiParam(name = "pretty", value = Documentation.FORM_PRETTY, defaultValue = "false") @QueryParam(value = "pretty") Boolean pretty, @ApiParam(name = "flat", value = Documentation.FORM_FLAT, defaultValue = "false") @QueryParam(value = "flat") Boolean flat, // --------------------------------------------------------
@ApiParam(value = "max-age-cache") @QueryParam(value = "max-age-cache") Integer maxagecache) throws NotFoundException, ArlasException {
    CollectionReference collectionReference = exploreService.getCollectionReferenceService().getCollectionReference(collection);
    if (collectionReference == null) {
        throw new NotFoundException(collection);
    }
    if (geohash.startsWith("#")) {
        geohash = geohash.substring(1);
    }
    if (agg == null || agg.size() == 0) {
        agg = Collections.singletonList("geohash:" + collectionReference.params.centroidPath + ":interval-" + geohash.length());
    }
    List<BoundingBox> bboxes = getBoundingBoxes(geohash, agg, collectionReference);
    List<CompletableFuture<AggregationResponse>> futureList = new ArrayList<>();
    AggregationTypeEnum aggType = null;
    for (BoundingBox b : bboxes) {
        Expression pwithinBbox = new Expression(collectionReference.params.centroidPath, OperatorEnum.within, b.getWest() + "," + b.getSouth() + "," + String.format(Locale.ROOT, "%.8f", b.getEast() - GEOHASH_EPSILON) + "," + String.format(Locale.ROOT, "%.8f", b.getNorth() - GEOHASH_EPSILON));
        MixedRequest request = getGeoaggregateRequest(collectionReference, ParamsParser.getFilter(collectionReference, f, q, dateformat, b, pwithinBbox), partitionFilter, columnFilter, agg);
        aggType = ((AggregationsRequest) request.basicRequest).aggregations.get(0).type;
        futureList.add(CompletableFuture.supplyAsync(() -> {
            try {
                return exploreService.aggregate(request, collectionReference, true, ((AggregationsRequest) request.basicRequest).aggregations, 0, System.nanoTime());
            } catch (ArlasException e) {
                throw new RuntimeException(e);
            }
        }));
    }
    List<AggregationResponse> aggResponses = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
    return cache(Response.ok(toGeoJson(merge(aggResponses), aggType, Boolean.TRUE.equals(flat), Optional.of(geohash))), maxagecache);
}
Also used : ArlasException(io.arlas.server.core.exceptions.ArlasException) CollectionReference(io.arlas.server.core.model.CollectionReference) CompletableFuture(java.util.concurrent.CompletableFuture) AggregationResponse(io.arlas.server.core.model.response.AggregationResponse) AggregationTypeEnum(io.arlas.server.core.model.enumerations.AggregationTypeEnum) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with AggregationTypeEnum

use of io.arlas.server.core.model.enumerations.AggregationTypeEnum in project ARLAS-server by gisaia.

the class GeoAggregateRESTService method getFeatureCollection.

private FeatureCollection getFeatureCollection(MixedRequest request, CollectionReference collectionReference, boolean flat, Optional<String> geohash) throws ArlasException {
    FeatureCollection fc;
    AggregationTypeEnum mainAggregationType = ((AggregationsRequest) request.basicRequest).aggregations.get(0).type;
    AggregationResponse aggregationResponse = exploreService.aggregate(request, collectionReference, true, ((AggregationsRequest) request.basicRequest).aggregations, 0, System.nanoTime());
    fc = toGeoJson(aggregationResponse, mainAggregationType, flat, geohash);
    return fc;
}
Also used : AggregationResponse(io.arlas.server.core.model.response.AggregationResponse) FeatureCollection(org.geojson.FeatureCollection) AggregationTypeEnum(io.arlas.server.core.model.enumerations.AggregationTypeEnum)

Example 3 with AggregationTypeEnum

use of io.arlas.server.core.model.enumerations.AggregationTypeEnum in project ARLAS-server by gisaia.

the class GeoAggregateRESTService method toGeoJson.

private FeatureCollection toGeoJson(AggregationResponse aggregationResponse, AggregationTypeEnum mainAggregationType, boolean flat, Optional<String> tile) {
    FeatureCollection fc = new FeatureCollection();
    List<AggregationResponse> elements = aggregationResponse.elements;
    if (!CollectionUtils.isEmpty(elements)) {
        for (AggregationResponse element : elements) {
            if (!CollectionUtils.isEmpty(element.geometries)) {
                element.geometries.forEach(g -> {
                    Feature feature = new Feature();
                    Map<String, Object> properties = new HashMap<>();
                    properties.put("count", element.count);
                    if (mainAggregationType == AggregationTypeEnum.geohash) {
                        properties.put("geohash", element.keyAsString);
                        tile.ifPresent(s -> properties.put("parent_geohash", s));
                    } else if (mainAggregationType == AggregationTypeEnum.h3) {
                        properties.put("h3", element.keyAsString);
                        tile.ifPresent(s -> properties.put("parent_cell", s));
                    } else if (mainAggregationType == AggregationTypeEnum.geotile) {
                        properties.put("tile", element.keyAsString);
                        tile.ifPresent(s -> properties.put("parent_tile", s));
                    } else {
                        properties.put("key", element.keyAsString);
                    }
                    if (flat) {
                        properties.putAll(exploreService.flat(element, new MapExplorer.ReduceArrayOnKey(ArlasServerConfiguration.FLATTEN_CHAR), s -> (!"elements".equals(s))));
                    } else {
                        properties.put("elements", element.elements);
                        properties.put("metrics", element.metrics);
                        if (element.hits != null) {
                            properties.put("hits", element.hits);
                        }
                    }
                    feature.setProperties(properties);
                    feature.setProperty(FEATURE_TYPE_KEY, FEATURE_TYPE_VALUE);
                    feature.setProperty(GEOMETRY_REFERENCE, g.reference);
                    String aggregationGeometryType = g.isRaw ? AggregationGeometryEnum.RAW.value() : AggregationGeometryEnum.AGGREGATED.value();
                    feature.setProperty(GEOMETRY_TYPE, aggregationGeometryType);
                    if (g.isRaw) {
                        feature.setProperty(GEOMETRY_SORT, g.sort);
                    }
                    GeoJsonObject geometry = g.geometry;
                    feature.setGeometry(geometry);
                    fc.add(feature);
                });
            }
        }
    }
    return fc;
}
Also used : java.util(java.util) AggregationTypeEnum(io.arlas.server.core.model.enumerations.AggregationTypeEnum) ApiParam(io.swagger.annotations.ApiParam) CompletableFuture(java.util.concurrent.CompletableFuture) ApiResponses(io.swagger.annotations.ApiResponses) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Feature(org.geojson.Feature) ApiOperation(io.swagger.annotations.ApiOperation) ArlasException(io.arlas.server.core.exceptions.ArlasException) ExploreService(io.arlas.server.core.services.ExploreService) FeatureCollection(org.geojson.FeatureCollection) InvalidParameterException(io.arlas.server.core.exceptions.InvalidParameterException) io.arlas.server.core.utils(io.arlas.server.core.utils) ArlasServerConfiguration(io.arlas.server.core.app.ArlasServerConfiguration) CollectionReference(io.arlas.server.core.model.CollectionReference) io.arlas.server.core.model.request(io.arlas.server.core.model.request) Documentation(io.arlas.server.core.app.Documentation) GeoJsonObject(org.geojson.GeoJsonObject) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Timed(com.codahale.metrics.annotation.Timed) ExploreRESTServices(io.arlas.server.rest.explore.ExploreRESTServices) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) AggregationResponse(io.arlas.server.core.model.response.AggregationResponse) ApiResponse(io.swagger.annotations.ApiResponse) AggregationGeometryEnum(io.arlas.server.core.model.enumerations.AggregationGeometryEnum) OperatorEnum(io.arlas.server.core.model.enumerations.OperatorEnum) Error(io.arlas.server.core.model.response.Error) AggregationResponse(io.arlas.server.core.model.response.AggregationResponse) FeatureCollection(org.geojson.FeatureCollection) GeoJsonObject(org.geojson.GeoJsonObject) GeoJsonObject(org.geojson.GeoJsonObject) Feature(org.geojson.Feature)

Example 4 with AggregationTypeEnum

use of io.arlas.server.core.model.enumerations.AggregationTypeEnum in project ARLAS-server by gisaia.

the class GeoAggregateRESTService method geotilegeoaggregate.

@Timed
@Path("{collection}/_geoaggregate/{z}/{x}/{y}")
@GET
@Produces(UTF8JSON)
@Consumes(UTF8JSON)
@ApiOperation(value = "GeoAggregate on a geotile", produces = UTF8JSON, notes = Documentation.GEOTILE_GEOAGGREGATION_OPERATION, consumes = UTF8JSON, response = FeatureCollection.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful operation", response = FeatureCollection.class, responseContainer = "FeatureCollection"), @ApiResponse(code = 500, message = "Arlas Server Error.", response = Error.class), @ApiResponse(code = 400, message = "Bad request.", response = Error.class), @ApiResponse(code = 501, message = "Not implemented functionality.", response = Error.class) })
public Response geotilegeoaggregate(// --------------------------------------------------------
@ApiParam(name = "collection", value = "collection", required = true) @PathParam(value = "collection") String collection, @ApiParam(name = "z", value = "z", required = true) @PathParam(value = "z") Integer z, @ApiParam(name = "x", value = "x", required = true) @PathParam(value = "x") Integer x, @ApiParam(name = "y", value = "y", required = true) @PathParam(value = "y") Integer y, // --------------------------------------------------------
@ApiParam(name = "agg", value = Documentation.GEOAGGREGATION_PARAM_AGG) @QueryParam(value = "agg") List<String> agg, // --------------------------------------------------------
@ApiParam(name = "f", value = Documentation.FILTER_PARAM_F, allowMultiple = true) @QueryParam(value = "f") List<String> f, @ApiParam(name = "q", value = Documentation.FILTER_PARAM_Q, allowMultiple = true) @QueryParam(value = "q") List<String> q, @ApiParam(name = "dateformat", value = Documentation.FILTER_DATE_FORMAT) @QueryParam(value = "dateformat") String dateformat, @ApiParam(hidden = true) @HeaderParam(value = "partition-filter") String partitionFilter, @ApiParam(hidden = true) @HeaderParam(value = "Column-Filter") Optional<String> columnFilter, // --------------------------------------------------------
@ApiParam(name = "pretty", value = Documentation.FORM_PRETTY, defaultValue = "false") @QueryParam(value = "pretty") Boolean pretty, @ApiParam(name = "flat", value = Documentation.FORM_FLAT, defaultValue = "false") @QueryParam(value = "flat") Boolean flat, // --------------------------------------------------------
@ApiParam(value = "max-age-cache") @QueryParam(value = "max-age-cache") Integer maxagecache) throws NotFoundException, ArlasException {
    CollectionReference collectionReference = exploreService.getCollectionReferenceService().getCollectionReference(collection);
    if (collectionReference == null) {
        throw new NotFoundException(collection);
    }
    if (agg == null || agg.size() == 0) {
        agg = Collections.singletonList("geotile:" + collectionReference.params.centroidPath + ":interval-" + (z + 3));
    }
    List<BoundingBox> bboxes = getBoundingBoxes(z, x, y, agg, collectionReference);
    List<CompletableFuture<AggregationResponse>> futureList = new ArrayList<>();
    AggregationTypeEnum aggType = null;
    for (BoundingBox b : bboxes) {
        Expression pwithinBbox = new Expression(collectionReference.params.centroidPath, OperatorEnum.within, b.getWest() + "," + b.getSouth() + "," + String.format(Locale.ROOT, "%.8f", b.getEast() - GEOHASH_EPSILON) + "," + String.format(Locale.ROOT, "%.8f", b.getNorth() - GEOHASH_EPSILON));
        MixedRequest request = getGeoaggregateRequest(collectionReference, ParamsParser.getFilter(collectionReference, f, q, dateformat, b, pwithinBbox), partitionFilter, columnFilter, agg);
        aggType = ((AggregationsRequest) request.basicRequest).aggregations.get(0).type;
        futureList.add(CompletableFuture.supplyAsync(() -> {
            try {
                return exploreService.aggregate(request, collectionReference, true, ((AggregationsRequest) request.basicRequest).aggregations, 0, System.nanoTime());
            } catch (ArlasException e) {
                throw new RuntimeException(e);
            }
        }));
    }
    List<AggregationResponse> aggResponses = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
    return cache(Response.ok(toGeoJson(merge(aggResponses), aggType, Boolean.TRUE.equals(flat), Optional.of(z + "/" + x + "/" + y))), maxagecache);
}
Also used : ArlasException(io.arlas.server.core.exceptions.ArlasException) CollectionReference(io.arlas.server.core.model.CollectionReference) CompletableFuture(java.util.concurrent.CompletableFuture) AggregationResponse(io.arlas.server.core.model.response.AggregationResponse) AggregationTypeEnum(io.arlas.server.core.model.enumerations.AggregationTypeEnum) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

AggregationTypeEnum (io.arlas.server.core.model.enumerations.AggregationTypeEnum)4 AggregationResponse (io.arlas.server.core.model.response.AggregationResponse)4 Timed (com.codahale.metrics.annotation.Timed)3 ArlasException (io.arlas.server.core.exceptions.ArlasException)3 CollectionReference (io.arlas.server.core.model.CollectionReference)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 FeatureCollection (org.geojson.FeatureCollection)2 ArlasServerConfiguration (io.arlas.server.core.app.ArlasServerConfiguration)1 Documentation (io.arlas.server.core.app.Documentation)1 InvalidParameterException (io.arlas.server.core.exceptions.InvalidParameterException)1 AggregationGeometryEnum (io.arlas.server.core.model.enumerations.AggregationGeometryEnum)1 OperatorEnum (io.arlas.server.core.model.enumerations.OperatorEnum)1 io.arlas.server.core.model.request (io.arlas.server.core.model.request)1 Error (io.arlas.server.core.model.response.Error)1 ExploreService (io.arlas.server.core.services.ExploreService)1 io.arlas.server.core.utils (io.arlas.server.core.utils)1 ExploreRESTServices (io.arlas.server.rest.explore.ExploreRESTServices)1 ApiParam (io.swagger.annotations.ApiParam)1