/** * Calculate nearest using {@link NearestVisitor}. * * @param Collection collection to calculate the maximum * @param Expression Single Expression argument * @param value Attribute value used for nearest search * @return An object containing the maximum value of the attributes * @throws IllegalFilterException * @throws IOException */ static Object near(SimpleFeatureCollection collection, Expression expression, Object value) throws IllegalFilterException, IOException { NearestVisitor visitor = new NearestVisitor(expression, value); collection.accepts(visitor, new NullProgressListener()); return visitor.getNearestMatch(); }
public void setValue(Object maxBelow, Object minAbove) { if (maxBelow == null) { this.nearest = minAbove; } else if (minAbove == null) { this.nearest = maxBelow; } else { NearestAccumulator accumulator = getAccumulator(maxBelow.getClass()); accumulator.visit(maxBelow); accumulator.visit(minAbove); nearest = accumulator.getNearest(); } }
Object targetValue = nearest.getValueToMatch(); Expression expr = nearest.getExpression(); String attribute = null; if (maxBelow != null && maxBelow.equals(targetValue)) { nearest.setValue(maxBelow, null); } else { handleVisitor(qAbove, min); Comparable minAbove = (Comparable) min.getResult().getValue(); nearest.setValue(maxBelow, minAbove);
NearestVisitor vis = new NearestVisitor(ff.property("bar"), Integer.valueOf(0)); verify(delegate); vis = new NearestVisitor(ff.property("baz"), "abc");
} else if (visitor instanceof NearestVisitor) { NearestVisitor nearestVisitor = (NearestVisitor) visitor; Expression exp = nearestVisitor.getExpression(); if (!(nearestVisitor.getValueToMatch() instanceof Date)) { return false; factory.property( "abs(ms(" + dateFormatUTC.format(nearestVisitor.getValueToMatch()) + "," + propName.getPropertyName()
|| ((Range) value).getMinValue().equals(((Range) value).getMaxValue()))) { Date date = (Date) (value instanceof Range ? ((Range) value).getMinValue() : value); NearestVisitor visitor = new NearestVisitor(attribute, date); Filter filter = Filter.INCLUDE; if (acceptableRange != null) { Object result = visitor.getResult().getValue(); if (date.equals(result)) { return value;
@Override public Object getValue() { return NearestVisitor.this.getNearestMatch(); } };
exp = ((NearestVisitor) visitor).getExpression(); } else { FeatureAttributeVisitor fav = (FeatureAttributeVisitor) visitor; } else if (NearestVisitor.class.isInstance(visitor)) { NearestVisitor ne = (NearestVisitor) visitor; Object valueToMatch = ne.getValueToMatch(); Object maxBelow = uniqueValues.floor(valueToMatch); Object minAbove = uniqueValues.ceiling(valueToMatch); ne.setValue(maxBelow, minAbove);
public @Test void handleNearestVisitorNotIndexed() { final int valueToMatch = 2050; Integer expectedResult = Integer.valueOf(2000); testVisitor(new NearestVisitor(ff.property("ip"), valueToMatch), expectedResult); }
/** * @see GeogigFeatureVisitorHandler */ public @Test void handleNearestVisitorMaterializedAttribute() { NodeRef typeRef = pointsSource.delegate.getTypeRef(); createIndex(typeRef, Collections.singletonList("ip")); final int valueToMatch = 2050; Integer expectedResult = Integer.valueOf(2000); testVisitor(new NearestVisitor(ff.property("ip"), valueToMatch), expectedResult); }
private void testNearest( SimpleFeatureCollection fc, String attributeName, Object target, Object... validResults) throws IOException { PropertyName expr = ff.property(attributeName); NearestVisitor visitor = new NearestVisitor(expr, target); fc.accepts(visitor, null); Object nearestMatch = visitor.getNearestMatch(); if (validResults.length == 0) { assertNull(nearestMatch); } else { boolean found = false; for (Object object : validResults) { found |= object != null ? (object.equals(nearestMatch)) : nearestMatch == null; } assertTrue( "Could match nearest " + nearestMatch + " among valid values " + Arrays.asList(validResults), found); } } }
/** * Visitor function, which looks at each feature and finds the value of the attribute given * attribute nearest to the given comparison value. * * @param feature the feature to be visited */ @SuppressWarnings("unchecked") public void visit(org.opengis.feature.Feature feature) { // bail out immediately if we have already found an exact match if (visited) { return; } if (attributeClass == null) { PropertyDescriptor desc = (PropertyDescriptor) expr.evaluate(feature.getType()); attributeClass = desc.getType().getBinding(); if (accumulator == null) { accumulator = getAccumulator(attributeClass); } } // extract the value Object attribValue = expr.evaluate(feature); if (attribValue == null) { return; } else { visited |= accumulator.visit(attribValue); } }
@Override public Object getDefaultValue( ResourceInfo resource, String dimensionName, DimensionInfo dimension, Class clz) { String attrName = dimension.getAttribute(); Class<?> attrType = String.class; if (resource instanceof FeatureTypeInfo) { List<AttributeTypeInfo> attrTypes; try { attrTypes = ((FeatureTypeInfo) resource).attributes(); for (AttributeTypeInfo attr : attrTypes) { if (attr.getName().equals(attrName)) { attrType = attr.getBinding(); break; } } } catch (IOException e) { } } final FeatureCalc nearest = new NearestVisitor(ff.property(dimension.getAttribute()), this.toMatch); CalcResult res = getCalculatedResult((FeatureTypeInfo) resource, dimension, nearest); if (res.equals(CalcResult.NULL_RESULT)) { return null; } else { return Converters.convert(res.getValue(), clz); } }
private FeatureCalc getFeatureCalcForStrategy( DimensionDefaultValueSelectionStrategy delegateStrategy, DimensionDescriptor dd) { String sa = dd.getStartAttribute(); String ea = dd.getEndAttribute(); if (delegateStrategy instanceof CoverageMaximumValueSelectionStrategyImpl) { if (ea != null) { return new MaxVisitor(ea); } else { return new MaxVisitor(sa); } } else if (delegateStrategy instanceof CoverageMinimumValueSelectionStrategyImpl) { return new MinVisitor(sa); } else if (delegateStrategy instanceof CoverageNearestValueSelectionStrategyImpl) { CoverageNearestValueSelectionStrategyImpl impl = (CoverageNearestValueSelectionStrategyImpl) delegateStrategy; Object targetValue = impl.getTargetValue(); return new NearestVisitor(ff.property(sa), targetValue); } else { throw new ServiceException( "Don't konw how to restrict the domain for strategy " + delegateStrategy); } }