Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bbox filter to queries #40

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.camptocamp.opendata.model;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Comparator;
import java.util.List;
Expand All @@ -16,20 +17,21 @@
@Accessors(chain = true)
public class DataQuery {

private @NonNull DataSource source;
private String layerName;
private Integer offset;
private Integer limit;
private String filter;
private @NonNull List<SortBy> sortBy;
private String targetCrs;
@NonNull DataSource source;
String layerName;
Integer offset;
Integer limit;
String filter;
@NonNull List<SortBy> sortBy;
String targetCrs;
List<BigDecimal> bbox;

public static DataQuery fromUri(URI dataUri) {
DataSource dataSource = DataSource.fromUri(dataUri);
return DataQuery.builder().source(dataSource).sortBy(List.of()).build();
}

public static record SortBy(String propertyName, boolean ascending) implements Comparator<GeodataRecord> {
public record SortBy(String propertyName, boolean ascending) implements Comparator<GeodataRecord> {

@SuppressWarnings("unchecked")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ private Query toQuery(@NonNull DataQuery query) {
throw new IllegalArgumentException("Unable to parse ECQL filter", e);
}
}
if (null != query.getBbox()) {
var bbox_filter = ff.bbox(query.getLayerName(), query.getBbox().get(0).doubleValue(),
query.getBbox().get(1).doubleValue(), query.getBbox().get(2).doubleValue(),
query.getBbox().get(3).doubleValue(), query.getTargetCrs());
q.setFilter(q.getFilter() != null ? ff.and(q.getFilter(), bbox_filter) : bbox_filter);
}
List<SortBy> sortBy = sortBy(query);
if (null != limit || null != offset) {
// always add natural order for paging consistency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -97,7 +98,7 @@ public ResponseEntity<FeatureCollection> getFeatures(@NonNull FeaturesQuery quer
private HttpHeaders getFeaturesHeaders(String collectionId) {
HttpHeaders headers = new HttpHeaders();

getRequest().map(req -> Arrays.stream(req.getHeader(ACCEPT).split(",")).findFirst().get())
getRequest().map(req -> Arrays.stream(Objects.requireNonNull(req.getHeader(ACCEPT)).split(",")).findFirst().get())
.map(MimeType::valueOf).flatMap(MimeTypes::find).ifPresent(m -> m.addHeaders(collectionId, headers));
return headers;
}
Expand Down Expand Up @@ -167,7 +168,7 @@ DataQuery toDataQuery(FeaturesQuery query) {
.withLimit(query.getLimit())//
.withOffset(query.getOffset())//
.withFilter(query.getFilter())//
.withSortBy(sortby).withTargetCrs(query.getCrs());
.withSortBy(sortby).withTargetCrs(query.getCrs()).withBbox(query.getBbox());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,71 @@ void testGetItemsWithReprojection_geojson() throws JSONException {

JSONAssert.assertEquals(expected, response.getBody(), false);
}

@Test
void testGetItemsWithBBoxFilter() throws JSONException {
final String url = itemsUrlTemplate + "&bbox=-2.416992,42.908160,8.613281,51.508742";
Map<String, String> urlVariables = Map.of("collection", "locations", "f", "geojson");
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, urlVariables);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.parseMediaType("application/geo+json"));
String expected = """
{
"type": "FeatureCollection",
"numberMatched": -1,
"numberReturned": -1,
"features": [
{
"type": "Feature",
"@typeName": "locations",
"@id": "15",
"geometry": {"type": "Point","@name": "geom","@srs": "EPSG:4326","coordinates": [7.099814,50.733992]},
"properties": {"city": "Bonn","number": 700,"year": 2016}
},
{
"type": "Feature",
"@typeName": "locations",
"@id": "6",
"geometry": {"type": "Point","@name": "geom","@srs": "EPSG:4326","coordinates": [6.6335,46.519833]},
"properties": {"city": " Lausanne","number": 560,"year": 2006}
}
],
"links": [
{
"href": "http://localhost:<port>/ogcapi/collections/locations/items?f=geojson&bbox=-2.416992,42.908160,8.613281,51.508742",
"rel": "self",
"type": "application/geo+json",
"title": "This document"
},
{
"href": "http://localhost:<port>/ogcapi/collections/locations/items?bbox=-2.416992,42.908160,8.613281,51.508742&f=json",
"rel": "alternate",
"type": "application/json",
"title": "This document as JSON"
},
{
"href": "http://localhost:<port>/ogcapi/collections/locations/items?bbox=-2.416992,42.908160,8.613281,51.508742&f=shapefile",
"rel": "alternate",
"type": "application/x-shapefile",
"title": "This document as Esri Shapefile"
},
{
"href": "http://localhost:<port>/ogcapi/collections/locations/items?bbox=-2.416992,42.908160,8.613281,51.508742&f=csv",
"rel": "alternate",
"type": "text/csv;charset=UTF-8",
"title": "This document as Comma Separated Values"
},
{
"href": "http://localhost:<port>/ogcapi/collections/locations/items?bbox=-2.416992,42.908160,8.613281,51.508742&f=ooxml",
"rel": "alternate",
"type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"title": "This document as Excel 2007 / OOXML"
}
]
}
"""
.replaceAll("<port>", String.valueOf(port));

JSONAssert.assertEquals(expected, response.getBody(), false);
}
}
Loading