diff --git a/xtraplatform-features-geoparquet/src/main/java/de/ii/xtraplatform/features/geoparquet/app/SqlDialectDuckdb.java b/xtraplatform-features-geoparquet/src/main/java/de/ii/xtraplatform/features/geoparquet/app/SqlDialectDuckdb.java index dc6ee9b1d..d6cf69f90 100644 --- a/xtraplatform-features-geoparquet/src/main/java/de/ii/xtraplatform/features/geoparquet/app/SqlDialectDuckdb.java +++ b/xtraplatform-features-geoparquet/src/main/java/de/ii/xtraplatform/features/geoparquet/app/SqlDialectDuckdb.java @@ -99,10 +99,6 @@ public Optional parseExtent(String extent, EpsgCrs crs) { @Override public Optional parseTemporalExtent(String start, String end, ZoneId timeZone) { - if (Objects.isNull(start)) { - return Optional.empty(); - } - DateTimeFormatter parser; if (Objects.isNull(timeZone)) { parser = @@ -118,18 +114,14 @@ public Optional parseTemporalExtent(String start, String end, ZoneId t // Note: Strictly, INFINITY/-INFINITY and INSTANT.MAX/INSTANT.MIN are not the same! May result // in undesired behavior. Instant parsedStart; - switch (start.toUpperCase(Locale.ROOT)) { + switch (Objects.requireNonNullElse(start, "-INFINITY").toUpperCase(Locale.ROOT)) { case "-INFINITY" -> parsedStart = Instant.MIN; case "INFINITY" -> parsedStart = Instant.MAX; default -> parsedStart = parser.parse(start, Instant::from); } - if (Objects.isNull(end)) { - return Optional.of(Interval.of(parsedStart, Instant.MAX)); - } - Instant parsedEnd; - switch (end.toUpperCase(Locale.ROOT)) { + switch (Objects.requireNonNullElse(end, "INFINITY").toUpperCase(Locale.ROOT)) { case "INFINITY" -> parsedEnd = Instant.MAX; case "-INFINITY" -> parsedEnd = Instant.MIN; default -> parsedEnd = parser.parse(end, Instant::from); diff --git a/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfs.java b/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfs.java index 5fbcf366b..d9b46ec14 100644 --- a/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfs.java +++ b/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfs.java @@ -7,34 +7,26 @@ */ package de.ii.xtraplatform.features.gml.app; -import static de.ii.xtraplatform.base.domain.util.LambdaWithException.mayThrow; - import de.ii.xtraplatform.crs.domain.BoundingBox; -import de.ii.xtraplatform.crs.domain.CrsTransformer; -import de.ii.xtraplatform.crs.domain.CrsTransformerFactory; -import de.ii.xtraplatform.crs.domain.EpsgCrs; -import de.ii.xtraplatform.crs.domain.OgcCrs; import de.ii.xtraplatform.features.domain.AggregateStatsReader; import de.ii.xtraplatform.features.domain.FeatureMetadata; import de.ii.xtraplatform.features.domain.FeatureSchema; import de.ii.xtraplatform.features.domain.Metadata; import de.ii.xtraplatform.streams.domain.Reactive; import de.ii.xtraplatform.streams.domain.Reactive.Stream; +import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import java.util.Objects; import java.util.Optional; import org.threeten.extra.Interval; public class AggregateStatsReaderWfs implements AggregateStatsReader { private final FeatureMetadata featureMetadata; - private final Optional crsTransformer; - public AggregateStatsReaderWfs( - FeatureMetadata featureMetadata, - CrsTransformerFactory crsTransformerFactory, - EpsgCrs nativeCrs) { + public AggregateStatsReaderWfs(FeatureMetadata featureMetadata) { this.featureMetadata = featureMetadata; - this.crsTransformer = crsTransformerFactory.getTransformer(OgcCrs.CRS84, nativeCrs, true); } @Override @@ -42,6 +34,7 @@ public Stream getCount(List sourceSchemas) { return Reactive.Source.single(-1L).to(Reactive.Sink.head()); } + /** The bounding box from the capabilities, in CRS84. */ @Override public Stream> getSpatialExtent( List sourceSchemas, boolean is3d) { @@ -52,14 +45,10 @@ public Stream> getSpatialExtent( .map(Metadata::getFeatureTypesBoundingBox) .flatMap( boundingBoxes -> - Optional.ofNullable( - boundingBoxes.get(sourceSchemas.get(0).getSourcePath().get().substring(1)))) - .flatMap( - boundingBox1 -> - crsTransformer.map( - mayThrow( - crsTransformer1 -> - crsTransformer1.transformBoundingBox(boundingBox1)))); + getTypeIds(sourceSchemas.get(0)).stream() + .map(boundingBoxes::get) + .filter(Objects::nonNull) + .findFirst()); return Reactive.Source.single(boundingBox).to(Reactive.Sink.head()); } @@ -68,4 +57,25 @@ public Stream> getSpatialExtent( public Stream> getTemporalExtent(List sourceSchemas) { return Reactive.Source.single(Optional.empty()).to(Reactive.Sink.head()); } + + /** + * The ids under which the capabilities analyzer stores the bounding box of a feature type: the + * prefixed name becomes {@code prefix_localname}, and if unique also {@code localname}, both in + * lower case. The id of the type is the last resort. + */ + static List getTypeIds(FeatureSchema schema) { + String prefixedName = + schema.getSourcePath().map(path -> path.substring(1)).orElse(schema.getName()); + int separator = prefixedName.indexOf(':'); + String localName = prefixedName.substring(separator + 1).toLowerCase(Locale.ROOT); + + // the type id comes last, it may be the local name of another feature type + List ids = new ArrayList<>(); + if (separator > 0) { + ids.add(prefixedName.substring(0, separator).toLowerCase(Locale.ROOT) + "_" + localName); + } + ids.add(localName); + ids.add(schema.getName()); + return ids; + } } diff --git a/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/FeatureProviderWfs.java b/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/FeatureProviderWfs.java index a4e3f57e9..eaf1e9019 100644 --- a/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/FeatureProviderWfs.java +++ b/xtraplatform-features-gml/src/main/java/de/ii/xtraplatform/features/gml/app/FeatureProviderWfs.java @@ -170,9 +170,7 @@ protected boolean onStartup() throws InterruptedException { getData().getNativeCrs().orElse(OgcCrs.CRS84), crsTransformerFactory, cql); - this.aggregateStatsReader = - new AggregateStatsReaderWfs( - this, crsTransformerFactory, getData().getNativeCrs().orElse(OgcCrs.CRS84)); + this.aggregateStatsReader = new AggregateStatsReaderWfs(this); return true; } @@ -299,10 +297,15 @@ public FeatureSchema getSortablesSchema( @Override public Optional getSpatialExtent(String typeName) { - if (getData().getTypes().containsKey(typeName)) { + if (!getData().getTypes().containsKey(typeName)) { return Optional.empty(); } + Optional configured = getConfiguredSpatialExtent(typeName); + if (configured.isPresent()) { + return configured; + } + try { Stream> extentGraph = aggregateStatsReader.getSpatialExtent( @@ -323,24 +326,32 @@ public Optional getSpatialExtent(String typeName) { @Override public Optional getSpatialExtent(String typeName, EpsgCrs crs) { + // a configured extent is in the native CRS, one from the capabilities in CRS84 return getSpatialExtent(typeName) .flatMap( boundingBox -> - crsTransformerFactory - .getTransformer(getNativeCrs(), crs, false) - .flatMap( - crsTransformer -> { - try { - return Optional.of(crsTransformer.transformBoundingBox(boundingBox)); - } catch (CrsTransformationException e) { - return Optional.empty(); - } - })); + Objects.equals(boundingBox.getEpsgCrs(), crs) + ? Optional.of(boundingBox) + : crsTransformerFactory + .getTransformer(boundingBox.getEpsgCrs(), crs, false) + .flatMap( + crsTransformer -> { + try { + return Optional.of( + crsTransformer.transformBoundingBox(boundingBox)); + } catch (CrsTransformationException e) { + return Optional.empty(); + } + })); } @Override public Optional getTemporalExtent(String typeName) { - return Optional.empty(); + if (!getData().getTypes().containsKey(typeName)) { + return Optional.empty(); + } + + return getConfiguredTemporalExtent(typeName); } @Override diff --git a/xtraplatform-features-gml/src/test/groovy/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfsSpec.groovy b/xtraplatform-features-gml/src/test/groovy/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfsSpec.groovy new file mode 100644 index 000000000..0d9881f99 --- /dev/null +++ b/xtraplatform-features-gml/src/test/groovy/de/ii/xtraplatform/features/gml/app/AggregateStatsReaderWfsSpec.groovy @@ -0,0 +1,41 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.gml.app + +import de.ii.xtraplatform.features.domain.ImmutableFeatureSchema +import de.ii.xtraplatform.features.domain.SchemaBase +import spock.lang.Specification + +class AggregateStatsReaderWfsSpec extends Specification { + + def "the bounding box of a prefixed feature type is found under the ids of the capabilities analyzer"() { + + given: 'a type whose source path is the prefixed name of the WFS feature type' + def schema = new ImmutableFeatureSchema.Builder() + .name("rbz") + .sourcePath("/dvg:nw_DVG1_rbz") + .type(SchemaBase.Type.OBJECT) + .build() + + expect: 'prefix_localname and localname in lower case, not the prefixed name, and the type id last' + AggregateStatsReaderWfs.getTypeIds(schema) == ["dvg_nw_dvg1_rbz", "nw_dvg1_rbz", "rbz"] + } + + def "a feature type without a prefix"() { + + given: + def schema = new ImmutableFeatureSchema.Builder() + .name("flurstueck") + .sourcePath("/Flurstueck") + .type(SchemaBase.Type.OBJECT) + .build() + + expect: + AggregateStatsReaderWfs.getTypeIds(schema) == ["flurstueck", "flurstueck"] + } +} diff --git a/xtraplatform-features-graphql/src/main/java/de/ii/xtraplatform/features/graphql/app/FeatureProviderGraphQl.java b/xtraplatform-features-graphql/src/main/java/de/ii/xtraplatform/features/graphql/app/FeatureProviderGraphQl.java index 1b3ed8b32..4f365dde3 100644 --- a/xtraplatform-features-graphql/src/main/java/de/ii/xtraplatform/features/graphql/app/FeatureProviderGraphQl.java +++ b/xtraplatform-features-graphql/src/main/java/de/ii/xtraplatform/features/graphql/app/FeatureProviderGraphQl.java @@ -15,7 +15,6 @@ import de.ii.xtraplatform.cql.domain.Cql; import de.ii.xtraplatform.crs.domain.BoundingBox; import de.ii.xtraplatform.crs.domain.CrsInfo; -import de.ii.xtraplatform.crs.domain.CrsTransformationException; import de.ii.xtraplatform.crs.domain.CrsTransformerFactory; import de.ii.xtraplatform.crs.domain.EpsgCrs; import de.ii.xtraplatform.crs.domain.OgcCrs; @@ -312,7 +311,7 @@ public boolean is3dSupported() { @Override @SuppressWarnings("PMD.AvoidCatchingGenericException") public long getFeatureCount(String typeName) { - if (getData().getTypes().containsKey(typeName)) { + if (!getData().getTypes().containsKey(typeName)) { return -1; } @@ -358,10 +357,15 @@ public FeatureSchema getSortablesSchema( @Override @SuppressWarnings("PMD.AvoidCatchingGenericException") public Optional getSpatialExtent(String typeName) { - if (getData().getTypes().containsKey(typeName)) { + if (!getData().getTypes().containsKey(typeName)) { return Optional.empty(); } + Optional configured = getConfiguredSpatialExtent(typeName); + if (configured.isPresent()) { + return configured; + } + try { Stream> extentGraph = aggregateStatsReader.getSpatialExtent( @@ -383,23 +387,15 @@ public Optional getSpatialExtent(String typeName) { @Override public Optional getSpatialExtent(String typeName, EpsgCrs crs) { return getSpatialExtent(typeName) - .flatMap( - boundingBox -> - crsTransformerFactory - .getTransformer(getNativeCrs(), crs, false) - .flatMap( - crsTransformer -> { - try { - return Optional.of(crsTransformer.transformBoundingBox(boundingBox)); - } catch (CrsTransformationException e) { - return Optional.empty(); - } - })); + .flatMap(boundingBox -> transformSpatialExtent(boundingBox, crs)); } @Override public Optional getTemporalExtent(String typeName) { - return Optional.empty(); + if (!getData().getTypes().containsKey(typeName)) { + return Optional.empty(); + } + return getConfiguredTemporalExtent(typeName); } @Override diff --git a/xtraplatform-features-oracle/src/main/java/de/ii/xtraplatform/features/oracle/app/SqlDialectOras.java b/xtraplatform-features-oracle/src/main/java/de/ii/xtraplatform/features/oracle/app/SqlDialectOras.java index 614429f0c..d797ac4df 100644 --- a/xtraplatform-features-oracle/src/main/java/de/ii/xtraplatform/features/oracle/app/SqlDialectOras.java +++ b/xtraplatform-features-oracle/src/main/java/de/ii/xtraplatform/features/oracle/app/SqlDialectOras.java @@ -124,17 +124,11 @@ public Optional parseExtent(String extent, EpsgCrs crs) { @Override public Optional parseTemporalExtent(String start, String end, ZoneId timeZone) { - if (Objects.isNull(start)) { - return Optional.empty(); - } DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd[['T'][' ']HH:mm:ss][.SSS][X]") .withZone(ZoneOffset.UTC); - Instant parsedStart = parser.parse(start, Instant::from); - if (Objects.isNull(end)) { - return Optional.of(Interval.of(parsedStart, Instant.MAX)); - } - Instant parsedEnd = parser.parse(end, Instant::from); + Instant parsedStart = Objects.isNull(start) ? Instant.MIN : parser.parse(start, Instant::from); + Instant parsedEnd = Objects.isNull(end) ? Instant.MAX : parser.parse(end, Instant::from); return Optional.of(Interval.of(parsedStart, parsedEnd)); } diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGenerator.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGenerator.java index c510cbe4f..562c98b92 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGenerator.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGenerator.java @@ -7,11 +7,14 @@ */ package de.ii.xtraplatform.features.sql.app; +import de.ii.xtraplatform.cql.domain.And; +import de.ii.xtraplatform.cql.domain.Cql2Expression; import de.ii.xtraplatform.features.sql.domain.SqlDialect; import de.ii.xtraplatform.features.sql.domain.SqlQueryColumn; import de.ii.xtraplatform.features.sql.domain.SqlQueryColumn.Operation; import de.ii.xtraplatform.features.sql.domain.SqlQueryMapping; import de.ii.xtraplatform.features.sql.domain.SqlQuerySchema; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -22,6 +25,13 @@ public class AggregateStatsQueryGenerator { private static final String FORMAT_TABLE_ALIAS = "%s %s"; private static final String FORMAT_WHERE = " WHERE %s"; + /** + * The temporal extent queries return no row if there is nothing to aggregate, so that NULL in the + * result always stands for an unbounded start or end. + */ + private static final String FORMAT_TEMPORAL_EXTENT = + "SELECT T.extent_start, T.extent_end FROM (%s) T WHERE T.row_count > 0"; + private final SqlDialect sqlDialect; private final FilterEncoderSql filterEncoder; @@ -31,13 +41,30 @@ public AggregateStatsQueryGenerator(SqlDialect sqlDialect, FilterEncoderSql filt } public String getCountQuery(SqlQueryMapping mapping) { + return getCountQuery(mapping, List.of()); + } + + /** + * Counts the features that match the filter of the main table and the additional filters, e.g. + * the restriction of all queries to a fixed extent. + */ + public String getCountQuery(SqlQueryMapping mapping, List additionalFilters) { SqlQuerySchema sourceSchema = mapping.getMainTable(); List aliases = AliasGenerator.getAliases(sourceSchema); String mainTable = String.format(FORMAT_TABLE_ALIAS, sourceSchema.getName(), aliases.get(0)); - Optional filter = getFilter(mapping, sourceSchema); + List filters = new ArrayList<>(); + sourceSchema.getFilter().ifPresent(filters::add); + filters.addAll(additionalFilters); + + Optional filter = + filters.isEmpty() + ? Optional.empty() + : Optional.of( + filterEncoder.encode( + filters.size() == 1 ? filters.get(0) : And.of(filters), mapping)); String where = filter.isPresent() ? String.format(FORMAT_WHERE, filter.get()) : ""; return String.format("SELECT COUNT(*) FROM %s%s", mainTable, where); @@ -91,8 +118,10 @@ public String getTemporalExtentQuery( String where = filter.isPresent() ? String.format(FORMAT_WHERE, filter.get()) : ""; return String.format( - "SELECT MIN(%s), MAX(%s) FROM %s%s%s%s", - column, column, mainTable, join.isEmpty() ? "" : " ", join, where); + FORMAT_TEMPORAL_EXTENT, + String.format( + "SELECT MIN(%s) AS extent_start, MAX(%s) AS extent_end, COUNT(%s) AS row_count FROM %s%s%s%s", + column, column, column, mainTable, join.isEmpty() ? "" : " ", join, where)); } public String getTemporalExtentQuery( @@ -128,8 +157,15 @@ public String getTemporalExtentQuery( String where = filter.isPresent() ? String.format(FORMAT_WHERE, filter.get()) : ""; return String.format( - "SELECT MIN(%s), MAX(%s) FROM %s%s%s%s", - columnStart, columnEnd, mainTable, join.isEmpty() ? "" : " ", join, where); + FORMAT_TEMPORAL_EXTENT, + String.format( + "SELECT %s AS extent_start, %s AS extent_end, COUNT(*) AS row_count FROM %s%s%s%s", + minOrUnbounded(columnStart), + maxOrUnbounded(columnEnd), + mainTable, + join.isEmpty() ? "" : " ", + join, + where)); } else { List startAliases = AliasGenerator.getAliases(intervalStart); @@ -159,11 +195,68 @@ public String getTemporalExtentQuery( String where = filter.isPresent() ? String.format(FORMAT_WHERE, filter.get()) : ""; return String.format( - "SELECT * FROM (SELECT MIN(%s) FROM %s%s) AS A, (SELECT MAX(%s) from %s%s) AS B;", - columnStart, startTableWithJoins, where, columnEnd, endTableWithJoins, where); + "SELECT S.extent_start, E.extent_end FROM (SELECT %s AS extent_start, COUNT(*) AS" + + " row_count FROM %s%s) S, (SELECT %s AS extent_end FROM %s%s) E WHERE S.row_count >" + + " 0", + minOrUnbounded(columnStart), + startTableWithJoins, + where, + maxOrUnbounded(columnEnd), + endTableWithJoins, + where); } } + /** + * The temporal extent of an interval with only a start or only an end; the other side is + * unbounded. + * + * @param mapping the type + * @param bound the table of the start or end + * @param boundColumn the column of the start or end + * @param isStart whether the column is the start + * @return the query + */ + public String getTemporalExtentQuery( + SqlQueryMapping mapping, SqlQuerySchema bound, SqlQueryColumn boundColumn, boolean isStart) { + SqlQuerySchema mainSchema = mapping.getMainTable(); + + List aliases = AliasGenerator.getAliases(bound); + String boundAlias = aliases.get(aliases.size() - 1); + + String mainTable = String.format(FORMAT_TABLE_ALIAS, mainSchema.getName(), aliases.get(0)); + + String column = + SqlQueryColumnOperations.getQualifiedColumnResolved( + boundAlias, SqlQueryColumnOperations.dateToDatetime(boundColumn), sqlDialect); + + String join = JoinGenerator.getJoins(bound, aliases, filterEncoder); + + Optional filter = getFilter(mapping, mainSchema); + String where = filter.isPresent() ? String.format(FORMAT_WHERE, filter.get()) : ""; + + return String.format( + FORMAT_TEMPORAL_EXTENT, + String.format( + "SELECT %s AS extent_start, %s AS extent_end, COUNT(*) AS row_count FROM %s%s%s%s", + isStart ? minOrUnbounded(column) : "NULL", + isStart ? "NULL" : maxOrUnbounded(column), + mainTable, + join.isEmpty() ? "" : " ", + join, + where)); + } + + /** The earliest start, or NULL (unbounded) if any start is NULL. */ + private static String minOrUnbounded(String column) { + return String.format("CASE WHEN COUNT(*) > COUNT(%1$s) THEN NULL ELSE MIN(%1$s) END", column); + } + + /** The latest end, or NULL (unbounded) if any end is NULL. */ + private static String maxOrUnbounded(String column) { + return String.format("CASE WHEN COUNT(*) > COUNT(%1$s) THEN NULL ELSE MAX(%1$s) END", column); + } + private Optional getFilter(SqlQueryMapping mapping, SqlQuerySchema schemaSql) { return schemaSql.getFilter().map(cql -> filterEncoder.encode(cql, mapping)); } diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsReaderSql.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsReaderSql.java index bda3de787..a0d1c9496 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsReaderSql.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/AggregateStatsReaderSql.java @@ -8,6 +8,7 @@ package de.ii.xtraplatform.features.sql.app; import de.ii.xtraplatform.base.domain.util.Tuple; +import de.ii.xtraplatform.cql.domain.Cql2Expression; import de.ii.xtraplatform.crs.domain.BoundingBox; import de.ii.xtraplatform.crs.domain.EpsgCrs; import de.ii.xtraplatform.features.domain.AggregateStatsReader; @@ -50,6 +51,11 @@ public AggregateStatsReaderSql( @Override public Stream getCount(List sourceSchemas) { + return getCount(sourceSchemas, List.of()); + } + + /** Like {@link #getCount(List)}, restricted to the features that match the filters. */ + public Stream getCount(List sourceSchemas, List filters) { return Reactive.Source.iterable(sourceSchemas) .via( Reactive.Transformer.flatMap( @@ -57,7 +63,8 @@ public Stream getCount(List sourceSchemas) { sqlClient .get() .getSourceStream( - queryGenerator.getCountQuery(schemaSql), SqlQueryOptions.single()))) + queryGenerator.getCountQuery(schemaSql, filters), + SqlQueryOptions.single()))) .via(Reactive.Transformer.map(sqlRow -> Long.parseLong((String) sqlRow.getValues().get(0)))) .to(Reactive.Sink.reduce(0L, Long::sum)); } @@ -136,6 +143,21 @@ public Stream> getTemporalExtent(List source SqlQueryOptions.tuple()); } + // only a start or only an end, the other side is unbounded + Optional> bound = + intervalStart.or(() -> intervalEnd); + if (bound.isPresent()) { + return sqlClient + .get() + .getSourceStream( + queryGenerator.getTemporalExtentQuery( + mapping, + bound.get().first(), + bound.get().second(), + intervalStart.isPresent()), + SqlQueryOptions.tuple()); + } + return Source.empty(); })) .via( diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/SqlMutationSession.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/SqlMutationSession.java index f356b1161..cff3e7414 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/SqlMutationSession.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/app/SqlMutationSession.java @@ -156,9 +156,10 @@ public FeatureTransactions.MutationResult createFeatures( .hasFeatures(false); List collected = new ArrayList<>(); + FeatureTokenStatsCollector.Stats stats = new FeatureTokenStatsCollector.Stats(crs); try { for (FeatureTokenSource src : featureTokenSources) { - drainSource(src, mapping, crs, builder, collected, false); + drainSource(src, mapping, crs, stats, collected, false); } } catch (RuntimeException e) { return builder.error(translateEncoderError(e)).build(); @@ -172,7 +173,9 @@ public FeatureTransactions.MutationResult createFeatures( for (FeatureDataSql feature : collected) { applyRoleOverrides(feature, roleOverrides); } + stats.applyRoleOverrides(roleOverrides); } + stats.writeTo(builder); RowCursor rowCursor = new RowCursor(mapping.getMainTable().getFullPath()); @@ -1721,11 +1724,13 @@ private FeatureTransactions.MutationResult writeFeatures( ImmutableMutationResult.builder().type(type).hasFeatures(false); List collected = new ArrayList<>(); + FeatureTokenStatsCollector.Stats stats = new FeatureTokenStatsCollector.Stats(crs); try { - drainSource(featureTokenSource, mapping, crs, builder, collected, partial); + drainSource(featureTokenSource, mapping, crs, stats, collected, partial); } catch (RuntimeException e) { return builder.error(translateEncoderError(e)).build(); } + stats.writeTo(builder); RowCursor rowCursor = new RowCursor(mapping.getMainTable().getFullPath()); boolean deleteFirst = @@ -1743,17 +1748,16 @@ private FeatureTransactions.MutationResult writeFeatures( // Assembles the token-source → FeatureEncoderSql pipeline and drains it into `collected`. A // fresh FeatureTokenStatsCollector is instantiated per source so internal transformer state - // doesn't leak across runs; the result builder is shared, so accumulated stats (bbox, temporal - // extent) overwrite per source — matching the existing single-source-multi-feature semantics - // where the last feature's stats win. + // doesn't leak across runs; the stats are shared, so the extents (bbox, temporal extent) cover + // the features of all sources. private void drainSource( FeatureTokenSource featureTokenSource, SqlQueryMapping mapping, EpsgCrs crs, - ImmutableMutationResult.Builder builder, + FeatureTokenStatsCollector.Stats stats, List collected, boolean partial) { - FeatureTokenStatsCollector statsCollector = new FeatureTokenStatsCollector(builder, crs); + FeatureTokenStatsCollector statsCollector = new FeatureTokenStatsCollector(stats, partial); Source featureSqlSource = featureTokenSource diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSql.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSql.java index 45dc7e82e..2afe4bf2c 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSql.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSql.java @@ -36,12 +36,12 @@ import de.ii.xtraplatform.entities.domain.Entity; import de.ii.xtraplatform.entities.domain.Entity.SubType; import de.ii.xtraplatform.features.domain.AbstractFeatureProvider; -import de.ii.xtraplatform.features.domain.AggregateStatsReader; import de.ii.xtraplatform.features.domain.ConnectionInfo; import de.ii.xtraplatform.features.domain.ConnectorFactory; import de.ii.xtraplatform.features.domain.DatasetChangeListener; import de.ii.xtraplatform.features.domain.DecoderFactories; import de.ii.xtraplatform.features.domain.DecoderFactory; +import de.ii.xtraplatform.features.domain.ExtentFilters; import de.ii.xtraplatform.features.domain.FeatureChangeListener; import de.ii.xtraplatform.features.domain.FeatureCrs; import de.ii.xtraplatform.features.domain.FeatureEventHandler.ModifiableContext; @@ -466,7 +466,7 @@ public class FeatureProviderSql private final Scheduler scheduler; private FeatureQueryEncoderSql queryTransformer; - private AggregateStatsReader aggregateStatsReader; + private AggregateStatsReaderSql aggregateStatsReader; private FeatureMutationsSql featureMutationsSql; private PathParserSql pathParser2; private SqlPathParser pathParser3; @@ -1041,7 +1041,9 @@ public long getFeatureCount(String typeName) { } try { - Stream countGraph = aggregateStatsReader.getCount(queryMappings.get(typeName)); + // count what queries for multiple features return, i.e. within a fixed extent if restricted + Stream countGraph = + aggregateStatsReader.getCount(queryMappings.get(typeName), extentFilters(typeName)); Long count = countGraph @@ -1072,6 +1074,11 @@ public Optional getSpatialExtent(String typeName) { return Optional.empty(); } + Optional configured = getConfiguredSpatialExtent(typeName); + if (configured.isPresent()) { + return configured; + } + String[] cacheKey = {typeName, CACHE_KEY_STATS, "spatial"}; String cacheValidator = getData().getStableHash(); @@ -1121,21 +1128,9 @@ public Optional getSpatialExtent(String typeName) { } @Override - @SuppressWarnings("PMD.AvoidCatchingGenericException") public Optional getSpatialExtent(String typeName, EpsgCrs crs) { return getSpatialExtent(typeName) - .flatMap( - boundingBox -> - crsTransformerFactory - .getTransformer(getNativeCrs(), crs, false) - .flatMap( - crsTransformer -> { - try { - return Optional.of(crsTransformer.transformBoundingBox(boundingBox)); - } catch (Exception e) { - return Optional.empty(); - } - })); + .flatMap(boundingBox -> transformSpatialExtent(boundingBox, crs)); } @Override @@ -1149,6 +1144,11 @@ public Optional getTemporalExtent(String typeName) { return Optional.empty(); } + Optional configured = getConfiguredTemporalExtent(typeName); + if (configured.isPresent()) { + return configured; + } + String[] cacheKey = {typeName, CACHE_KEY_STATS, "temporal"}; String cacheValidator = getData().getStableHash(); @@ -1422,7 +1422,9 @@ protected Query preprocessQuery(Query query) { } @SuppressWarnings("PMD.CognitiveComplexity") - protected Query preprocessQuery(Query query, List resultSetTables) { + protected Query preprocessQuery(Query originalQuery, List resultSetTables) { + Query query = restrictToExtent(originalQuery); + if (query instanceof FeatureQuery && (((FeatureQuery) query).getFields().size() > 1 || !"*".equals(((FeatureQuery) query).getFields().get(0)))) { @@ -1495,6 +1497,26 @@ protected Query preprocessQuery(Query query, List resultSetTables) { return query; } + private Query restrictToExtent(Query query) { + return ExtentFilters.restrict(query, this::extentFilters); + } + + private List extentFilters(String typeName) { + Optional settings = Optional.ofNullable(getData().getQueryGeneration()); + boolean spatial = settings.map(QueryGeneratorSettings::getRestrictToSpatialExtent).orElse(true); + boolean temporal = + settings.map(QueryGeneratorSettings::getRestrictToTemporalExtent).orElse(true); + + return Optional.ofNullable(getData().getTypes().get(typeName)) + .map( + type -> + ExtentFilters.of( + type, + spatial ? getConfiguredSpatialExtent(typeName) : Optional.empty(), + temporal ? getConfiguredTemporalExtent(typeName) : Optional.empty())) + .orElse(List.of()); + } + @Override public FeatureStream getFeatureStream(MultiFeatureQuery query) { validateQuery(query); diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSqlData.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSqlData.java index 3613ba116..c72118361 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSqlData.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureProviderSqlData.java @@ -228,6 +228,38 @@ default boolean getNativeDatetimeComparison() { return false; } + /** + * @langEn Restrict all queries for multiple features to the spatial extent of the type, if a + * fixed spatial extent is configured. Features are selected if they intersect the extent, + * so they may reach beyond it. Queries for a single feature by id are not affected. + * @langDe Schränkt alle Abfragen mehrerer Features auf den räumlichen Extent des Types ein, + * wenn ein fester räumlicher Extent konfiguriert ist. Selektiert werden Features, die den + * Extent schneiden, sie können also über ihn hinausreichen. Abfragen eines einzelnen + * Features per Id sind nicht betroffen. + * @default true + * @since v4.9 + */ + @Value.Default + default boolean getRestrictToSpatialExtent() { + return true; + } + + /** + * @langEn Restrict all queries for multiple features to the temporal extent of the type, if a + * fixed temporal extent is configured. Features are selected if they intersect the extent, + * so they may reach beyond it. Queries for a single feature by id are not affected. + * @langDe Schränkt alle Abfragen mehrerer Features auf den zeitlichen Extent des Types ein, + * wenn ein fester zeitlicher Extent konfiguriert ist. Selektiert werden Features, die den + * Extent schneiden, sie können also über ihn hinausreichen. Abfragen eines einzelnen + * Features per Id sind nicht betroffen. + * @default true + * @since v4.9 + */ + @Value.Default + default boolean getRestrictToTemporalExtent() { + return true; + } + @DocIgnore @Value.Default default Optional getAccentiCollation() { diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollector.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollector.java index 318d45417..7c5777f7f 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollector.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollector.java @@ -9,6 +9,7 @@ import de.ii.xtraplatform.crs.domain.BoundingBox; import de.ii.xtraplatform.crs.domain.EpsgCrs; +import de.ii.xtraplatform.features.domain.FeatureTransactions; import de.ii.xtraplatform.features.domain.ImmutableMutationResult.Builder; import de.ii.xtraplatform.features.domain.SchemaBase.Role; import de.ii.xtraplatform.features.domain.Tuple; @@ -18,24 +19,32 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeParseException; +import java.util.Map; import java.util.Objects; +/** + * Collects the spatial and temporal extent of the features of one token source into {@link Stats}, + * which can be shared by all sources of a mutation. + */ public class FeatureTokenStatsCollector extends FeatureTokenTransformerSql { - private final Builder builder; - private final EpsgCrs crs; + private final Stats stats; + private final boolean partial; private int dim = 2; - private Double xmin; - private Double ymin; - private Double xmax; - private Double ymax; - private String start = ""; - private String end = ""; - - public FeatureTokenStatsCollector(Builder builder, EpsgCrs crs) { + // the values of the current feature; null if not given, PATCH_NULL_VALUE if cleared + private String featureInstant; + private String featureStart; + private String featureEnd; + + /** + * @param stats the extents of the mutation + * @param partial whether the features only carry the properties that change, so that a missing + * start or end leaves the extent as it is instead of making it unbounded + */ + public FeatureTokenStatsCollector(Stats stats, boolean partial) { super(); - this.builder = builder; - this.crs = crs; + this.stats = stats; + this.partial = partial; } @Override @@ -47,62 +56,77 @@ public void onStart(ModifiableContext context) } @Override - public void onEnd(ModifiableContext context) { - if (xmin != null && ymin != null && xmax != null && ymax != null) { - builder.spatialExtent(BoundingBox.of(xmin, ymin, xmax, ymax, crs)); - } + // null stands for a value that the next feature does not give + @SuppressWarnings("PMD.NullAssignment") + public void onFeatureStart(ModifiableContext context) { + this.featureInstant = null; + this.featureStart = null; + this.featureEnd = null; + + super.onFeatureStart(context); + } + + @Override + public void onFeatureEnd(ModifiableContext context) { + SqlQueryMapping mapping = context.mapping(); - if (!start.isEmpty() || !end.isEmpty()) { - builder.temporalExtent(Tuple.of(parseTemporal(start), parseTemporal(end))); + if (mapping.getSchemaForRole(Role.PRIMARY_INSTANT).isPresent()) { + if (Objects.nonNull(featureInstant) && !isCleared(featureInstant)) { + stats.addInstant(featureInstant); + } + } else if (mapping.getSchemaForRole(Role.PRIMARY_INTERVAL_START).isPresent() + || mapping.getSchemaForRole(Role.PRIMARY_INTERVAL_END).isPresent()) { + addInterval(mapping); } - super.onEnd(context); + super.onFeatureEnd(context); } - private Long parseTemporal(String temporal) { - if (temporal.isEmpty()) { - return null; + private void addInterval(SqlQueryMapping mapping) { + // a type with only one of the two roles is unbounded on the other side + if (mapping.getSchemaForRole(Role.PRIMARY_INTERVAL_START).isEmpty()) { + stats.addIntervalStart(null); + } else if (isDetermined(featureStart)) { + stats.addIntervalStart(boundOrUnbounded(featureStart)); } - try { - if (temporal.length() > 10) { - return ZonedDateTime.parse(temporal).toInstant().toEpochMilli(); - } - return LocalDate.parse(temporal).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli(); - } catch (DateTimeParseException e) { - return null; + if (mapping.getSchemaForRole(Role.PRIMARY_INTERVAL_END).isEmpty()) { + stats.addIntervalEnd(null); + } else if (isDetermined(featureEnd)) { + stats.addIntervalEnd(boundOrUnbounded(featureEnd)); } } + // a missing value leaves a partial update as it is, but is unbounded for a complete feature + private boolean isDetermined(String value) { + return !partial || Objects.nonNull(value); + } + + private static String boundOrUnbounded(String value) { + return Objects.isNull(value) || isCleared(value) ? null : value; + } + + private static boolean isCleared(String value) { + return Objects.equals(value, FeatureTransactions.PATCH_NULL_VALUE); + } + @Override - @SuppressWarnings("PMD.CyclomaticComplexity") public void onValue(ModifiableContext context) { String value = context.value(); - if (Objects.isNull(value)) { - super.onValue(context); - return; - } - - if (hasRole(context, Role.PRIMARY_INSTANT)) { - if (start.isEmpty() || value.compareTo(start) < 0) { - this.start = value; + if (Objects.nonNull(value)) { + if (hasRole(context, Role.PRIMARY_INSTANT)) { + this.featureInstant = value; + } else if (hasRole(context, Role.PRIMARY_INTERVAL_START)) { + this.featureStart = value; + } else if (hasRole(context, Role.PRIMARY_INTERVAL_END)) { + this.featureEnd = value; } - if (end.isEmpty() || value.compareTo(end) > 0) { - this.end = value; - } - } else if (hasRole(context, Role.PRIMARY_INTERVAL_START) - && (start.isEmpty() || value.compareTo(start) < 0)) { - this.start = value; - } else if (hasRole(context, Role.PRIMARY_INTERVAL_END) - && (end.isEmpty() || value.compareTo(end) > 0)) { - this.end = value; } super.onValue(context); } @Override - @SuppressWarnings({"PMD.CognitiveComplexity", "PMD.CyclomaticComplexity"}) public void onGeometry(ModifiableContext context) { // Only the primary geometry feeds the spatial extent: secondary geometry properties may // store positions in a different CRS (position variants), which must not be interpreted in @@ -113,10 +137,7 @@ public void onGeometry(ModifiableContext contex double[][] minMax = value.accept(new MinMaxDeriver()); if (minMax.length > 1 && minMax[0].length >= dim && minMax[1].length >= dim) { - this.xmin = xmin == null ? minMax[0][0] : Math.min(xmin, minMax[0][0]); - this.xmax = xmax == null ? minMax[1][0] : Math.max(xmax, minMax[1][0]); - this.ymin = ymin == null ? minMax[0][1] : Math.min(ymin, minMax[0][1]); - this.ymax = ymax == null ? minMax[1][1] : Math.max(ymax, minMax[1][1]); + stats.addBoundingBox(minMax[0][0], minMax[0][1], minMax[1][0], minMax[1][1]); } } @@ -130,4 +151,136 @@ private boolean hasRole(ModifiableContext conte .filter(schema -> Objects.equals(schema.getFullPathAsString(), context.pathAsString())) .isPresent(); } + + /** + * The extents of the features written by one mutation. In the temporal extent, null stands for an + * unbounded start or end, as soon as one feature has no start or no end. + */ + public static final class Stats { + + // A side without a value, e.g. the start of a partial update that only clears the end, must + // not change the extent; since extents only grow, the latest start and the earliest end are + // the values that never do. Null would stand for an unbounded side. + private static final long NO_START = Long.MAX_VALUE; + private static final long NO_END = Long.MIN_VALUE; + + private final EpsgCrs crs; + private Double xmin; + private Double ymin; + private Double xmax; + private Double ymax; + // earliest start and latest end that are set + private String start; + private String end; + private boolean startUnbounded; + private boolean endUnbounded; + // whether a written feature determines the start or the end; not the case for a partial + // update that leaves them as they are + private boolean startKnown; + private boolean endKnown; + + public Stats(EpsgCrs crs) { + this.crs = crs; + } + + void addBoundingBox(double minX, double minY, double maxX, double maxY) { + this.xmin = xmin == null ? minX : Math.min(xmin, minX); + this.ymin = ymin == null ? minY : Math.min(ymin, minY); + this.xmax = xmax == null ? maxX : Math.max(xmax, maxX); + this.ymax = ymax == null ? maxY : Math.max(ymax, maxY); + } + + void addInstant(String value) { + addIntervalStart(value); + addIntervalEnd(value); + } + + void addIntervalStart(String value) { + this.startKnown = true; + if (Objects.isNull(value)) { + this.startUnbounded = true; + } else if (Objects.isNull(start) || value.compareTo(start) < 0) { + this.start = value; + } + } + + void addIntervalEnd(String value) { + this.endKnown = true; + if (Objects.isNull(value)) { + this.endUnbounded = true; + } else if (Objects.isNull(end) || value.compareTo(end) > 0) { + this.end = value; + } + } + + /** + * Applies values that the mutation sets for all of its features, e.g. the start and the open + * end of new versions. A null value is an unbounded start or end. + * + * @param overrides the values per role + */ + public void applyRoleOverrides(Map overrides) { + if (overrides.containsKey(Role.PRIMARY_INTERVAL_START)) { + this.start = Objects.toString(overrides.get(Role.PRIMARY_INTERVAL_START), null); + this.startUnbounded = Objects.isNull(start); + this.startKnown = true; + } + if (overrides.containsKey(Role.PRIMARY_INTERVAL_END)) { + this.end = Objects.toString(overrides.get(Role.PRIMARY_INTERVAL_END), null); + this.endUnbounded = Objects.isNull(end); + this.endKnown = true; + } + } + + /** + * Sets the extents of the mutation result. A side that no written feature determines takes a + * value of the other side: extents only grow, and the start of a feature is never after its + * end, so that value does not change the extent on the unknown side. If the other side is + * unbounded and has no value, the unknown side gets a value that never changes an extent. + * Without any temporal value, the result has no temporal extent. + * + * @param builder the result of the mutation + */ + public void writeTo(Builder builder) { + if (xmin != null && ymin != null && xmax != null && ymax != null) { + builder.spatialExtent(BoundingBox.of(xmin, ymin, xmax, ymax, crs)); + } + + writeTemporal(builder); + } + + private void writeTemporal(Builder builder) { + String startValue = startKnown ? start : end; + String endValue = endKnown ? end : start; + boolean startOpen = startKnown && startUnbounded; + boolean endOpen = endKnown && endUnbounded; + + boolean nothingWritten = + !startOpen && !endOpen && (Objects.isNull(startValue) || Objects.isNull(endValue)); + if (nothingWritten) { + return; + } + + builder.temporalExtent( + Tuple.of(bound(startOpen, startValue, NO_START), bound(endOpen, endValue, NO_END))); + } + + private static Long bound(boolean unbounded, String value, long noValue) { + return unbounded ? null : parseTemporal(value, noValue); + } + + private static Long parseTemporal(String temporal, long noValue) { + if (Objects.isNull(temporal)) { + return noValue; + } + try { + if (temporal.length() > 10) { + return ZonedDateTime.parse(temporal).toInstant().toEpochMilli(); + } + return LocalDate.parse(temporal).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli(); + } catch (DateTimeParseException e) { + return noValue; + } + } + } } diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialect.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialect.java index 3835ee993..77174ff11 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialect.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialect.java @@ -177,6 +177,12 @@ default String dropResultSetTable(String name) { Optional parseExtent(String extent, EpsgCrs crs); + /** + * @param start the earliest start, or null if the start is unbounded + * @param end the latest end, or null if the end is unbounded + * @param timeZone the time zone of values without an offset + * @return the temporal extent + */ Optional parseTemporalExtent(String start, String end, ZoneId timeZone); String escapeString(String value); diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectGpkg.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectGpkg.java index 708a50da1..10600f3b7 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectGpkg.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectGpkg.java @@ -83,17 +83,11 @@ public Optional parseExtent(String extent, EpsgCrs crs) { @Override public Optional parseTemporalExtent(String start, String end, ZoneId timeZone) { - if (Objects.isNull(start)) { - return Optional.empty(); - } DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd[['T'][' ']HH:mm:ss][.SSS][X]") .withZone(ZoneOffset.UTC); - Instant parsedStart = parser.parse(start, Instant::from); - if (Objects.isNull(end)) { - return Optional.of(Interval.of(parsedStart, Instant.MAX)); - } - Instant parsedEnd = parser.parse(end, Instant::from); + Instant parsedStart = Objects.isNull(start) ? Instant.MIN : parser.parse(start, Instant::from); + Instant parsedEnd = Objects.isNull(end) ? Instant.MAX : parser.parse(end, Instant::from); return Optional.of(Interval.of(parsedStart, parsedEnd)); } diff --git a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectPgis.java b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectPgis.java index 19717fdbe..b42b47571 100644 --- a/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectPgis.java +++ b/xtraplatform-features-sql/src/main/java/de/ii/xtraplatform/features/sql/domain/SqlDialectPgis.java @@ -175,15 +175,8 @@ public Optional parseExtent(String extent, EpsgCrs crs) { @Override public Optional parseTemporalExtent(String start, String end, ZoneId timeZone) { - if (Objects.isNull(start)) { - return Optional.empty(); - } - - Instant parsedStart = parseInstant(start, timeZone); - if (Objects.isNull(end)) { - return Optional.of(Interval.of(parsedStart, Instant.MAX)); - } - Instant parsedEnd = parseInstant(end, timeZone); + Instant parsedStart = Objects.isNull(start) ? Instant.MIN : parseInstant(start, timeZone); + Instant parsedEnd = Objects.isNull(end) ? Instant.MAX : parseInstant(end, timeZone); return Optional.of(Interval.of(parsedStart, parsedEnd)); } diff --git a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGeneratorSpec.groovy b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGeneratorSpec.groovy new file mode 100644 index 000000000..4d5daf944 --- /dev/null +++ b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/AggregateStatsQueryGeneratorSpec.groovy @@ -0,0 +1,144 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.sql.app + +import de.ii.xtraplatform.cql.app.CqlImpl +import de.ii.xtraplatform.cql.domain.Bbox +import de.ii.xtraplatform.cql.domain.Property +import de.ii.xtraplatform.cql.domain.SIntersects +import de.ii.xtraplatform.cql.domain.SpatialLiteral +import de.ii.xtraplatform.cql.domain.TIntersects +import de.ii.xtraplatform.cql.domain.TemporalLiteral +import de.ii.xtraplatform.crs.domain.OgcCrs +import de.ii.xtraplatform.features.domain.FeatureSchemaFixtures +import de.ii.xtraplatform.features.domain.MappingOperationResolver +import de.ii.xtraplatform.features.domain.MappingRuleFixtures +import de.ii.xtraplatform.features.json.app.DecoderFactoryJson +import de.ii.xtraplatform.features.sql.domain.ImmutableQueryGeneratorSettings +import de.ii.xtraplatform.features.sql.domain.ImmutableSqlPathDefaults +import de.ii.xtraplatform.features.sql.domain.ImmutableSqlQuerySchema +import de.ii.xtraplatform.features.sql.domain.SqlDialectPgis +import de.ii.xtraplatform.features.sql.domain.SqlPathParser +import de.ii.xtraplatform.features.sql.domain.SqlQueryMapping +import spock.lang.Shared +import spock.lang.Specification + +import java.time.Instant +import java.util.function.Function + +class AggregateStatsQueryGeneratorSpec extends Specification { + + static final String INTERSECTS = "ST_Intersects(A.geom, ST_GeomFromText('POLYGON((7.0 50.0,7.1 50.0,7.1 50.1,7.0 50.1,7.0 50.0))',4326))" + + @Shared + SqlQueryMapping unfaelleMapping + + @Shared + AggregateStatsQueryGenerator generator + + def setupSpec() { + def cql = new CqlImpl() + def pathParser = new SqlPathParser(new ImmutableSqlPathDefaults.Builder().build(), cql, + Map.of("JSON", new DecoderFactoryJson(), "EXPRESSION", new DecoderFactorySqlExpression())) + def mappingDeriver = new SqlMappingDeriver(pathParser, new ImmutableQueryGeneratorSettings.Builder().build()) + def schema = FeatureSchemaFixtures.fromYaml("strassen_unfaelle2") + def resolved = schema.accept(new MappingOperationResolver(), List.of()) + unfaelleMapping = mappingDeriver.derive(MappingRuleFixtures.fromYaml("strassen_unfaelle2"), resolved).get(0) + + def filterEncoder = new FilterEncoderSql(OgcCrs.CRS84, new SqlDialectPgis(), null, null, new CqlImpl(), List.of(), null, + { type -> Optional.empty() } as Function, Map.of()) + generator = new AggregateStatsQueryGenerator(new SqlDialectPgis(), filterEncoder) + } + + def 'without additional filters the count is unrestricted'() { + + when: + String actual = generator.getCountQuery(unfaelleMapping) + + then: + actual == "SELECT COUNT(*) FROM unfaelle_point A" + } + + def 'a restriction to a fixed spatial extent becomes the where clause of the count'() { + + given: 'the filter that restricts queries for multiple features to the extent' + def filter = SIntersects.of(Property.of("geometry"), SpatialLiteral.of(Bbox.of(7.0, 50.0, 7.1, 50.1, OgcCrs.CRS84))) + + when: + String actual = generator.getCountQuery(unfaelleMapping, List.of(filter)) + + then: 'the count matches what queries for multiple features return' + actual == "SELECT COUNT(*) FROM unfaelle_point A WHERE ${INTERSECTS}" + } + + def 'spatial and temporal restrictions are combined'() { + + given: + def spatial = SIntersects.of(Property.of("geometry"), SpatialLiteral.of(Bbox.of(7.0, 50.0, 7.1, 50.1, OgcCrs.CRS84))) + def temporal = TIntersects.of(Property.of("unfzeit"), + new TemporalLiteral.Builder(org.threeten.extra.Interval.of(Instant.parse("2019-01-01T00:00:00Z"), Instant.parse("2019-12-31T23:59:59Z"))).build()) + + when: + String actual = generator.getCountQuery(unfaelleMapping, List.of(spatial, temporal)) + + then: 'both predicates are part of a single conjunction' + actual.startsWith("SELECT COUNT(*) FROM unfaelle_point A WHERE (${INTERSECTS} AND ") + actual.contains("OVERLAPS (TIMESTAMP '2019-01-01T00:00:00Z',TIMESTAMP '2019-12-31T23:59:59Z')") + } + + def 'the temporal extent of an instant is empty if there is no value'() { + + given: + def instant = unfaelleMapping.getColumnForPrimaryInstant().get() + + when: + String actual = generator.getTemporalExtentQuery(unfaelleMapping, instant.first(), instant.second()) + + then: 'no row without a value, so that a NULL can only stand for an unbounded start or end' + actual == "SELECT T.extent_start, T.extent_end FROM (SELECT MIN(A.unfzeit::timestamp(0)) AS extent_start, MAX(A.unfzeit::timestamp(0)) AS extent_end, COUNT(A.unfzeit::timestamp(0)) AS row_count FROM unfaelle_point A) T WHERE T.row_count > 0" + } + + def 'the temporal extent of an interval is unbounded at the start or end if any start or end is open'() { + + given: 'start and end in the same table' + def column = unfaelleMapping.getColumnForPrimaryInstant().get() + + when: + String actual = generator.getTemporalExtentQuery(unfaelleMapping, column.first(), column.second(), column.first(), column.second()) + + then: 'a NULL start or end is not ignored by MIN or MAX but makes that side unbounded' + actual == "SELECT T.extent_start, T.extent_end FROM (SELECT CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MIN(A.unfzeit::timestamp(0)) END AS extent_start, CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MAX(A.unfzeit::timestamp(0)) END AS extent_end, COUNT(*) AS row_count FROM unfaelle_point A) T WHERE T.row_count > 0" + } + + def 'the temporal extent of an interval with start and end in different tables'() { + + given: + def column = unfaelleMapping.getColumnForPrimaryInstant().get() + def endTable = new ImmutableSqlQuerySchema.Builder().from(column.first()).name("unfaelle_end").build() + + when: + String actual = generator.getTemporalExtentQuery(unfaelleMapping, column.first(), column.second(), endTable, column.second()) + + then: + actual == "SELECT S.extent_start, E.extent_end FROM (SELECT CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MIN(A.unfzeit::timestamp(0)) END AS extent_start, COUNT(*) AS row_count FROM unfaelle_point A) S, (SELECT CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MAX(A.unfzeit::timestamp(0)) END AS extent_end FROM unfaelle_point A) E WHERE S.row_count > 0" + } + + def 'the temporal extent of an interval with only a start or only an end is unbounded on the other side'() { + + given: + def column = unfaelleMapping.getColumnForPrimaryInstant().get() + + when: + String startOnly = generator.getTemporalExtentQuery(unfaelleMapping, column.first(), column.second(), true) + String endOnly = generator.getTemporalExtentQuery(unfaelleMapping, column.first(), column.second(), false) + + then: + startOnly == "SELECT T.extent_start, T.extent_end FROM (SELECT CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MIN(A.unfzeit::timestamp(0)) END AS extent_start, NULL AS extent_end, COUNT(*) AS row_count FROM unfaelle_point A) T WHERE T.row_count > 0" + endOnly == "SELECT T.extent_start, T.extent_end FROM (SELECT NULL AS extent_start, CASE WHEN COUNT(*) > COUNT(A.unfzeit::timestamp(0)) THEN NULL ELSE MAX(A.unfzeit::timestamp(0)) END AS extent_end, COUNT(*) AS row_count FROM unfaelle_point A) T WHERE T.row_count > 0" + } +} diff --git a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/FilterEncoderSqlSpec.groovy b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/FilterEncoderSqlSpec.groovy index 4c1478a27..af6ab8b43 100644 --- a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/FilterEncoderSqlSpec.groovy +++ b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/app/FilterEncoderSqlSpec.groovy @@ -28,7 +28,12 @@ import de.ii.xtraplatform.cql.domain.AEquals import de.ii.xtraplatform.cql.domain.AOverlaps import de.ii.xtraplatform.cql.domain.ArrayLiteral import de.ii.xtraplatform.crs.domain.CrsTransformerFactory +import de.ii.xtraplatform.crs.domain.BoundingBox import de.ii.xtraplatform.crs.domain.OgcCrs +import de.ii.xtraplatform.features.domain.ExtentFilters +import de.ii.xtraplatform.features.domain.FeatureSchema +import de.ii.xtraplatform.features.domain.ImmutableFeatureSchema +import de.ii.xtraplatform.features.domain.SchemaBase import de.ii.xtraplatform.crs.infra.CrsTransformerFactoryProj import de.ii.xtraplatform.features.sql.domain.SqlDialectGpkg import de.ii.xtraplatform.features.sql.domain.SqlDialectPgis @@ -37,6 +42,7 @@ import spock.lang.Shared import spock.lang.Specification import java.nio.file.Path +import java.time.Instant import java.util.concurrent.CompletableFuture class FilterEncoderSqlSpec extends Specification { @@ -368,6 +374,64 @@ class FilterEncoderSqlSpec extends Specification { actual == expected } + static FeatureSchema typeWithExtentProperties(Map properties) { + def builder = new ImmutableFeatureSchema.Builder().name("building").sourcePath("/building").type(SchemaBase.Type.OBJECT) + properties.each { name, role -> + builder.putProperties2(name, new ImmutableFeatureSchema.Builder() + .sourcePath(name) + .type(role == SchemaBase.Role.PRIMARY_GEOMETRY ? SchemaBase.Type.GEOMETRY : SchemaBase.Type.DATETIME) + .role(role)) + } + builder.build() + } + + def 'extent restriction, spatial'() { + + given: + def type = typeWithExtentProperties(["location": SchemaBase.Role.PRIMARY_GEOMETRY]) + def extent = BoundingBox.of(-118.0, 33.8, -117.9, 34.0, OgcCrs.CRS84) + + when: + def filters = ExtentFilters.of(type, Optional.of(extent), Optional.empty()) + String actual = filterEncoder.encode(filters[0], QuerySchemaFixtures.SIMPLE_GEOMETRY) + + then: 'intersects, not within, so features reaching beyond the extent are kept' + filters.size() == 1 + actual == "ST_Intersects(A.location, ST_GeomFromText('POLYGON((-118.0 33.8,-117.9 33.8,-117.9 34.0,-118.0 34.0,-118.0 33.8))',4326))" + } + + def 'extent restriction, temporal with open end'() { + + given: + def type = typeWithExtentProperties(["updated": SchemaBase.Role.PRIMARY_INSTANT]) + def extent = org.threeten.extra.Interval.of(Instant.parse("2017-06-10T00:00:00Z"), Instant.MAX) + + when: + def filters = ExtentFilters.of(type, Optional.empty(), Optional.of(extent)) + String actual = filterEncoder.encode(filters[0], QuerySchemaFixtures.SIMPLE_INTERVAL) + + then: 'the open end of the extent becomes infinity' + filters.size() == 1 + actual == "(A.updated::timestamp(0),A.updated::timestamp(0)) OVERLAPS (TIMESTAMP '2017-06-10T00:00:00Z',TIMESTAMP 'infinity')" + } + + def 'extent restriction, temporal on an interval'() { + + given: + def type = typeWithExtentProperties([ + "start": SchemaBase.Role.PRIMARY_INTERVAL_START, + "end" : SchemaBase.Role.PRIMARY_INTERVAL_END]) + def extent = org.threeten.extra.Interval.of(Instant.parse("2017-06-10T00:00:00Z"), Instant.parse("2017-06-12T00:00:00Z")) + + when: + def filters = ExtentFilters.of(type, Optional.empty(), Optional.of(extent)) + String actual = filterEncoder.encode(filters[0], QuerySchemaFixtures.SIMPLE_INTERVAL) + + then: + filters.size() == 1 + actual == "(COALESCE(A.start,TIMESTAMP '-infinity'), COALESCE(A.end,TIMESTAMP 'infinity')) OVERLAPS (TIMESTAMP '2017-06-10T00:00:00Z',TIMESTAMP '2017-06-12T00:00:00Z')" + } + def 'temporal operation, timestamp'() { given: diff --git a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollectorSpec.groovy b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollectorSpec.groovy new file mode 100644 index 000000000..423653951 --- /dev/null +++ b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/FeatureTokenStatsCollectorSpec.groovy @@ -0,0 +1,179 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.sql.domain + +import de.ii.xtraplatform.crs.domain.OgcCrs +import de.ii.xtraplatform.features.domain.FeatureSchema +import de.ii.xtraplatform.features.domain.FeatureTransactions +import de.ii.xtraplatform.features.domain.ImmutableMutationResult +import de.ii.xtraplatform.features.domain.SchemaBase.Role +import de.ii.xtraplatform.features.domain.Tuple +import spock.lang.Specification + +import java.time.Instant + +class FeatureTokenStatsCollectorSpec extends Specification { + + static final String JAN = "2024-01-01T00:00:00Z" + static final String MAR = "2024-03-01T00:00:00Z" + static final String APR = "2024-04-01T00:00:00Z" + + def "a feature without an end makes the end unbounded, whatever the other features have"() { + + given: 'two features written in one mutation, from separate sources' + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, false, JAN, null) + interval(stats, false, MAR, APR) + + expect: + temporal(stats) == Tuple.of(millis(JAN), null) + } + + def "a feature without a start makes the start unbounded"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, false, MAR, APR) + interval(stats, false, null, MAR) + + expect: + temporal(stats) == Tuple.of(null, millis(APR)) + } + + def "features without instants are ignored"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + stats.addInstant(MAR) + stats.addInstant(JAN) + + expect: + temporal(stats) == Tuple.of(millis(JAN), millis(MAR)) + } + + def "no temporal extent without temporal values"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + + expect: + temporal(stats) == null + } + + def "a type with only a start is unbounded at the end"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, false, JAN, null, true, false) + + expect: + temporal(stats) == Tuple.of(millis(JAN), null) + } + + def "a type with only an end is unbounded at the start"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, false, null, APR, false, true) + + expect: + temporal(stats) == Tuple.of(null, millis(APR)) + } + + def "the values set by the mutation replace those of the features"() { + + given: 'a new version: the mutation sets the start and leaves the end open' + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, false, null, null) + def overrides = new HashMap() + overrides.put(Role.PRIMARY_INTERVAL_START, MAR) + overrides.put(Role.PRIMARY_INTERVAL_END, null) + stats.applyRoleOverrides(overrides) + + expect: + temporal(stats) == Tuple.of(millis(MAR), null) + } + + def "a partial update without start or end leaves the extent as it is"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, true, null, null) + + expect: + temporal(stats) == null + } + + def "a partial update with an end only uses it for both sides"() { + + given: 'the start is unchanged and not after the end' + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, true, null, APR) + + expect: + temporal(stats) == Tuple.of(millis(APR), millis(APR)) + } + + def "a partial update that clears the end makes it unbounded"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, true, MAR, FeatureTransactions.PATCH_NULL_VALUE) + + expect: + temporal(stats) == Tuple.of(millis(MAR), null) + } + + def "a partial update that only clears the end makes it unbounded without changing the start"() { + + given: 'the start is unchanged and unknown' + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, true, null, FeatureTransactions.PATCH_NULL_VALUE) + + expect: 'the latest possible start, which never widens an extent' + temporal(stats) == Tuple.of(Long.MAX_VALUE, null) + } + + def "a partial update that only clears the start makes it unbounded without changing the end"() { + + given: + def stats = new FeatureTokenStatsCollector.Stats(OgcCrs.CRS84) + interval(stats, true, FeatureTransactions.PATCH_NULL_VALUE, null) + + expect: + temporal(stats) == Tuple.of(null, Long.MIN_VALUE) + } + + // one feature as the collector passes it on at the end of the feature + static void interval(FeatureTokenStatsCollector.Stats stats, boolean partial, String start, String end, + boolean hasStart = true, boolean hasEnd = true) { + def collector = new FeatureTokenStatsCollector(stats, partial) + collector.featureStart = start + collector.featureEnd = end + collector.addInterval(mapping(hasStart, hasEnd)) + } + + static SqlQueryMapping mapping(boolean hasStart, boolean hasEnd) { + def schema = new de.ii.xtraplatform.features.domain.ImmutableFeatureSchema.Builder() + .name("t").type(de.ii.xtraplatform.features.domain.SchemaBase.Type.DATETIME).build() + return [getSchemaForRole: { Role role -> + (role == Role.PRIMARY_INTERVAL_START && hasStart) || (role == Role.PRIMARY_INTERVAL_END && hasEnd) + ? Optional.of(schema) : Optional.empty() + }] as SqlQueryMapping + } + + static Tuple temporal(FeatureTokenStatsCollector.Stats stats) { + def builder = ImmutableMutationResult.builder().type(FeatureTransactions.MutationResult.Type.CREATE).hasFeatures(false) + stats.writeTo(builder) + builder.build().getTemporalExtent().orElse(null) + } + + static Long millis(String instant) { + Instant.parse(instant).toEpochMilli() + } +} diff --git a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/SqlDialectPostGisSpec.groovy b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/SqlDialectPostGisSpec.groovy index fba35b54c..639d8afdf 100644 --- a/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/SqlDialectPostGisSpec.groovy +++ b/xtraplatform-features-sql/src/test/groovy/de/ii/xtraplatform/features/sql/domain/SqlDialectPostGisSpec.groovy @@ -41,7 +41,8 @@ class SqlDialectPostGisSpec extends Specification { when: Optional interval = sqlDialectPostGis.parseTemporalExtent(null, end, ZoneId.of("UTC")) then: - interval.isEmpty() + interval.get().isUnboundedStart() + interval.get().getEnd().toString() == "2015-12-21T22:17:56Z" } def 'Temporal extent end is null'() { @@ -57,7 +58,8 @@ class SqlDialectPostGisSpec extends Specification { when: Optional interval = sqlDialectPostGis.parseTemporalExtent(null, null, ZoneId.of("UTC")) then: - interval.isEmpty() + interval.get().isUnboundedStart() + interval.get().isUnboundedEnd() } def 'Temporal extent start > end'() { diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/AbstractFeatureProvider.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/AbstractFeatureProvider.java index cd91f12fb..a0f1a8834 100644 --- a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/AbstractFeatureProvider.java +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/AbstractFeatureProvider.java @@ -19,6 +19,7 @@ import de.ii.xtraplatform.codelists.domain.Codelist; import de.ii.xtraplatform.crs.domain.BoundingBox; import de.ii.xtraplatform.crs.domain.CrsInfo; +import de.ii.xtraplatform.crs.domain.CrsTransformationException; import de.ii.xtraplatform.crs.domain.CrsTransformerFactory; import de.ii.xtraplatform.crs.domain.EpsgCrs; import de.ii.xtraplatform.crs.domain.OgcCrs; @@ -64,6 +65,7 @@ import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.threeten.extra.Interval; @SuppressWarnings({ "PMD.CouplingBetweenObjects", @@ -642,6 +644,36 @@ protected Query preprocessQuery(Query query) { return query; } + protected Optional getConfiguredSpatialExtent(String typeName) { + return getConfiguredExtent(typeName) + .getSpatial() + .flatMap(extent -> extent.toBoundingBox(getData().getNativeCrs().orElse(OgcCrs.CRS84))); + } + + protected Optional getConfiguredTemporalExtent(String typeName) { + return getConfiguredExtent(typeName).getTemporal().flatMap(TemporalExtent::toInterval); + } + + private Extent getConfiguredExtent(String typeName) { + return Extent.inherit( + Optional.ofNullable(getData().getTypes().get(typeName)).flatMap(FeatureSchema::getExtent), + getData().getExtent()); + } + + protected Optional transformSpatialExtent( + BoundingBox boundingBox, EpsgCrs targetCrs) { + return crsTransformerFactory + .getTransformer(getData().getNativeCrs().orElse(OgcCrs.CRS84), targetCrs, false) + .flatMap( + crsTransformer -> { + try { + return Optional.of(crsTransformer.transformBoundingBox(boundingBox)); + } catch (CrsTransformationException e) { + return Optional.empty(); + } + }); + } + @Override public FeatureChanges changes() { return changeHandler; diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/Extent.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/Extent.java new file mode 100644 index 000000000..8255acec8 --- /dev/null +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/Extent.java @@ -0,0 +1,43 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.google.common.base.Preconditions; +import java.util.Optional; +import org.immutables.value.Value; + +/** Extent object for spatial and temporal extents. */ +@Value.Immutable +@JsonDeserialize(builder = ImmutableExtent.Builder.class) +public interface Extent { + Optional getSpatial(); + + Optional getTemporal(); + + /** + * Each part of the extent replaces the same part of the extent of the parent level (provider for + * types, API for collections, tileset defaults for tilesets); a part with {@code computed: true} + * therefore discards an inherited fixed extent. + */ + static Extent inherit(Optional extent, Optional parentExtent) { + return ImmutableExtent.builder() + .spatial( + extent.flatMap(Extent::getSpatial).or(() -> parentExtent.flatMap(Extent::getSpatial))) + .temporal( + extent.flatMap(Extent::getTemporal).or(() -> parentExtent.flatMap(Extent::getTemporal))) + .build(); + } + + /** Tiles have only a spatial extent, a temporal extent is rejected. */ + static void checkSpatialOnly(Optional extent) { + Preconditions.checkState( + extent.flatMap(Extent::getTemporal).isEmpty(), + "Extent: 'temporal' is not supported for tiles, only 'spatial'."); + } +} diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/ExtentFilters.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/ExtentFilters.java new file mode 100644 index 000000000..090b899db --- /dev/null +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/ExtentFilters.java @@ -0,0 +1,109 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain; + +import de.ii.xtraplatform.cql.domain.Bbox; +import de.ii.xtraplatform.cql.domain.Cql2Expression; +import de.ii.xtraplatform.cql.domain.Interval; +import de.ii.xtraplatform.cql.domain.Operand; +import de.ii.xtraplatform.cql.domain.Property; +import de.ii.xtraplatform.cql.domain.SIntersects; +import de.ii.xtraplatform.cql.domain.SpatialLiteral; +import de.ii.xtraplatform.cql.domain.TIntersects; +import de.ii.xtraplatform.cql.domain.Temporal; +import de.ii.xtraplatform.cql.domain.TemporalLiteral; +import de.ii.xtraplatform.crs.domain.BoundingBox; +import de.ii.xtraplatform.features.domain.MultiFeatureQuery.SubQuery; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +/** + * Filters that restrict a query to a fixed extent. Intersects is used instead of contains so that + * features and tiles select the same features, even if some of them reach beyond the extent. + */ +public final class ExtentFilters { + + private ExtentFilters() {} + + public static List of( + FeatureSchema type, + Optional spatialExtent, + Optional temporalExtent) { + List filters = new ArrayList<>(); + + spatialExtent + .flatMap(extent -> type.getFilterGeometry().map(geometry -> spatial(geometry, extent))) + .ifPresent(filters::add); + + temporalExtent + .flatMap(extent -> primaryTemporal(type).map(temporal -> temporal(temporal, extent))) + .ifPresent(filters::add); + + return filters; + } + + /** + * Adds the extent filters of each queried type to a query. Queries for a single feature are not + * restricted, so every feature stays reachable by its id. + */ + public static Query restrict(Query query, Function> filtersForType) { + if (query instanceof FeatureQuery featureQuery && !featureQuery.returnsSingleFeature()) { + return ImmutableFeatureQuery.builder() + .from(featureQuery) + .addAllFilters(filtersForType.apply(featureQuery.getType())) + .build(); + } + if (query instanceof MultiFeatureQuery multiQuery) { + return ImmutableMultiFeatureQuery.builder() + .from(multiQuery) + .queries( + multiQuery.getQueries().stream() + .map(subQuery -> restrictSubQuery(subQuery, filtersForType)) + .toList()) + .build(); + } + return query; + } + + private static SubQuery restrictSubQuery( + SubQuery subQuery, Function> filtersForType) { + return ImmutableSubQuery.builder() + .from(subQuery) + .addAllFilters(filtersForType.apply(subQuery.getType())) + .build(); + } + + private static Cql2Expression spatial(FeatureSchema geometry, BoundingBox extent) { + return SIntersects.of( + Property.of(geometry.getFullPathAsString()), SpatialLiteral.of(Bbox.of(extent))); + } + + private static Cql2Expression temporal(Temporal property, org.threeten.extra.Interval extent) { + return TIntersects.of(property, new TemporalLiteral.Builder(extent).build()); + } + + private static Optional primaryTemporal(FeatureSchema type) { + return type.getPrimaryInstant() + .map(instant -> Property.of(instant.getFullPathAsString())) + .or( + () -> + type.getPrimaryInterval() + .map( + interval -> + Interval.of( + List.of(boundOf(interval.first()), boundOf(interval.second()))))); + } + + private static Operand boundOf(FeatureSchema property) { + return property == null + ? TemporalLiteral.of("..") + : Property.of(property.getFullPathAsString()); + } +} diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureProviderDataV2.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureProviderDataV2.java index cee2fd2a1..0734ac391 100644 --- a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureProviderDataV2.java +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureProviderDataV2.java @@ -261,6 +261,19 @@ default List getCql2Functions() { @Override Optional getAuto(); + /** + * @langEn Optional spatial (in the native CRS) and temporal extent for all types in this + * provider. If set, the extent is not computed from the data. SQL providers also restrict + * queries for multiple features to the extent, see `queryGeneration`; other providers do not + * restrict queries. + * @langDe Optionaler räumlicher (im nativen Koordinatenreferenzsystem) und zeitlicher Extent für + * alle Types dieses Providers. Wenn gesetzt, wird der Extent nicht aus den Daten berechnet. + * SQL-Provider schränken zudem Abfragen mehrerer Features auf den Extent ein, siehe + * `queryGeneration`; andere Provider schränken Abfragen nicht ein. + * @since v4.9 + */ + Optional getExtent(); + // custom builder to automatically use keys of types as name of FeatureTypeV2 abstract class Builder> implements EntityDataBuilder { diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureSchema.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureSchema.java index 309957177..d2fd6f679 100644 --- a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureSchema.java +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/FeatureSchema.java @@ -401,6 +401,18 @@ default Type getType() { */ Optional getDescription(); + /** + * @langEn Optional spatial (in the native CRS) and temporal extent for this feature type. Each + * part replaces the same part of the extent of the provider. SQL providers also restrict + * queries for multiple features to the extent, see `queryGeneration`. + * @langDe Optionaler räumlicher (im nativen Koordinatenreferenzsystem) und zeitlicher Extent für + * diesen Feature-Type. Jeder Teil ersetzt denselben Teil des Extents des Providers. + * SQL-Provider schränken zudem Abfragen mehrerer Features auf den Extent ein, siehe + * `queryGeneration`. + * @since v4.9 + */ + Optional getExtent(); + /** * @langEn The unit of measurement of the value, only relevant for numeric properties. * @langDe Die Maßeinheit des Wertes, nur relevant bei numerischen Eigenschaften. diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/SpatialExtent.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/SpatialExtent.java new file mode 100644 index 000000000..085339c53 --- /dev/null +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/SpatialExtent.java @@ -0,0 +1,139 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.google.common.base.Preconditions; +import de.ii.xtraplatform.crs.domain.BoundingBox; +import de.ii.xtraplatform.crs.domain.EpsgCrs; +import java.util.Optional; +import javax.annotation.Nullable; +import org.immutables.value.Value; + +/** + * Spatial extent. The coordinates are in the native CRS of the feature provider for providers and + * types, and in CRS84 (longitude/latitude) for tilesets and APIs. + */ +@Value.Immutable +@JsonDeserialize(builder = ImmutableSpatialExtent.Builder.class) +public interface SpatialExtent { + + /** + * @langEn Minimum x coordinate. + * @langDe Minimale x-Koordinate. + * @since v4.9 + */ + @Nullable + Double getXmin(); + + /** + * @langEn Minimum y coordinate. + * @langDe Minimale y-Koordinate. + * @since v4.9 + */ + @Nullable + Double getYmin(); + + /** + * @langEn Minimum z coordinate, optional. + * @langDe Minimale z-Koordinate, optional. + * @since v4.9 + */ + @Nullable + Double getZmin(); + + /** + * @langEn Maximum x coordinate. + * @langDe Maximale x-Koordinate. + * @since v4.9 + */ + @Nullable + Double getXmax(); + + /** + * @langEn Maximum y coordinate. + * @langDe Maximale y-Koordinate. + * @since v4.9 + */ + @Nullable + Double getYmax(); + + /** + * @langEn Maximum z coordinate, optional. + * @langDe Maximale z-Koordinate, optional. + * @since v4.9 + */ + @Nullable + Double getZmax(); + + /** + * @langEn Set to `true` to compute the extent from the data instead of inheriting a fixed extent + * from the parent level (provider for types, tileset defaults for tilesets). Without a fixed + * extent on the parent level, this is the same as omitting the extent. Either the coordinates + * or `computed: true` must be set; `false` is not allowed. + * @langDe Auf `true` setzen, um den Extent aus den Daten zu berechnen, statt einen festen Extent + * der übergeordneten Ebene (Provider für Types, Tileset-Defaults für Tilesets) zu erben. Ohne + * festen Extent auf der übergeordneten Ebene ist das dasselbe, wie den Extent wegzulassen. + * Anzugeben sind entweder die Koordinaten oder `computed: true`; `false` ist nicht erlaubt. + * @since v4.9 + */ + @Nullable + Boolean getComputed(); + + @Value.Check + default void checkExclusiveComputed() { + boolean hasCoordinates = hasCoordinates(); + boolean autoCompute = Boolean.TRUE.equals(getComputed()); + + Preconditions.checkState( + !Boolean.FALSE.equals(getComputed()), + "SpatialExtent: 'computed' may only be true, omit it for a fixed extent."); + Preconditions.checkState( + !(hasCoordinates && autoCompute), + "SpatialExtent: 'computed' and explicit coordinates must not be set at the same time."); + Preconditions.checkState( + hasCoordinates || autoCompute, "SpatialExtent: set the coordinates or 'computed: true'."); + + if (hasCoordinates) { + checkCoordinatesComplete(); + } + } + + private boolean hasCoordinates() { + return getXmin() != null + || getYmin() != null + || getZmin() != null + || getXmax() != null + || getYmax() != null + || getZmax() != null; + } + + private void checkCoordinatesComplete() { + Preconditions.checkState( + getXmin() != null && getYmin() != null && getXmax() != null && getYmax() != null, + "SpatialExtent: xmin, ymin, xmax, ymax are required when coordinates are used."); + + Preconditions.checkState( + (getZmin() == null && getZmax() == null) || (getZmin() != null && getZmax() != null), + "SpatialExtent: zmin and zmax must both be set or both be absent."); + } + + default Optional toBoundingBox(EpsgCrs nativeCrs) { + if (getXmin() == null || getYmin() == null || getXmax() == null || getYmax() == null) { + return Optional.empty(); + } + + if (getZmin() != null && getZmax() != null) { + return Optional.of( + BoundingBox.of( + getXmin(), getYmin(), getZmin(), getXmax(), getYmax(), getZmax(), nativeCrs)); + } + + return Optional.of(BoundingBox.of(getXmin(), getYmin(), getXmax(), getYmax(), nativeCrs)); + } +} diff --git a/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/TemporalExtent.java b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/TemporalExtent.java new file mode 100644 index 000000000..a25a6c810 --- /dev/null +++ b/xtraplatform-features/src/main/java/de/ii/xtraplatform/features/domain/TemporalExtent.java @@ -0,0 +1,123 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.google.common.base.Preconditions; +import java.time.DateTimeException; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeParseException; +import java.util.Optional; +import javax.annotation.Nullable; +import org.immutables.value.Value; +import org.threeten.extra.Interval; + +/** + * Temporal extent. Bounds are either dates (yyyy-MM-dd, interpreted in UTC) or date-times with + * offset (yyyy-MM-ddTHH:mm:ss[.SSS]Z or ±HH:mm). A missing bound is open. + */ +@Value.Immutable +@JsonDeserialize(builder = ImmutableTemporalExtent.Builder.class) +public interface TemporalExtent { + + /** + * @langEn Start of the extent, either a date (`yyyy-MM-dd`, UTC) or a date-time with offset. Open + * if not set. + * @langDe Beginn des Extents, entweder ein Datum (`yyyy-MM-dd`, UTC) oder ein Zeitpunkt mit + * Offset. Offen, wenn nicht gesetzt. + * @since v4.9 + */ + @Nullable + String getStart(); + + /** + * @langEn End of the extent, either a date (`yyyy-MM-dd`, inclusive, UTC) or a date-time with + * offset. Open if not set. + * @langDe Ende des Extents, entweder ein Datum (`yyyy-MM-dd`, inklusive, UTC) oder ein Zeitpunkt + * mit Offset. Offen, wenn nicht gesetzt. + * @since v4.9 + */ + @Nullable + String getEnd(); + + /** + * @langEn Set to `true` to compute the extent from the data instead of inheriting a fixed extent + * from the parent level (provider for types). Without a fixed extent on the parent level, + * this is the same as omitting the extent. Either `start`, `end` or both, or `computed: true` + * must be set; `false` is not allowed. + * @langDe Auf `true` setzen, um den Extent aus den Daten zu berechnen, statt einen festen Extent + * der übergeordneten Ebene (Provider für Types) zu erben. Ohne festen Extent auf der + * übergeordneten Ebene ist das dasselbe, wie den Extent wegzulassen. Anzugeben sind entweder + * `start`, `end` oder beide oder `computed: true`; `false` ist nicht erlaubt. + * @since v4.9 + */ + @Nullable + Boolean getComputed(); + + @JsonIgnore + default Optional toInterval() { + if (getStart() == null && getEnd() == null) { + return Optional.empty(); + } + + Instant start = getStart() == null ? Instant.MIN : parseStart(getStart()); + Instant end = getEnd() == null ? Instant.MAX : parseEnd(getEnd()); + + return Optional.of(Interval.of(start, end)); + } + + @Value.Check + default void check() { + boolean hasBounds = getStart() != null || getEnd() != null; + + Preconditions.checkState( + !Boolean.FALSE.equals(getComputed()), + "TemporalExtent: 'computed' may only be true, omit it for a fixed extent."); + + Preconditions.checkState( + !(hasBounds && Boolean.TRUE.equals(getComputed())), + "TemporalExtent: 'computed' and explicit start/end must not be set at the same time."); + + Preconditions.checkState( + hasBounds || Boolean.TRUE.equals(getComputed()), + "TemporalExtent: set 'start', 'end' or 'computed: true'."); + + try { + toInterval(); + } catch (DateTimeParseException e) { + throw new IllegalStateException( + String.format( + "TemporalExtent: '%s' is neither a date (yyyy-MM-dd) nor a date-time with offset.", + e.getParsedString()), + e); + } catch (DateTimeException e) { + throw new IllegalStateException("TemporalExtent: start must not be after end.", e); + } + } + + private static Instant parseStart(String value) { + return isDate(value) + ? LocalDate.parse(value).atStartOfDay().toInstant(ZoneOffset.UTC) + : OffsetDateTime.parse(value).toInstant(); + } + + private static Instant parseEnd(String value) { + return isDate(value) + ? LocalDate.parse(value).atTime(LocalTime.of(23, 59, 59)).toInstant(ZoneOffset.UTC) + : OffsetDateTime.parse(value).toInstant(); + } + + private static boolean isDate(String value) { + return !value.contains("T"); + } +} diff --git a/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentFiltersSpec.groovy b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentFiltersSpec.groovy new file mode 100644 index 000000000..d9851db74 --- /dev/null +++ b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentFiltersSpec.groovy @@ -0,0 +1,121 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain + +import de.ii.xtraplatform.cql.domain.Bbox +import de.ii.xtraplatform.cql.domain.Cql2Expression +import de.ii.xtraplatform.cql.domain.Interval +import de.ii.xtraplatform.cql.domain.Property +import de.ii.xtraplatform.cql.domain.SIntersects +import de.ii.xtraplatform.cql.domain.SpatialLiteral +import de.ii.xtraplatform.cql.domain.TIntersects +import de.ii.xtraplatform.cql.domain.TemporalLiteral +import de.ii.xtraplatform.crs.domain.BoundingBox +import de.ii.xtraplatform.crs.domain.EpsgCrs +import spock.lang.Specification + +import java.time.Instant + +class ExtentFiltersSpec extends Specification { + + static BoundingBox BBOX = BoundingBox.of(300000, 5600000, 400000, 5700000, EpsgCrs.of(25832)) + static org.threeten.extra.Interval INTERVAL = + org.threeten.extra.Interval.of(Instant.parse("2020-01-01T00:00:00Z"), Instant.MAX) + + static FeatureSchema type(Map temporalRoles) { + def builder = new ImmutableFeatureSchema.Builder() + .name("t").sourcePath("/t").type(SchemaBase.Type.OBJECT) + .putProperties2("geom", new ImmutableFeatureSchema.Builder() + .sourcePath("geom").type(SchemaBase.Type.GEOMETRY) + .role(SchemaBase.Role.PRIMARY_GEOMETRY)) + temporalRoles.each { name, role -> + builder.putProperties2(name, new ImmutableFeatureSchema.Builder() + .sourcePath(name).type(SchemaBase.Type.DATETIME).role(role)) + } + builder.build() + } + + def "spatial and instant extents become intersects filters"() { + when: + def filters = ExtentFilters.of(type(["date": SchemaBase.Role.PRIMARY_INSTANT]), Optional.of(BBOX), Optional.of(INTERVAL)) + + then: + filters == [ + SIntersects.of(Property.of("geom"), SpatialLiteral.of(Bbox.of(BBOX))), + TIntersects.of(Property.of("date"), new TemporalLiteral.Builder(INTERVAL).build()) + ] + } + + def "an interval extent uses both interval properties"() { + when: + def filters = ExtentFilters.of( + type(["begin": SchemaBase.Role.PRIMARY_INTERVAL_START, "end": SchemaBase.Role.PRIMARY_INTERVAL_END]), + Optional.empty(), Optional.of(INTERVAL)) + + then: + filters == [ + TIntersects.of( + Interval.of([Property.of("begin"), Property.of("end")]), + new TemporalLiteral.Builder(INTERVAL).build()) + ] + } + + def "no extent or no matching property means no filter"() { + expect: + ExtentFilters.of(type([:]), Optional.empty(), Optional.empty()).isEmpty() + ExtentFilters.of(type([:]), Optional.empty(), Optional.of(INTERVAL)).isEmpty() + } + + static Cql2Expression FILTER = SIntersects.of(Property.of("geom"), SpatialLiteral.of(Bbox.of(BBOX))) + + def "queries for multiple features are restricted"() { + given: + def query = ImmutableFeatureQuery.builder().type("t").build() + + when: + def restricted = ExtentFilters.restrict(query, { type -> type == "t" ? [FILTER] : [] }) as FeatureQuery + + then: + restricted.filters == [FILTER] + } + + def "existing filters are kept"() { + given: + def existing = SIntersects.of(Property.of("other"), SpatialLiteral.of(Bbox.of(0, 0, 1, 1))) + def query = ImmutableFeatureQuery.builder().type("t").addFilters(existing).build() + + when: + def restricted = ExtentFilters.restrict(query, { [FILTER] }) as FeatureQuery + + then: + restricted.filters == [existing, FILTER] + } + + def "queries for a single feature are not restricted"() { + given: + def query = ImmutableFeatureQuery.builder().type("t").returnsSingleFeature(true).build() + + expect: + ExtentFilters.restrict(query, { [FILTER] }).is(query) + } + + def "each sub query of a multi query gets the filters of its type"() { + given: + def other = TIntersects.of(Property.of("date"), new TemporalLiteral.Builder(INTERVAL).build()) + def query = ImmutableMultiFeatureQuery.builder() + .addQueries(ImmutableSubQuery.builder().type("a").collectionId("a").build()) + .addQueries(ImmutableSubQuery.builder().type("b").collectionId("b").build()) + .build() + + when: + def restricted = ExtentFilters.restrict(query, { type -> type == "a" ? [FILTER] : [other] }) as MultiFeatureQuery + + then: + restricted.queries*.filters == [[FILTER], [other]] + } +} diff --git a/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentSpec.groovy b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentSpec.groovy new file mode 100644 index 000000000..9de12bc10 --- /dev/null +++ b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/ExtentSpec.groovy @@ -0,0 +1,60 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain + +import spock.lang.Specification + +class ExtentSpec extends Specification { + + static SpatialExtent FIXED_SPATIAL = new ImmutableSpatialExtent.Builder().xmin(0).ymin(0).xmax(1).ymax(1).build() + static SpatialExtent OTHER_SPATIAL = new ImmutableSpatialExtent.Builder().xmin(5).ymin(5).xmax(6).ymax(6).build() + static SpatialExtent COMPUTED_SPATIAL = new ImmutableSpatialExtent.Builder().computed(true).build() + static TemporalExtent FIXED_TEMPORAL = new ImmutableTemporalExtent.Builder().start("2020-01-01").build() + + static Optional extent(SpatialExtent spatial, TemporalExtent temporal) { + Optional.of(ImmutableExtent.builder() + .spatial(Optional.ofNullable(spatial)) + .temporal(Optional.ofNullable(temporal)) + .build()) + } + + def "a type without extent inherits the provider extent"() { + when: + def resolved = Extent.inherit(Optional.empty(), extent(FIXED_SPATIAL, FIXED_TEMPORAL)) + + then: + resolved.spatial == Optional.of(FIXED_SPATIAL) + resolved.temporal == Optional.of(FIXED_TEMPORAL) + } + + def "the type extent replaces the provider extent per part"() { + when: + def resolved = Extent.inherit(extent(OTHER_SPATIAL, null), extent(FIXED_SPATIAL, FIXED_TEMPORAL)) + + then: + resolved.spatial == Optional.of(OTHER_SPATIAL) + resolved.temporal == Optional.of(FIXED_TEMPORAL) + } + + def "computed on the type discards the inherited fixed extent"() { + when: + def resolved = Extent.inherit(extent(COMPUTED_SPATIAL, null), extent(FIXED_SPATIAL, null)) + + then: + resolved.spatial.flatMap { it.toBoundingBox(de.ii.xtraplatform.crs.domain.OgcCrs.CRS84) }.isEmpty() + } + + def "nothing configured means nothing fixed"() { + when: + def resolved = Extent.inherit(Optional.empty(), Optional.empty()) + + then: + resolved.spatial.isEmpty() + resolved.temporal.isEmpty() + } +} diff --git a/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/SpatialExtentSpec.groovy b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/SpatialExtentSpec.groovy new file mode 100644 index 000000000..3f18decd5 --- /dev/null +++ b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/SpatialExtentSpec.groovy @@ -0,0 +1,71 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain + +import de.ii.xtraplatform.crs.domain.BoundingBox +import de.ii.xtraplatform.crs.domain.EpsgCrs +import de.ii.xtraplatform.crs.domain.OgcCrs +import spock.lang.Specification + +class SpatialExtentSpec extends Specification { + + def "computed extent has no bounding box"() { + expect: + new ImmutableSpatialExtent.Builder().computed(true).build().toBoundingBox(OgcCrs.CRS84).isEmpty() + } + + def "computed may only be true"() { + when: + new ImmutableSpatialExtent.Builder().computed(false).build() + + then: + thrown(IllegalStateException) + } + + def "an extent without coordinates and computed is rejected"() { + when: 'an empty object, which would otherwise silently stop the inheritance' + new ImmutableSpatialExtent.Builder().build() + + then: + def e = thrown(IllegalStateException) + e.message == "SpatialExtent: set the coordinates or 'computed: true'." + } + + def "computed must not be combined with coordinates"() { + when: + new ImmutableSpatialExtent.Builder().xmin(0).ymin(0).xmax(1).ymax(1).computed(true).build() + + then: + thrown(IllegalStateException) + } + + def "coordinates become a bounding box in the given CRS"() { + given: + def crs = EpsgCrs.of(25832) + + expect: + new ImmutableSpatialExtent.Builder().xmin(1).ymin(2).xmax(3).ymax(4).build().toBoundingBox(crs) == + Optional.of(BoundingBox.of(1, 2, 3, 4, crs)) + new ImmutableSpatialExtent.Builder().xmin(1).ymin(2).zmin(0).xmax(3).ymax(4).zmax(10).build().toBoundingBox(crs) == + Optional.of(BoundingBox.of(1, 2, 0, 3, 4, 10, crs)) + } + + def "incomplete coordinates are rejected"() { + when: + new ImmutableSpatialExtent.Builder().xmin(1).ymin(2).xmax(3).build() + + then: + thrown(IllegalStateException) + + when: + new ImmutableSpatialExtent.Builder().xmin(1).ymin(2).xmax(3).ymax(4).zmin(0).build() + + then: + thrown(IllegalStateException) + } +} diff --git a/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/TemporalExtentSpec.groovy b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/TemporalExtentSpec.groovy new file mode 100644 index 000000000..7fc2c1b67 --- /dev/null +++ b/xtraplatform-features/src/test/groovy/de/ii/xtraplatform/features/domain/TemporalExtentSpec.groovy @@ -0,0 +1,80 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.features.domain + +import org.threeten.extra.Interval +import spock.lang.Specification + +import java.time.Instant + +class TemporalExtentSpec extends Specification { + + static TemporalExtent extent(String start, String end) { + new ImmutableTemporalExtent.Builder().start(start).end(end).build() + } + + def "bounds are parsed as #description"() { + expect: + extent(start, end).toInterval() == Optional.of(Interval.of(Instant.parse(expectedStart), Instant.parse(expectedEnd))) + + where: + description | start | end || expectedStart | expectedEnd + "dates" | "2020-01-01" | "2020-12-31" || "2020-01-01T00:00:00Z" | "2020-12-31T23:59:59Z" + "UTC date-times" | "2020-01-01T10:00:00Z" | "2020-01-02T10:00:00.5Z" || "2020-01-01T10:00:00Z" | "2020-01-02T10:00:00.5Z" + "offset date-times" | "2020-01-01T10:00:00+01:00" | "2020-01-01T12:00:00+01:00" || "2020-01-01T09:00:00Z" | "2020-01-01T11:00:00Z" + } + + def "missing bounds are open"() { + expect: + extent(null, "2020-12-31").toInterval().get().isUnboundedStart() + extent("2020-01-01", null).toInterval().get().isUnboundedEnd() + } + + def "an extent without start, end and computed is rejected"() { + when: 'an empty object, which would otherwise silently stop the inheritance' + extent(null, null) + + then: + def e = thrown(IllegalStateException) + e.message == "TemporalExtent: set 'start', 'end' or 'computed: true'." + } + + def "invalid extent '#start' / '#end' is rejected"() { + when: + extent(start, end) + + then: + thrown(IllegalStateException) + + where: + start | end + "1577836800" | null + "2020-01-01T10:00:00" | null + "2020-13-01" | null + "2021-01-01" | "2020-01-01" + } + + def "computed must not be combined with bounds"() { + when: + new ImmutableTemporalExtent.Builder().start("2020-01-01").computed(true).build() + + then: + thrown(IllegalStateException) + } + + def "computed may only be true"() { + expect: + new ImmutableTemporalExtent.Builder().computed(true).build().toInterval().isEmpty() + + when: + new ImmutableTemporalExtent.Builder().computed(false).build() + + then: + thrown(IllegalStateException) + } +} diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderFeatures.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderFeatures.java index 74ffa36e5..de7249481 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderFeatures.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderFeatures.java @@ -26,11 +26,13 @@ import de.ii.xtraplatform.crs.domain.BoundingBox; import de.ii.xtraplatform.crs.domain.CrsInfo; import de.ii.xtraplatform.crs.domain.CrsTransformerFactory; +import de.ii.xtraplatform.crs.domain.OgcCrs; import de.ii.xtraplatform.entities.domain.Entity; import de.ii.xtraplatform.entities.domain.Entity.SubType; import de.ii.xtraplatform.entities.domain.EntityRegistry; import de.ii.xtraplatform.features.domain.DatasetChange; import de.ii.xtraplatform.features.domain.DatasetChangeListener; +import de.ii.xtraplatform.features.domain.Extent; import de.ii.xtraplatform.features.domain.FeatureChange; import de.ii.xtraplatform.features.domain.FeatureChangeListener; import de.ii.xtraplatform.features.domain.FeatureProvider; @@ -1042,20 +1044,31 @@ private TilesetMetadata loadMetadata(TilesetFeatures tileset) { getLayers(tileset).stream() .map(id -> tileGenerator.getVectorSchema(id, FeatureEncoderMVT.FORMAT)) .collect(Collectors.toList()); - Optional bounds = - getLayers(tileset).stream() - .map(tileGenerator::getBounds) - .reduce( - Optional.empty(), - (a, b) -> { - if (b.isEmpty()) { - return a; - } - if (a.isPresent()) { - return Optional.of(BoundingBox.merge(b.get(), a.get())); - } - return b; - }); + + Optional configuredExtent = + Extent.inherit(tileset.getExtent(), getData().getTilesetDefaults().getExtent()) + .getSpatial() + .flatMap(extent -> extent.toBoundingBox(OgcCrs.CRS84)); + + Optional bounds; + if (configuredExtent.isPresent()) { + bounds = configuredExtent; + } else { + bounds = + getLayers(tileset).stream() + .map(tileGenerator::getBounds) + .reduce( + Optional.empty(), + (a, b) -> { + if (b.isEmpty()) { + return a; + } + if (a.isPresent()) { + return Optional.of(BoundingBox.merge(b.get(), a.get())); + } + return b; + }); + } return ImmutableTilesetMetadata.builder() .addEncodings(TilesFormat.MVT) diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderHttp.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderHttp.java index 2a378487d..fbcb9917f 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderHttp.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderHttp.java @@ -11,11 +11,19 @@ import dagger.assisted.Assisted; import dagger.assisted.AssistedInject; import de.ii.xtraplatform.base.domain.resiliency.VolatileRegistry; +import de.ii.xtraplatform.crs.domain.BoundingBox; +import de.ii.xtraplatform.crs.domain.CrsTransformationException; +import de.ii.xtraplatform.crs.domain.CrsTransformerFactory; +import de.ii.xtraplatform.crs.domain.OgcCrs; import de.ii.xtraplatform.entities.domain.Entity; import de.ii.xtraplatform.entities.domain.Entity.SubType; +import de.ii.xtraplatform.features.domain.Extent; import de.ii.xtraplatform.features.domain.ProviderData; import de.ii.xtraplatform.tiles.domain.ChainedTileProvider; import de.ii.xtraplatform.tiles.domain.ImmutableTilesetMetadata; +import de.ii.xtraplatform.tiles.domain.MinMax; +import de.ii.xtraplatform.tiles.domain.TileMatrixSet; +import de.ii.xtraplatform.tiles.domain.TileMatrixSetRepository; import de.ii.xtraplatform.tiles.domain.TileProviderData; import de.ii.xtraplatform.tiles.domain.TileProviderHttpData; import de.ii.xtraplatform.tiles.domain.TileQuery; @@ -29,6 +37,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,11 +56,19 @@ public class TileProviderHttp extends AbstractTileProvider private static final Logger LOGGER = LoggerFactory.getLogger(TileProviderHttp.class); private final ChainedTileProvider providerChain; private final Map metadata; + private final TileMatrixSetRepository tileMatrixSetRepository; + private final CrsTransformerFactory crsTransformerFactory; @AssistedInject - public TileProviderHttp(VolatileRegistry volatileRegistry, @Assisted TileProviderHttpData data) { + public TileProviderHttp( + TileMatrixSetRepository tileMatrixSetRepository, + CrsTransformerFactory crsTransformerFactory, + VolatileRegistry volatileRegistry, + @Assisted TileProviderHttpData data) { super(volatileRegistry, data, "access"); this.metadata = new LinkedHashMap<>(); + this.tileMatrixSetRepository = tileMatrixSetRepository; + this.crsTransformerFactory = crsTransformerFactory; Map tilesetSources = data.getTilesets().entrySet().stream() @@ -114,16 +131,47 @@ private void loadMetadata() { } private TilesetMetadata loadMetadata(TilesetHttp tileset) { + Map levels = + tileset.getLevels().isEmpty() + ? getData().getTilesetDefaults().getLevels() + : tileset.getLevels(); + Optional bounds = + Extent.inherit(tileset.getExtent(), getData().getTilesetDefaults().getExtent()) + .getSpatial() + .flatMap(extent -> extent.toBoundingBox(OgcCrs.CRS84)) + .or(() -> getTileMatrixSetsExtent(levels.keySet())); + return ImmutableTilesetMetadata.builder() .encodings( tileset.getEncodings().isEmpty() ? getData().getTilesetDefaults().getEncodings().keySet() : tileset.getEncodings().keySet()) - .levels( - tileset.getLevels().isEmpty() - ? getData().getTilesetDefaults().getLevels() - : tileset.getLevels()) + .levels(levels) .center(tileset.getCenter().or(() -> getData().getTilesetDefaults().getCenter())) + .bounds(bounds) .build(); } + + private Optional getTileMatrixSetsExtent(Set tileMatrixSetIds) { + return tileMatrixSetIds.stream() + .map(tileMatrixSetRepository::get) + .flatMap(Optional::stream) + .map(this::toCrs84) + .flatMap(Optional::stream) + .reduce(BoundingBox::merge); + } + + private Optional toCrs84(TileMatrixSet tileMatrixSet) { + try { + return Optional.of(tileMatrixSet.getBoundingBoxCrs84(crsTransformerFactory)); + } catch (CrsTransformationException e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug( + "Could not transform the extent of tile matrix set '{}' to CRS84: {}", + tileMatrixSet.getId(), + e.getMessage()); + } + return Optional.empty(); + } + } } diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderMbTiles.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderMbTiles.java index bc7f3fc30..34da2cbc3 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderMbTiles.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/app/TileProviderMbTiles.java @@ -18,6 +18,7 @@ import de.ii.xtraplatform.crs.domain.OgcCrs; import de.ii.xtraplatform.entities.domain.Entity; import de.ii.xtraplatform.entities.domain.Entity.SubType; +import de.ii.xtraplatform.features.domain.Extent; import de.ii.xtraplatform.features.domain.FeatureSchema; import de.ii.xtraplatform.features.domain.ProviderData; import de.ii.xtraplatform.tiles.domain.ChainedTileProvider; @@ -175,12 +176,24 @@ private void loadMetadata(Map tilesetSources) { (key, path) -> { Tuple tilesetKey = toTuple(key); - metadata.put(tilesetKey.first(), loadMetadata(tilesetKey.second(), path)); + TilesetMbTiles tileset = + getData() + .getTilesets() + .get(tilesetKey.first()) + .mergeDefaults(getData().getTilesetDefaults()); + Optional configuredExtent = + Extent.inherit(tileset.getExtent(), getData().getTilesetDefaults().getExtent()) + .getSpatial() + .flatMap(extent -> extent.toBoundingBox(OgcCrs.CRS84)); + + metadata.put( + tilesetKey.first(), loadMetadata(tilesetKey.second(), path, configuredExtent)); tmsRanges.put(tilesetKey.first(), metadata.get(tilesetKey.first()).getTmsRanges()); }); } - private TilesetMetadata loadMetadata(String tms, Path path) { + private TilesetMetadata loadMetadata( + String tms, Path path, Optional configuredExtent) { try { MbtilesMetadata metadata = new MbtilesTileset(path, false).getMetadata(); TileMatrixSet tileMatrixSet = @@ -208,11 +221,12 @@ private TilesetMetadata loadMetadata(String tms, Path path) { tms, new ImmutableMinMax.Builder().min(minzoom).max(maxzoom).getDefault(defzoom).build()); List bbox = metadata.getBounds(); - Optional bounds = + Optional boundsFromMetadata = bbox.size() == 4 ? Optional.of( BoundingBox.of(bbox.get(0), bbox.get(1), bbox.get(2), bbox.get(3), OgcCrs.CRS84)) : Optional.empty(); + Optional bounds = configuredExtent.or(() -> boundsFromMetadata); TilesFormat format = metadata.getFormat(); List vectorSchemas = metadata.getVectorLayers().stream() diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeatures.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeatures.java index 71a9c73dd..2d1d7ff7d 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeatures.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeatures.java @@ -13,6 +13,7 @@ import de.ii.xtraplatform.entities.domain.maptobuilder.Buildable; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableBuilder; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; import java.util.List; import java.util.Map; import java.util.Objects; @@ -49,6 +50,20 @@ public interface TilesetFeatures @Override Optional getFeatureProvider(); + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If set, the + * extent is not derived from the feature provider. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn gesetzt, wird + * der Extent nicht vom Feature-Provider übernommen. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } + @DocIgnore @Nullable @Override diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeaturesDefaults.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeaturesDefaults.java index f650d3f42..d0fcddd82 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeaturesDefaults.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetFeaturesDefaults.java @@ -11,6 +11,8 @@ import de.ii.xtraplatform.entities.domain.maptobuilder.Buildable; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableBuilder; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; +import java.util.Optional; import org.immutables.value.Value; /** @@ -35,4 +37,18 @@ default ImmutableTilesetFeaturesDefaults.Builder getBuilder() { } abstract class Builder implements BuildableBuilder {} + + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If set, the + * extent is not derived from the feature provider. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn gesetzt, wird + * der Extent nicht vom Feature-Provider übernommen. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } } diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttp.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttp.java index 22f2071bf..7ef2d294c 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttp.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttp.java @@ -12,6 +12,7 @@ import de.ii.xtraplatform.entities.domain.maptobuilder.Buildable; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableBuilder; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; import java.util.Map; import java.util.Optional; import org.immutables.value.Value; @@ -40,6 +41,20 @@ public interface TilesetHttp extends TilesetCommon, WithEncodings, Buildable getEncodings(); + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If not set, + * the extent of the tile matrix sets is used. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn nicht gesetzt, + * wird der Extent der Kachelschemas verwendet. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } + /** * @langEn URL template for accessing tiles. Parameters to use are `{{tileMatrix}}`, * `{{tileRow}}`, `{{tileCol}}` and `{{fileExtension}}`. Single curly braces are also allowed. diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttpDefaults.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttpDefaults.java index acd094cd5..5245ff576 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttpDefaults.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetHttpDefaults.java @@ -11,6 +11,8 @@ import de.ii.xtraplatform.entities.domain.maptobuilder.Buildable; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableBuilder; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; +import java.util.Optional; import org.immutables.value.Value; /** @@ -31,5 +33,19 @@ default ImmutableTilesetHttpDefaults.Builder getBuilder() { return new ImmutableTilesetHttpDefaults.Builder().from(this); } + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If not set, + * the extent of the tile matrix sets is used. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn nicht gesetzt, + * wird der Extent der Kachelschemas verwendet. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } + abstract class Builder implements BuildableBuilder {} } diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTiles.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTiles.java index 37d14ec20..00f9d1b08 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTiles.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTiles.java @@ -13,6 +13,7 @@ import de.ii.xtraplatform.entities.domain.maptobuilder.Buildable; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableBuilder; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; import java.util.Objects; import java.util.Optional; import javax.annotation.Nullable; @@ -38,6 +39,20 @@ public interface TilesetMbTiles extends TilesetCommon, Buildable */ String getSource(); + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If set, + * `bounds` from the MBTiles metadata is ignored. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn gesetzt, wird + * `bounds` aus den MBTiles-Metadaten ignoriert. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } + /** * @langEn Tile Matrix Set of the tiles in the MBTiles file. * @langDe Kachelschema der Kacheln in der MBTiles-Datei. diff --git a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTilesDefaults.java b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTilesDefaults.java index ab183c13f..760be6657 100644 --- a/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTilesDefaults.java +++ b/xtraplatform-tiles/src/main/java/de/ii/xtraplatform/tiles/domain/TilesetMbTilesDefaults.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import de.ii.xtraplatform.docs.DocIgnore; import de.ii.xtraplatform.entities.domain.maptobuilder.BuildableMap; +import de.ii.xtraplatform.features.domain.Extent; import de.ii.xtraplatform.tiles.domain.ImmutableMinMax.Builder; import java.util.Optional; import org.immutables.value.Value; @@ -36,4 +37,18 @@ public interface TilesetMbTilesDefaults extends TilesetCommonDefaults { default String getTileMatrixSet() { return "WebMercatorQuad"; } + + /** + * @langEn Fixed extent of the tileset, only `spatial` in CRS84 (longitude/latitude). If set, + * `bounds` from the MBTiles metadata is ignored. + * @langDe Fester Extent des Tilesets, nur `spatial` in CRS84 (Länge/Breite). Wenn gesetzt, wird + * `bounds` aus den MBTiles-Metadaten ignoriert. + * @since v4.9 + */ + Optional getExtent(); + + @Value.Check + default void checkExtentIsSpatial() { + Extent.checkSpatialOnly(getExtent()); + } } diff --git a/xtraplatform-tiles/src/test/groovy/de/ii/xtraplatform/tiles/app/TileProviderHttpExtentSpec.groovy b/xtraplatform-tiles/src/test/groovy/de/ii/xtraplatform/tiles/app/TileProviderHttpExtentSpec.groovy new file mode 100644 index 000000000..b3780836f --- /dev/null +++ b/xtraplatform-tiles/src/test/groovy/de/ii/xtraplatform/tiles/app/TileProviderHttpExtentSpec.groovy @@ -0,0 +1,126 @@ +/* + * Copyright 2026 interactive instruments GmbH + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package de.ii.xtraplatform.tiles.app + +import de.ii.xtraplatform.base.domain.resiliency.VolatileRegistry +import de.ii.xtraplatform.crs.domain.BoundingBox +import de.ii.xtraplatform.crs.domain.CrsTransformationException +import de.ii.xtraplatform.crs.domain.CrsTransformerFactory +import de.ii.xtraplatform.crs.domain.OgcCrs +import de.ii.xtraplatform.features.domain.ImmutableExtent +import de.ii.xtraplatform.features.domain.ImmutableSpatialExtent +import de.ii.xtraplatform.features.domain.ImmutableTemporalExtent +import de.ii.xtraplatform.features.domain.SpatialExtent +import de.ii.xtraplatform.tiles.domain.ImmutableMinMax +import de.ii.xtraplatform.tiles.domain.ImmutableTileProviderHttpData +import de.ii.xtraplatform.tiles.domain.ImmutableTilesetHttp +import de.ii.xtraplatform.tiles.domain.ImmutableTilesetHttpDefaults +import de.ii.xtraplatform.tiles.domain.TileMatrixSet +import de.ii.xtraplatform.tiles.domain.TileMatrixSetRepository +import de.ii.xtraplatform.tiles.domain.TilesFormat +import de.ii.xtraplatform.tiles.domain.TilesetHttp +import spock.lang.Specification + +class TileProviderHttpExtentSpec extends Specification { + + static SpatialExtent TILESET_EXTENT = new ImmutableSpatialExtent.Builder().xmin(7).ymin(50).xmax(8).ymax(51).build() + static SpatialExtent DEFAULTS_EXTENT = new ImmutableSpatialExtent.Builder().xmin(5).ymin(47).xmax(15).ymax(55).build() + static SpatialExtent COMPUTED = new ImmutableSpatialExtent.Builder().computed(true).build() + + TileMatrixSetRepository tileMatrixSets = Stub() + CrsTransformerFactory crsTransformerFactory = Stub() + + TileMatrixSet tileMatrixSet(String id, BoundingBox crs84) { + TileMatrixSet tms = Stub() + tms.getId() >> id + tms.getBoundingBoxCrs84(_) >> crs84 + tileMatrixSets.get(id) >> Optional.of(tms) + tms + } + + TileProviderHttp provider(SpatialExtent defaultsExtent) { + def defaults = new ImmutableTilesetHttpDefaults.Builder() + .putLevels("Unknown", new ImmutableMinMax.Builder().min(0).max(10)) + .putEncodings(TilesFormat.MVT, "pbf") + if (defaultsExtent != null) { + defaults.extent(ImmutableExtent.builder().spatial(defaultsExtent).build()) + } + def data = new ImmutableTileProviderHttpData.Builder() + .id("tiles") + .providerType("TILE") + .providerSubType("HTTP") + .tilesetDefaults(defaults.build()) + .build() + new TileProviderHttp(tileMatrixSets, crsTransformerFactory, Stub(VolatileRegistry), data) + } + + static TilesetHttp tileset(SpatialExtent extent, List tileMatrixSetIds = []) { + def builder = new ImmutableTilesetHttp.Builder().id("ts").urlTemplate("https://example.com/{z}/{y}/{x}") + tileMatrixSetIds.each { builder.putLevels(it, new ImmutableMinMax.Builder().min(0).max(10)) } + if (extent != null) { + builder.extent(ImmutableExtent.builder().spatial(extent).build()) + } + builder.build() + } + + static BoundingBox crs84(double xmin, double ymin, double xmax, double ymax) { + BoundingBox.of(xmin, ymin, xmax, ymax, OgcCrs.CRS84) + } + + def "the tileset extent wins over the defaults"() { + expect: + provider(DEFAULTS_EXTENT).loadMetadata(tileset(TILESET_EXTENT)).bounds == Optional.of(crs84(7, 50, 8, 51)) + } + + def "the defaults extent is used if the tileset has none"() { + expect: + provider(DEFAULTS_EXTENT).loadMetadata(tileset(null)).bounds == Optional.of(crs84(5, 47, 15, 55)) + } + + def "without a fixed extent the union of the tile matrix set extents is used"() { + given: + tileMatrixSet("A", crs84(0, 0, 10, 10)) + tileMatrixSet("B", crs84(-5, 5, 5, 20)) + + expect: + provider(null).loadMetadata(tileset(null, ["A", "B"])).bounds == Optional.of(crs84(-5, 0, 10, 20)) + } + + def "computed on the tileset discards the defaults extent"() { + given: + tileMatrixSet("A", crs84(0, 0, 10, 10)) + + expect: + provider(DEFAULTS_EXTENT).loadMetadata(tileset(COMPUTED, ["A"])).bounds == Optional.of(crs84(0, 0, 10, 10)) + } + + def "tile matrix sets that cannot be transformed are skipped"() { + given: + TileMatrixSet broken = Stub() + broken.getBoundingBoxCrs84(_) >> { throw new CrsTransformationException("no") } + tileMatrixSets.get("broken") >> Optional.of(broken) + + expect: + provider(null).loadMetadata(tileset(null, ["broken"])).bounds.isEmpty() + } + + def "a temporal extent is rejected for a tileset"() { + + when: + new ImmutableTilesetHttp.Builder().id("ts").urlTemplate("https://example.com/{z}/{y}/{x}") + .extent(ImmutableExtent.builder() + .spatial(TILESET_EXTENT) + .temporal(new ImmutableTemporalExtent.Builder().start("2020-01-01").build()) + .build()) + .build() + + then: + def e = thrown(IllegalStateException) + e.message == "Extent: 'temporal' is not supported for tiles, only 'spatial'." + } +}