Skip to content

Commit

Permalink
Optional geo field check (fix #748)
Browse files Browse the repository at this point in the history
  • Loading branch information
alainbodiguel committed Jan 6, 2022
1 parent 450cd70 commit 5bba562
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty;
import io.arlas.server.core.services.CollectionReferenceService;
import io.arlas.server.core.utils.MapExplorer;
import io.arlas.server.core.utils.StringUtil;
import io.dropwizard.servlets.tasks.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -116,8 +115,12 @@ private CollectionReferenceDescription checkCollectionValidity(CollectionReferen
break;
}
}
return StringUtil.isNullOrEmpty(collection.params.idPath) ? null : collection;
}
if (collection.params.idPath == null || collection.params.idPath.isEmpty()
|| collection.params.centroidPath == null || collection.params.centroidPath.isEmpty()
|| collection.params.geometryPath == null || collection.params.geometryPath.isEmpty()
|| collection.params.timestampPath == null || collection.params.timestampPath.isEmpty())
return null;
return collection; }

@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ public class CollectionReferenceParameters implements Serializable {
@JsonProperty(value = "id_path", required = true)
public String idPath;

@JsonProperty(value = "geometry_path")
@NotEmpty
@JsonProperty(value = "geometry_path", required = true)
public String geometryPath;

@JsonProperty(value = "centroid_path")
@NotEmpty
@JsonProperty(value = "centroid_path", required = true)
public String centroidPath;

@JsonProperty(value = "h3_path")
public String h3Path;

@JsonProperty(value = "timestamp_path")
@NotEmpty
@JsonProperty(value = "timestamp_path", required = true)
public String timestampPath;

@JsonProperty(value = "exclude_fields")
Expand Down Expand Up @@ -104,7 +107,7 @@ public void setGeometryType(String path, GeoTypeEnum type) {
}

public GeoTypeEnum getGeometryType(String path) {
return path == null ? null : this.geoTypes.get(path);
return this.geoTypes.get(path);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package io.arlas.server.core.model;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.geojson.LngLatAlt;
import org.geojson.Polygon;
Expand All @@ -33,113 +32,110 @@
import java.util.Date;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class DublinCoreElementName implements Serializable {
private static final long serialVersionUID = -3452567240629298463L;

public DublinCoreElementName(){
public DublinCoreElementName() {
}

@JsonProperty(value = "title")
@JsonProperty(value = "title", required = false)
public String title = "";

@JsonProperty(value = "creator")
@JsonProperty(value = "creator", required = false)
public String creator = "";

@JsonProperty(value = "subject")
@JsonProperty(value = "subject", required = false)
public String subject = "";

@JsonProperty(value = "description")
@JsonProperty(value = "description", required = false)
public String description = "";

@JsonProperty(value = "publisher")
@JsonProperty(value = "publisher", required = false)
public String publisher = "";

@JsonProperty(value = "contributor")
@JsonProperty(value = "contributor", required = false)
public String contributor = "";

@JsonProperty(value = "type")
@JsonProperty(value = "type", required = false)
public String type = "";

@JsonProperty(value = "format")
@JsonProperty(value = "format", required = false)
public String format = "";

@JsonProperty(value = "identifier")
@JsonProperty(value = "identifier", required = false)
public String identifier = String.valueOf(java.util.UUID.randomUUID());

@JsonProperty(value = "source")
@JsonProperty(value = "source", required = false)
public String source = "";

@JsonProperty(value = "language")
@JsonProperty(value = "language", required = false)
public String language = "";

@JsonProperty(value = "bbox")
public Bbox bbox;
@JsonProperty(value = "bbox", required = false)
public Bbox bbox = new Bbox();

private Date date = new Date();

@JsonGetter(value = "date")
public String getDate(){
public String getDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return simpleDateFormat.format(date);
return simpleDateFormat.format(date);
}

private Polygon coverageGeometry;

private JSONObject coverage;

@JsonGetter(value = "coverage")
public JSONObject getCoverage() {
if (bbox != null) {
coverageGeometry = new org.geojson.Polygon();
List<LngLatAlt> exteriorRing = new ArrayList<>();
exteriorRing.add(new LngLatAlt(bbox.west, bbox.south));
exteriorRing.add(new LngLatAlt(bbox.east, bbox.south));
exteriorRing.add(new LngLatAlt(bbox.east, bbox.north));
exteriorRing.add(new LngLatAlt(bbox.west, bbox.north));
exteriorRing.add(new LngLatAlt(bbox.west, bbox.south));
coverageGeometry.setExteriorRing(exteriorRing);
coverage = new JSONObject();
JSONArray jsonArayExt = new JSONArray();
coverageGeometry.getExteriorRing().forEach(lngLatAlt -> {
JSONArray jsonArayLngLat = new JSONArray();
jsonArayLngLat.add(0, lngLatAlt.getLongitude());
jsonArayLngLat.add(1, lngLatAlt.getLatitude());
jsonArayExt.add(jsonArayLngLat);
});
JSONArray jsonAray = new JSONArray();
jsonAray.add(jsonArayExt);
coverage.put("type", "Polygon");
coverage.put("coordinates", jsonAray);
}
coverageGeometry = new org.geojson.Polygon();
List<LngLatAlt> exteriorRing = new ArrayList<>();
exteriorRing.add(new LngLatAlt(bbox.west, bbox.south));
exteriorRing.add(new LngLatAlt(bbox.east, bbox.south));
exteriorRing.add(new LngLatAlt(bbox.east, bbox.north));
exteriorRing.add(new LngLatAlt(bbox.west, bbox.north));
exteriorRing.add(new LngLatAlt(bbox.west, bbox.south));
coverageGeometry.setExteriorRing(exteriorRing);
coverage = new JSONObject();
JSONArray jsonArayExt = new JSONArray();
coverageGeometry.getExteriorRing().forEach(lngLatAlt -> {
JSONArray jsonArayLngLat = new JSONArray();
jsonArayLngLat.add(0, lngLatAlt.getLongitude());
jsonArayLngLat.add(1, lngLatAlt.getLatitude());
jsonArayExt.add(jsonArayLngLat);
});
JSONArray jsonAray = new JSONArray();
jsonAray.add(jsonArayExt);
coverage.put("type", "Polygon");
coverage.put("coordinates", jsonAray);
return coverage;
}

private String coverageCentroid;

@JsonGetter(value = "coverage_centroid")
public String getCoverageCentroid(){
public String getCoverageCentroid() {
if (coverageGeometry != null) {
LngLatAlt bottomLeft = coverageGeometry.getExteriorRing().get(0);
LngLatAlt topRight = coverageGeometry.getExteriorRing().get(2);
double centroidLat = (bottomLeft.getLatitude() + topRight.getLatitude()) / 2;
double centroidLng = (bottomLeft.getLongitude() + topRight.getLongitude()) / 2;
coverageCentroid = centroidLat + "," + centroidLng;
} else {
coverageCentroid = "0,0";
}
return coverageCentroid;
}

public class Bbox implements Serializable {
private static final long serialVersionUID = 364766455618062216L;

@JsonProperty(value = "north", required = true)
public double north = 90.0;

@JsonProperty(value = "south", required = true)
public double south = -90.0;

@JsonProperty(value = "east", required = true)
public double east = 180.0;

@JsonProperty(value = "west", required = true)
public double west = -180.0;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ protected Map<String, LinkedHashMap> getMapping(String indexName) throws ArlasEx
}

public CollectionReference putCollectionReference(CollectionReference collectionReference) throws ArlasException {
checkCollectionReferenceParameters(collectionReference);
return putCollectionReference(collectionReference, true);
}

public CollectionReference putCollectionReference(CollectionReference collectionReference, boolean checkGeo) throws ArlasException {
checkCollectionReferenceParameters(collectionReference, checkGeo);
putCollectionReferenceWithDao(collectionReference);
//explicit clean-up cache
cacheManager.removeCollectionReference(collectionReference.collectionName);
Expand Down Expand Up @@ -254,16 +258,16 @@ public List<CollectionReferenceDescription> getAllIndicesAsCollections() throws
}

@SuppressWarnings({"unchecked", "rawtypes"})
protected void checkCollectionReferenceParameters(CollectionReference collectionReference) throws ArlasException {
protected void checkCollectionReferenceParameters(CollectionReference collectionReference, boolean checkGeo) throws ArlasException {
//get fields
List<String> fields = new ArrayList<>();
if (collectionReference.params.idPath != null)
fields.add(collectionReference.params.idPath);
if (collectionReference.params.geometryPath != null)
if (collectionReference.params.geometryPath != null && checkGeo)
fields.add(collectionReference.params.geometryPath);
if (collectionReference.params.centroidPath != null)
if (collectionReference.params.centroidPath != null && checkGeo)
fields.add(collectionReference.params.centroidPath);
if (collectionReference.params.h3Path != null)
if (collectionReference.params.h3Path != null && checkGeo)
fields.add(collectionReference.params.h3Path);
if (collectionReference.params.timestampPath != null)
fields.add(collectionReference.params.timestampPath);
Expand All @@ -272,15 +276,13 @@ protected void checkCollectionReferenceParameters(CollectionReference collection
CheckParams.checkExcludeField(excludeField, fields);
}
Map<String, LinkedHashMap> mappings = CollectionUtil.checkAliasMappingFields(getMapping(collectionReference.params.indexName), fields.toArray(new String[0]));
if (collectionReference.params.timestampPath != null) {
for (String index : mappings.keySet()) {
Map<String, Object> timestampMD = CollectionUtil.getFieldFromProperties(collectionReference.params.timestampPath, mappings.get(index));
collectionReference.params.customParams = new HashMap<>();
if (timestampMD.containsKey("format")) {
collectionReference.params.customParams.put(CollectionReference.TIMESTAMP_FORMAT, timestampMD.get("format").toString());
} else {
collectionReference.params.customParams.put(CollectionReference.TIMESTAMP_FORMAT, CollectionReference.DEFAULT_TIMESTAMP_FORMAT);
}
for (String index : mappings.keySet()) {
Map<String, Object> timestampMD = CollectionUtil.getFieldFromProperties(collectionReference.params.timestampPath, mappings.get(index));
collectionReference.params.customParams = new HashMap<>();
if (timestampMD.containsKey("format")) {
collectionReference.params.customParams.put(CollectionReference.TIMESTAMP_FORMAT, timestampMD.get("format").toString());
} else {
collectionReference.params.customParams.put(CollectionReference.TIMESTAMP_FORMAT, CollectionReference.DEFAULT_TIMESTAMP_FORMAT);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,8 @@ public Response opensearch(@Context UriInfo uri,
addURLs(prefix, description.url, exploreService.describeCollection(cr, columnFilter).properties, new Stack<>());
List<Url> urls = new ArrayList<>();
description.url.forEach(url -> {
if (cr.params.geometryPath != null) {
urls.add(url(url.template + "&f=" + cr.params.geometryPath + ":intersect:{geo:box?}"));
urls.add(url(url.template + "&f=" + cr.params.geometryPath + ":intersect:{geo:geometry?}"));
} else {
urls.add(url(url.template));
}
urls.add(url(url.template + "&f="+cr.params.geometryPath+":intersect:{geo:box?}"));
urls.add(url(url.template + "&f="+cr.params.geometryPath+":intersect:{geo:geometry?}"));
});
description.url = urls;
return cache(Response.ok(description), maxagecache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Response importCollections(
if ((c.endsWith("*") && collection.collectionName.startsWith(c.substring(0, c.indexOf("*"))))
|| collection.collectionName.equals(c)){
try {
savedCollections.add(save(collection.collectionName, collection.params));
savedCollections.add(save(collection.collectionName, collection.params, true));
} catch (Exception e) {
throw new ArlasException(e.getMessage());
}
Expand Down Expand Up @@ -236,16 +236,19 @@ public Response put(
@ApiParam(name = "pretty",
value = Documentation.FORM_PRETTY,
defaultValue = "false")
@QueryParam(value = "pretty") Boolean pretty
@QueryParam(value = "pretty") Boolean pretty,

@ApiParam(name = "isgeo", defaultValue = "true")
@QueryParam(value = "isgeo") Boolean checkGeo

) throws ArlasException {
if (collection != null && collection.equals(META_COLLECTION_NAME)) {
throw new NotAllowedException("'" + META_COLLECTION_NAME + "' is not allowed as a name for collections");
}
return ResponseFormatter.getResultResponse(save(collection, collectionReferenceParameters));
return ResponseFormatter.getResultResponse(save(collection, collectionReferenceParameters, checkGeo));
}

public CollectionReference save(String collection, CollectionReferenceParameters collectionReferenceParameters) throws ArlasException {
public CollectionReference save(String collection, CollectionReferenceParameters collectionReferenceParameters, Boolean checkGeo) throws ArlasException {
CollectionReference collectionReference = new CollectionReference(collection, collectionReferenceParameters);
setDefaultInspireParameters(collectionReference);
if (inspireConfigurationEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ public Response geohashgeoaggregate(
if (collectionReference == null) {
throw new NotFoundException(collection);
}
if (collectionReference.params.centroidPath == null) {
throw new BadRequestException("You cannot request a geohash aggregation on a collection which has no centroid path defined.");
}

if (geohash.startsWith("#")) {
geohash = geohash.substring(1);
Expand Down Expand Up @@ -425,9 +422,6 @@ public Response geotilegeoaggregate(
if (collectionReference == null) {
throw new NotFoundException(collection);
}
if (collectionReference.params.centroidPath == null) {
throw new BadRequestException("You cannot request a geotilegeoaggregate on a collection which has no centroid path defined.");
}

if (agg == null || agg.size() == 0) {
agg = Collections.singletonList("geotile:" + collectionReference.params.centroidPath + ":interval-" + (z+3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,6 @@ public Response tiledgeosearch(
if (collectionReference == null) {
throw new NotFoundException(collection);
}
if (collectionReference.params.centroidPath == null) {
throw new BadRequestException("You cannot request a tiledgeosearch on a collection which has no centroid path defined.");
}

BoundingBox bbox = GeoTileUtil.getBoundingBox(new Tile(x, y, z));
// west, south, east, north
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ public Response tiledgeosearch(
if (collectionReference == null) {
throw new NotFoundException(collection);
}
if (collectionReference.params.geometryPath == null) {
throw new BadRequestException("You cannot request a tiledgeosearch on a collection which has no geometry path defined.");
}
if (collectionReference.params.rasterTileURL == null) {
throw new NotFoundException(collectionReference.collectionName+" has no URL defined for fetching the tiles.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ public static void addBriefIdentificationInfo(MDMetadataType mdMetadataType, Col
.map(inspireURI -> inspireURI.code)
.orElse(collectionReference.params.dublinCoreElementName.identifier);
addCICitation(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.title, null, null, uniqueResourceIdentifier);
if (collectionReference.params.dublinCoreElementName.bbox != null) {
addExtent(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.bbox);
}
addExtent(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.bbox);
mdIdentificationPropertyType.setAbstractMDIdentification(gmdObjectFactory.createMDDataIdentification(mdDataIdentificationType));
mdMetadataType.getIdentificationInfo().add(mdIdentificationPropertyType);
}
Expand Down Expand Up @@ -326,9 +324,7 @@ public static void addSummaryIdentificationInfo(MDMetadataType mdMetadataType, C
}
addIdentificationLanguage(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.language);
addSpatialRepresentationType(mdDataIdentificationType);
if (collectionReference.params.dublinCoreElementName.bbox != null) {
addExtent(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.bbox);
}
addExtent(mdDataIdentificationType, collectionReference.params.dublinCoreElementName.bbox);
mdIdentificationPropertyType.setAbstractMDIdentification(gmdObjectFactory.createMDDataIdentification(mdDataIdentificationType));
mdMetadataType.getIdentificationInfo().add(mdIdentificationPropertyType);
}
Expand Down

0 comments on commit 5bba562

Please sign in to comment.