String crsCode = request.getCrs();
if (null == crsCode) {
throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "crs");
}
String[] layerIds = request.getLayerIds();
Geometry location = converter.toInternal(request.getLocation());
int queryType = request.getQueryType();
double ratio = request.getRatio();
int searchType = request.getSearchType();
Crs crs = geoService.getCrs2(request.getCrs());
// Check if a buffer should be added around the location:
Geometry geometry = location;
if (request.getBuffer() > 0) {
geometry = location.buffer(request.getBuffer());
}
log.debug("search by location " + geometry);
if (layerIds != null && layerIds.length > 0) {
for (String layerId : layerIds) {
if (securityContext.isLayerVisible(layerId)) {
VectorLayer vectorLayer = configurationService.getVectorLayer(layerId);
if (vectorLayer != null) {
String geomName = vectorLayer.getLayerInfo().getFeatureInfo().getGeometryType().getName();
// Transform geometry to layer CRS:
Geometry layerGeometry = geoService.transform(geometry, crs, vectorLayer.getCrs());
log.trace("on layer " + layerId + " use " + layerGeometry);
// Create the correct Filter object:
Filter f = null;
switch (queryType) {
case SearchByLocationRequest.QUERY_INTERSECTS:
f = filterCreator.createIntersectsFilter(layerGeometry, geomName);
break;
case SearchByLocationRequest.QUERY_CONTAINS:
f = filterCreator.createContainsFilter(layerGeometry, geomName);
break;
case SearchByLocationRequest.QUERY_TOUCHES:
f = filterCreator.createTouchesFilter(layerGeometry, geomName);
break;
case SearchByLocationRequest.QUERY_WITHIN:
f = filterCreator.createWithinFilter(layerGeometry, geomName);
break;
}
//Set the per layer filter
if (null != request.getFilter(layerId)) {
if (null == f) {
f = filterCreator.parseFilter(request.getFilter(layerId));
} else {
f = filterCreator.createAndFilter(
filterCreator.parseFilter(request.getFilter(layerId)), f);
}
}
//Set the global filter
if (null != request.getFilter()) {
if (null == f) {
f = filterCreator.parseFilter(request.getFilter());
} else {
f = filterCreator.createAndFilter(filterCreator.parseFilter(request.getFilter()), f);
}
}
// Get the features:
List<InternalFeature> temp = layerService.getFeatures(layerId, crs, f, null, request
.getFeatureIncludes());
if (temp.size() > 0) {
List<Feature> features = new ArrayList<Feature>();
// Calculate overlap ratio in case of intersects:
if (queryType == SearchByLocationRequest.QUERY_INTERSECTS && ratio >= 0 && ratio < 1) {
for (InternalFeature feature : temp) {
double minimalOverlap = feature.getGeometry().getArea() * ratio;
Geometry overlap = location.intersection(feature.getGeometry());
double effectiveOverlap = overlap.getArea();
if (minimalOverlap <= effectiveOverlap) {
log.trace("found " + feature);
Feature dto = converter.toDto(feature);
dto.setCrs(crsCode);
features.add(dto);