Java Code Examples for org.opengis.filter.Filter#evaluate()

The following examples show how to use org.opengis.filter.Filter#evaluate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShapeInMemLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This implementation does not support the 'offset' and 'maxResultSize' parameters.
 */
public Iterator<?> getElements(Filter filter, int offset, int maxResultSize) throws LayerException {
	List<SimpleFeature> filteredList = new ArrayList<SimpleFeature>();
	for (SimpleFeature feature : features.values()) {
		if (filter.evaluate(feature)) {
			filteredList.add(feature);
			if (filteredList.size() == maxResultSize) {
				break;
			}
		}
	}
	return filteredList.iterator();
}
 
Example 2
Source File: DelaySecurityFilterPostStep.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(PipelineContext context, GetFeaturesContainer response) throws GeomajasException {
	Filter filter = context.getOptional(SAVED_FILTER_KEY, Filter.class);
	context.put(PipelineCode.FILTER_KEY, filter);
	Filter securityFilter = context.getOptional(SECURITY_FILTER_KEY, Filter.class);
	if (securityFilter != null) {
		for (ListIterator<InternalFeature> it = response.getFeatures().listIterator(); it.hasNext();) {
			if (!securityFilter.evaluate(it.next())) {
				it.remove();
			}
		}
		recorder.record("layer", "applied security filter after layer");
	}
}
 
Example 3
Source File: SaveOrUpdateDeleteStep.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
public void execute(PipelineContext context, Object response) throws GeomajasException {
	InternalFeature newFeature = context.getOptional(PipelineCode.FEATURE_KEY, InternalFeature.class);
	if (null == newFeature) {
		// delete ?
		InternalFeature oldFeature = context.getOptional(PipelineCode.OLD_FEATURE_KEY, InternalFeature.class);
		if (null != oldFeature) {
			String layerId = context.get(PipelineCode.LAYER_ID_KEY, String.class);
			if (securityContext.isFeatureDeleteAuthorized(layerId, oldFeature)) {
				VectorLayer layer = context.get(PipelineCode.LAYER_KEY, VectorLayer.class);
				Object featureObj = layer.read(oldFeature.getId());
				if (null != featureObj) {
					Filter securityFilter = getSecurityFilter(layer,
							securityContext.getDeleteAuthorizedArea(layerId));
					if (securityFilter.evaluate(featureObj)) {
						layer.delete(oldFeature.getId());
					} else {
						throw new GeomajasSecurityException(ExceptionCode.FEATURE_DELETE_PROHIBITED,
								oldFeature.getId(), securityContext.getUserId());
					}
				}
			} else {
				throw new GeomajasSecurityException(ExceptionCode.FEATURE_DELETE_PROHIBITED,
						oldFeature.getId(), securityContext.getUserId());
			}
		}
		context.setFinished(true); // stop pipeline execution
	}
}
 
Example 4
Source File: BeanLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This implementation does not support the 'offset' and 'maxResultSize' parameters.
 */
public Iterator<?> getElements(Filter filter, int offset, int maxResultSize) throws LayerException {
	if (null == filter) {
		filter = Filter.INCLUDE;
	}
	List<Object> filteredList = new ArrayList<Object>();
	try {
		synchronized (featuresById) {
			for (Object feature : featuresById.values()) {
				if (filter.evaluate(feature)) {
					filteredList.add(feature);
				}
			}
		}
	} catch (Exception e) { // NOSONAR
		throw new LayerException(e, ExceptionCode.FILTER_EVALUATION_PROBLEM, filter, getId());
	}
	// Sorting of elements.
	if (comparator != null) {
		Collections.sort(filteredList, comparator);
	}
	if (maxResultSize > 0) {
		int fromIndex = Math.max(0, offset);
		int toIndex = Math.min(offset + maxResultSize, filteredList.size());
		toIndex = Math.max(fromIndex, toIndex);
		return filteredList.subList(fromIndex, toIndex).iterator();
	} else {
		return filteredList.iterator();
	}
}
 
Example 5
Source File: GeoWaveTransactionManagement.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIterator<SimpleFeature> interweaveTransaction(
    final Integer limit,
    final Filter filter,
    final CloseableIterator<SimpleFeature> it) {
  return new CloseableIterator<SimpleFeature>() {

    Iterator<SimpleFeature> addedIt = addedFeatures.values().iterator();
    SimpleFeature feature = null;
    long count = 0;

    @Override
    public boolean hasNext() {
      if ((limit != null) && (limit.intValue() > 0) && (count > limit)) {
        return false;
      }
      while (addedIt.hasNext() && (feature == null)) {
        feature = addedIt.next();
        if (!filter.evaluate(feature)) {
          feature = null;
        }
      }
      while (it.hasNext() && (feature == null)) {
        feature = it.next();
        final ModifiedFeature modRecord = modifiedFeatures.get(feature.getID());
        // exclude removed features
        // and include updated features not written yet.
        final Collection<SimpleFeature> oldFeatures = removedFeatures.get(feature.getID());

        if (modRecord != null) {
          feature = modRecord.newFeature;
        } else if ((oldFeatures != null) && !oldFeatures.isEmpty()) {
          // need to check if the removed feature
          // was just moved meaning its original matches the
          // boundaries of this 'feature'. matchesOne(oldFeatures,
          // feature))
          feature = null;
        }
      }
      return feature != null;
    }

    @Override
    public SimpleFeature next() throws NoSuchElementException {
      if (feature == null) {
        throw new NoSuchElementException();
      }
      final SimpleFeature retVal = feature;
      feature = null;
      count++;
      return retVal;
    }

    @Override
    public void remove() {
      removedFeatures.put(feature.getID(), feature);
    }

    @Override
    public void close() {
      it.close();
    }
  };
}
 
Example 6
Source File: SaveOrUpdateSaveStep.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
public void execute(PipelineContext context, Object response) throws GeomajasException {
	InternalFeature newFeature = context.getOptional(PipelineCode.FEATURE_KEY, InternalFeature.class);
	Object feature = context.get(PipelineCode.FEATURE_DATA_OBJECT_KEY);
	String layerId = context.get(PipelineCode.LAYER_ID_KEY, String.class);
	VectorLayer layer = context.get(PipelineCode.LAYER_KEY, VectorLayer.class);
	FeatureInfo featureInfo = layer.getLayerInfo().getFeatureInfo();
	Map<String, AbstractAttributeInfo> attributesMap = featureInfo.getAttributesMap();
	FeatureModel featureModel = layer.getFeatureModel();
	Boolean isCreateObject = context.getOptional(PipelineCode.IS_CREATE_KEY, Boolean.class);
	boolean isCreate  = false;
	if (null != isCreateObject && isCreateObject) {
		isCreate = true;
	}

	// Assure only writable attributes are set
	Map<String, Attribute> requestAttributes = newFeature.getAttributes();
	Map<String, Attribute> filteredAttributes = new HashMap<String, Attribute>();
	if (null != requestAttributes) {
		for (Map.Entry<String, Attribute> entry : requestAttributes.entrySet()) {
			String key = entry.getKey();
			AbstractAttributeInfo attributeInfo = attributesMap.get(key);
			if (securityContext.isAttributeWritable(layerId, newFeature, key) &&
					attributeInfo instanceof EditableAttributeInfo &&
					((EditableAttributeInfo) attributeInfo).isEditable()) {
				filteredAttributes.put(key, entry.getValue());
			}
		}
	}
	featureModel.setAttributes(feature, filteredAttributes);

	if (featureInfo.getGeometryType().isEditable()) {
		if (null != newFeature.getGeometry()) {
			featureModel.setGeometry(feature, newFeature.getGeometry());
		} else {
			if (isCreate) {
				VectorLayerInfo layerInfo = layer.getLayerInfo();
				if (layerInfo.isAllowEmptyGeometries()) {
					// use empty geometry
					LayerType layerType = layer.getLayerInfo().getLayerType();
					com.vividsolutions.jts.geom.Geometry geometry = converterService.toInternal(
							new Geometry(layerType.getGeometryType(), featureModel.getSrid(), -1));
					newFeature.setGeometry(geometry);
					featureModel.setGeometry(feature, geometry);
				} else {
					throw new LayerException(ExceptionCode.LAYER_EMPTY_GEOMETRY_NOT_ALLOWED, layerId);
				}
			}
		}
	} else {
		if (isCreate) {
			throw new LayerException(ExceptionCode.CANNOT_CREATE_FEATURE_WITHOUT_GEOMETRY, layerId);
		}
	}

	Filter securityFilter;
	if (isCreate) {
		securityFilter = getSecurityFilter(layer, securityContext.getCreateAuthorizedArea(layerId));
	} else {
		securityFilter = getSecurityFilter(layer, securityContext.getUpdateAuthorizedArea(layerId));
	}
	if (securityFilter.evaluate(feature)) {
		context.put(PipelineCode.FEATURE_DATA_OBJECT_KEY, layer.saveOrUpdate(feature));
		if (isCreate) {
			newFeature.setId(featureModel.getId(feature));
		}
	} else {
		if (isCreate) {
			throw new GeomajasSecurityException(ExceptionCode.FEATURE_CREATE_PROHIBITED,
					securityContext.getUserId());
		} else {
			throw new GeomajasSecurityException(ExceptionCode.FEATURE_UPDATE_PROHIBITED, newFeature.getId(),
					securityContext.getUserId());				
		}
	}
}