Search in sources :

Example 1 with TileProvider

use of de.ii.ogcapi.tiles.domain.TileProvider in project ldproxy by interactive-instruments.

the class AbstractEndpointTileMultiCollection method getTile.

protected Response getTile(OgcApi api, ApiRequestContext requestContext, UriInfo uriInfo, String definitionPath, String tileMatrixSetId, String tileMatrix, String tileRow, String tileCol, TileProvider tileProvider) throws CrsTransformationException, IOException, NotFoundException {
    OgcApiDataV2 apiData = api.getData();
    Map<String, String> queryParams = toFlatMap(uriInfo.getQueryParameters());
    TilesConfiguration tilesConfiguration = apiData.getExtension(TilesConfiguration.class).orElseThrow();
    checkPathParameter(extensionRegistry, apiData, definitionPath, "tileMatrixSetId", tileMatrixSetId);
    checkPathParameter(extensionRegistry, apiData, definitionPath, "tileMatrix", tileMatrix);
    checkPathParameter(extensionRegistry, apiData, definitionPath, "tileRow", tileRow);
    checkPathParameter(extensionRegistry, apiData, definitionPath, "tileCol", tileCol);
    final List<OgcApiQueryParameter> allowedParameters = getQueryParameters(extensionRegistry, apiData, definitionPath);
    int row;
    int col;
    int level;
    try {
        level = Integer.parseInt(tileMatrix);
        row = Integer.parseInt(tileRow);
        col = Integer.parseInt(tileCol);
    } catch (NumberFormatException e) {
        throw new ServerErrorException("Could not convert tile coordinates that have been validated to integers", 500);
    }
    MinMax zoomLevels = tilesConfiguration.getZoomLevelsDerived().get(tileMatrixSetId);
    if (zoomLevels.getMax() < level || zoomLevels.getMin() > level)
        throw new NotFoundException("The requested tile is outside the zoom levels for this tile set.");
    TileMatrixSet tileMatrixSet = tileMatrixSetRepository.get(tileMatrixSetId).orElseThrow(() -> new NotFoundException("Unknown tile matrix set: " + tileMatrixSetId));
    TileMatrixSetLimits tileLimits = limitsGenerator.getTileMatrixSetLimits(api, tileMatrixSet, zoomLevels).stream().filter(limits -> limits.getTileMatrix().equals(tileMatrix)).findAny().orElse(null);
    if (tileLimits != null) {
        if (tileLimits.getMaxTileCol() < col || tileLimits.getMinTileCol() > col || tileLimits.getMaxTileRow() < row || tileLimits.getMinTileRow() > row)
            // return 404, if outside the range
            throw new NotFoundException("The requested tile is outside of the limits for this zoom level and tile set.");
    }
    String path = definitionPath.replace("{tileMatrixSetId}", tileMatrixSetId).replace("{tileMatrix}", tileMatrix).replace("{tileRow}", tileRow).replace("{tileCol}", tileCol);
    TileFormatExtension outputFormat = requestContext.getApi().getOutputFormat(TileFormatExtension.class, requestContext.getMediaType(), path, Optional.empty()).orElseThrow(() -> new NotAcceptableException(MessageFormat.format("The requested media type ''{0}'' is not supported for this resource.", requestContext.getMediaType())));
    Optional<FeatureProvider2> featureProvider = providers.getFeatureProvider(apiData);
    List<String> collections = queryParams.containsKey("collections") ? Splitter.on(",").splitToList(queryParams.get("collections")) : apiData.getCollections().values().stream().filter(collection -> apiData.isCollectionEnabled(collection.getId())).filter(collection -> {
        Optional<TilesConfiguration> layerConfiguration = collection.getExtension(TilesConfiguration.class);
        if (layerConfiguration.isEmpty() || !layerConfiguration.get().isEnabled() || !layerConfiguration.get().isMultiCollectionEnabled())
            return false;
        MinMax levels = layerConfiguration.get().getZoomLevelsDerived().get(tileMatrixSetId);
        return !Objects.nonNull(levels) || (levels.getMax() >= level && levels.getMin() <= level);
    }).map(FeatureTypeConfiguration::getId).collect(Collectors.toList());
    // check, if the cache can be used (no query parameters except f)
    boolean useCache = tileProvider.tilesMayBeCached() && tilesConfiguration.getCache() != TilesConfiguration.TileCacheType.NONE && (queryParams.isEmpty() || (queryParams.size() == 1 && queryParams.containsKey("f")));
    // don't store the tile in the cache if it is outside the range
    MinMax cacheMinMax = tilesConfiguration.getZoomLevelsDerived().get(tileMatrixSetId);
    useCache = useCache && (Objects.isNull(cacheMinMax) || (level <= cacheMinMax.getMax() && level >= cacheMinMax.getMin()));
    Tile tile = new ImmutableTile.Builder().tileMatrixSet(tileMatrixSet).tileLevel(level).tileRow(row).tileCol(col).api(api).apiData(apiData).outputFormat(outputFormat).featureProvider(featureProvider).collectionIds(collections).temporary(!useCache).isDatasetTile(true).build();
    QueryInput queryInput = null;
    // if cache can be used and the tile is cached for the requested format, return the cache
    if (useCache) {
        // get the tile from the cache and return it
        Optional<InputStream> tileStream = Optional.empty();
        try {
            tileStream = cache.getTile(tile);
        } catch (Exception e) {
            LOGGER.warn("Failed to retrieve multi-collection tile {}/{}/{}/{} from the cache. Reason: {}", tile.getTileMatrixSet().getId(), tile.getTileLevel(), tile.getTileRow(), tile.getTileCol(), e.getMessage());
        }
        if (tileStream.isPresent()) {
            queryInput = new Builder().from(getGenericQueryInput(apiData)).tile(tile).tileContent(tileStream.get()).build();
        }
    }
    // not cached or cache access failed
    if (Objects.isNull(queryInput))
        queryInput = tileProvider.getQueryInput(apiData, requestContext.getUriCustomizer(), queryParams, allowedParameters, getGenericQueryInput(apiData), tile);
    TilesQueriesHandler.Query query = null;
    if (queryInput instanceof TilesQueriesHandler.QueryInputTileMbtilesTile)
        query = TilesQueriesHandler.Query.MBTILES_TILE;
    else if (queryInput instanceof TilesQueriesHandler.QueryInputTileTileServerTile)
        query = TilesQueriesHandler.Query.TILESERVER_TILE;
    else if (queryInput instanceof TilesQueriesHandler.QueryInputTileEmpty)
        query = TilesQueriesHandler.Query.EMPTY_TILE;
    else if (queryInput instanceof TilesQueriesHandler.QueryInputTileStream)
        query = TilesQueriesHandler.Query.TILE_STREAM;
    else if (queryInput instanceof TilesQueriesHandler.QueryInputTileMultiLayer)
        query = TilesQueriesHandler.Query.MULTI_LAYER_TILE;
    else if (queryInput instanceof TilesQueriesHandler.QueryInputTileSingleLayer)
        query = TilesQueriesHandler.Query.SINGLE_LAYER_TILE;
    return queryHandler.handle(query, queryInput, requestContext);
}
Also used : Endpoint(de.ii.ogcapi.foundation.domain.Endpoint) CrsTransformationException(de.ii.xtraplatform.crs.domain.CrsTransformationException) LoggerFactory(org.slf4j.LoggerFactory) EndpointTileMultiCollection(de.ii.ogcapi.tiles.infra.EndpointTileMultiCollection) Map(java.util.Map) TileMatrixSetRepository(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSetRepository) ImmutableOgcApiResourceData(de.ii.ogcapi.collections.domain.ImmutableOgcApiResourceData) Tile(de.ii.ogcapi.tiles.domain.Tile) ExtensionRegistry(de.ii.ogcapi.foundation.domain.ExtensionRegistry) Splitter(com.google.common.base.Splitter) ApiOperation(de.ii.ogcapi.foundation.domain.ApiOperation) ApiRequestContext(de.ii.ogcapi.foundation.domain.ApiRequestContext) FeaturesCoreProviders(de.ii.ogcapi.features.core.domain.FeaturesCoreProviders) OgcApi(de.ii.ogcapi.foundation.domain.OgcApi) HttpMethods(de.ii.ogcapi.foundation.domain.HttpMethods) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ImmutableApiEndpointDefinition(de.ii.ogcapi.foundation.domain.ImmutableApiEndpointDefinition) Objects(java.util.Objects) ApiEndpointDefinition(de.ii.ogcapi.foundation.domain.ApiEndpointDefinition) List(java.util.List) TilesQueriesHandler(de.ii.ogcapi.tiles.domain.TilesQueriesHandler) Response(javax.ws.rs.core.Response) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) TileMatrixSet(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSet) CrsTransformerFactory(de.ii.xtraplatform.crs.domain.CrsTransformerFactory) TileProvider(de.ii.ogcapi.tiles.domain.TileProvider) Optional(java.util.Optional) FormatExtension(de.ii.ogcapi.foundation.domain.FormatExtension) ImmutableTile(de.ii.ogcapi.tiles.domain.ImmutableTile) UriInfo(javax.ws.rs.core.UriInfo) TileMatrixSetLimits(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSetLimits) ExtensionConfiguration(de.ii.ogcapi.foundation.domain.ExtensionConfiguration) OgcApiPathParameter(de.ii.ogcapi.foundation.domain.OgcApiPathParameter) QueryInput(de.ii.ogcapi.foundation.domain.QueryInput) TilesConfiguration(de.ii.ogcapi.tiles.domain.TilesConfiguration) MessageFormat(java.text.MessageFormat) TileMatrixSetLimitsGenerator(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSetLimitsGenerator) FeatureTypeConfiguration(de.ii.xtraplatform.features.domain.FeatureTypeConfiguration) OgcApiQueryParameter(de.ii.ogcapi.foundation.domain.OgcApiQueryParameter) TileCache(de.ii.ogcapi.tiles.domain.TileCache) NotAcceptableException(javax.ws.rs.NotAcceptableException) Logger(org.slf4j.Logger) MinMax(de.ii.ogcapi.tiles.domain.MinMax) IOException(java.io.IOException) FeatureProvider2(de.ii.xtraplatform.features.domain.FeatureProvider2) StaticTileProviderStore(de.ii.ogcapi.tiles.domain.StaticTileProviderStore) ServerErrorException(javax.ws.rs.ServerErrorException) TileFormatExtension(de.ii.ogcapi.tiles.domain.TileFormatExtension) Builder(de.ii.ogcapi.tiles.domain.ImmutableQueryInputTileStream.Builder) InputStream(java.io.InputStream) TilesQueriesHandler(de.ii.ogcapi.tiles.domain.TilesQueriesHandler) FeatureProvider2(de.ii.xtraplatform.features.domain.FeatureProvider2) Builder(de.ii.ogcapi.tiles.domain.ImmutableQueryInputTileStream.Builder) TilesConfiguration(de.ii.ogcapi.tiles.domain.TilesConfiguration) NotFoundException(javax.ws.rs.NotFoundException) MinMax(de.ii.ogcapi.tiles.domain.MinMax) QueryInput(de.ii.ogcapi.foundation.domain.QueryInput) TileMatrixSetLimits(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSetLimits) TileMatrixSet(de.ii.ogcapi.tiles.domain.tileMatrixSet.TileMatrixSet) TileFormatExtension(de.ii.ogcapi.tiles.domain.TileFormatExtension) Optional(java.util.Optional) InputStream(java.io.InputStream) Tile(de.ii.ogcapi.tiles.domain.Tile) ImmutableTile(de.ii.ogcapi.tiles.domain.ImmutableTile) OgcApiQueryParameter(de.ii.ogcapi.foundation.domain.OgcApiQueryParameter) Endpoint(de.ii.ogcapi.foundation.domain.Endpoint) CrsTransformationException(de.ii.xtraplatform.crs.domain.CrsTransformationException) NotFoundException(javax.ws.rs.NotFoundException) NotAcceptableException(javax.ws.rs.NotAcceptableException) IOException(java.io.IOException) ServerErrorException(javax.ws.rs.ServerErrorException) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) NotAcceptableException(javax.ws.rs.NotAcceptableException) FeatureTypeConfiguration(de.ii.xtraplatform.features.domain.FeatureTypeConfiguration) ServerErrorException(javax.ws.rs.ServerErrorException)

Aggregations

Splitter (com.google.common.base.Splitter)1 ImmutableOgcApiResourceData (de.ii.ogcapi.collections.domain.ImmutableOgcApiResourceData)1 FeaturesCoreProviders (de.ii.ogcapi.features.core.domain.FeaturesCoreProviders)1 ApiEndpointDefinition (de.ii.ogcapi.foundation.domain.ApiEndpointDefinition)1 ApiOperation (de.ii.ogcapi.foundation.domain.ApiOperation)1 ApiRequestContext (de.ii.ogcapi.foundation.domain.ApiRequestContext)1 Endpoint (de.ii.ogcapi.foundation.domain.Endpoint)1 ExtensionConfiguration (de.ii.ogcapi.foundation.domain.ExtensionConfiguration)1 ExtensionRegistry (de.ii.ogcapi.foundation.domain.ExtensionRegistry)1 FormatExtension (de.ii.ogcapi.foundation.domain.FormatExtension)1 HttpMethods (de.ii.ogcapi.foundation.domain.HttpMethods)1 ImmutableApiEndpointDefinition (de.ii.ogcapi.foundation.domain.ImmutableApiEndpointDefinition)1 OgcApi (de.ii.ogcapi.foundation.domain.OgcApi)1 OgcApiDataV2 (de.ii.ogcapi.foundation.domain.OgcApiDataV2)1 OgcApiPathParameter (de.ii.ogcapi.foundation.domain.OgcApiPathParameter)1 OgcApiQueryParameter (de.ii.ogcapi.foundation.domain.OgcApiQueryParameter)1 QueryInput (de.ii.ogcapi.foundation.domain.QueryInput)1 Builder (de.ii.ogcapi.tiles.domain.ImmutableQueryInputTileStream.Builder)1 ImmutableTile (de.ii.ogcapi.tiles.domain.ImmutableTile)1 MinMax (de.ii.ogcapi.tiles.domain.MinMax)1