@Override public Boolean convert(SimpleFeature feature) { Object value = feature.getAttribute(attributeName); if (value == null || value.equals("")) { return nullIsTrue; } else { return !nullIsTrue; } }
/** * Recursively breaks down SimpleFeatures that may contain other features as attributes to * accumulate simple attribute values to a List * * @param attributeValues The List of attribute values * @param feature A SimpleFeature to harvest attributes */ private void accumulateAttributes(SimpleFeature feature) { for (int i = 0; i < feature.getAttributes().size(); i++) { Object attr = feature.getAttribute(i); if (attr instanceof SimpleFeature) { // go forth and harvest attrubutes accumulateAttributes((SimpleFeature) attr); } else { builder.add(attr); } } } }
@Test public void testQueryWithPropertyNames() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); Query q = new Query(RENAMED, Filter.INCLUDE, new String[] {"ADDRESS"}); FeatureCollection<SimpleFeatureType, SimpleFeature> fc = fs.getFeatures(q); assertEquals(1, fc.getSchema().getAttributeCount()); // make sure the feature schema is good as well FeatureIterator<SimpleFeature> it = fc.features(); SimpleFeature sf = it.next(); it.close(); assertEquals(1, sf.getAttributeCount()); assertNull(sf.getAttribute("FID")); assertNotNull(sf.getAttribute("ADDRESS")); } }
@Override public Boolean convert(SimpleFeature feature) { String key = feature.getAttribute(attributeName).toString(); Boolean value = values.get(key); if (value == null) { value = defaultValue; } return value; } }
protected Alert getNote(SimpleFeature feature) { Alert alert = Alert.createSimpleAlerts("winkki:" + feature.getAttribute("licence_type")); alert.alertDescriptionText = feature.getAttribute("event_description") == null ? new NonLocalizedString("") : new NonLocalizedString(feature.getAttribute("event_description").toString()); alert.effectiveStartDate = feature.getAttribute("licence_startdate") == null ? (Date) feature.getAttribute("event_startdate") : (Date) feature.getAttribute("licence_startdate"); return alert; } }
@Override public P2<StreetTraversalPermission> convert(SimpleFeature feature) { if (attributeName == null) { return defaultPermission; } Object key = feature.getAttribute(attributeName); if (key == null) { return defaultPermission; } P2<StreetTraversalPermission> permission = _permissions.get(key.toString()); if (permission == null) { log.info("unexpected permission " + key.toString()); return defaultPermission; } return permission; } }
@Override public P2<Double> convert(SimpleFeature feature) { Double d = (Double) feature.getAttribute(safetyAttributeName); if (d == null) { return oneone; } return new P2<Double>(d, d); }
@Override public String convert(SimpleFeature feature) { String attr = (String) feature.getAttribute(getAttributeName()); // Since dBase (used in shapefiles) has poor/no null support, null strings are sometimes // stored as a single space " " if (attr == null || attr.equals(" ")) { attr = defaultValue; } return attr; }
@SuppressWarnings({ "unchecked", "deprecation" }) @Override public T convert(SimpleFeature feature) { T value = (T) feature.getAttribute(attributeName); if (value instanceof String && decodeUTF8) { String str = (String) value; //decode UTF-8, irritatingly Charset charset = Charset.forName("UTF-8"); byte[] bytes = new byte[str.length()]; //we have to use a deprecated method because it's the only one that works. str.getBytes(0, str.length(), bytes, 0); ByteBuffer bb = ByteBuffer.wrap(bytes); value = (T) charset.decode(bb).toString(); } return value; }
@Override public P2<Double> convert(SimpleFeature feature) { String safetyKey = feature.getAttribute(safetyAttributeName).toString(); Double safetyFeature = safetyFeatures.get(safetyKey); if (safetyFeature == null) return oneone; int directionFeature = 3; // Default to applying the safety feature in both directions // (useful if the dataset doesn't include direction information) if (directionAttributeName != null) { String directionKey = feature.getAttribute(directionAttributeName).toString(); if (directionKey != null) { directionFeature = directions.get(directionKey.toString()); } } return new P2<Double>((directionFeature & 0x1) == 0 ? 1.0 : safetyFeature, (directionFeature & 0x2) == 0 ? 1.0 : safetyFeature); }
@Override public T convert(SimpleFeature feature) { ensureCached(); String mainKeyValue = toHashableString(feature.getAttribute(this.mainKey)); if (mainKeyValue == null) { log.warn("Feature " + feature.getID() + " has null value for its mainKey (" + mainKey + ")"); return null; } SimpleFeature joinedFeature = cache.get(mainKeyValue); if (joinedFeature == null) { return null; } else { return converter.convert(joinedFeature); } }
@Override public String resolve(double x, double y) { System.out.println("x="+x+", y="+y); FeatureIterator<Feature> iterator = collection.features(); while( iterator.hasNext() ){ SimpleFeature feature = (SimpleFeature) iterator.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(x, y); Point point = geometryFactory.createPoint(coord); //System.out.println("checking "+point.toString()); if(geom.contains(point)) { return feature.getAttribute(this.nameField).toString(); } } return null; }
public void write() throws IOException { try { SimpleFeatureType target = getFeatureType(); for (int i = 0; i < target.getAttributeCount(); i++) { AttributeDescriptor at = target.getDescriptor(i); Object value = retyped.getAttribute(i); current.setAttribute(at.getLocalName(), value); } delegate.write(); } catch (IllegalAttributeException e) { throw (IOException) new IOException("Error occurred while retyping feature").initCause(e); } } }
/** We have to cache all the features in the supplemental file, because * if we try to load them on the fly, GeoTools wigs out. */ private void ensureCached() { if (cache != null) { return; } cache = new HashMap<String, SimpleFeature>(); try { FeatureCollection<SimpleFeatureType, SimpleFeature> features = joinedSource .getFeatures(); FeatureIterator<SimpleFeature> it = features.features(); while (it.hasNext()) { SimpleFeature feature = it.next(); String joinedKeyValue = toHashableString(feature.getAttribute(joinedKey)); if (joinedKeyValue != null) { cache.put(joinedKeyValue, feature); } else { log.warn("Feature " + feature.getID() + " has null value for its joinedKey (" + joinedKey + ")"); } } it.close(); } catch (IOException e) { throw new RuntimeException("Could not cache values for joined shapefile", e); } }
public SimpleFeature next() throws NoSuchElementException { final SimpleFeature next = delegate.next(); // check all write protected attributes are null final SimpleFeatureType featureType = next.getFeatureType(); for (AttributeDescriptor att : featureType.getAttributeDescriptors()) { String name = att.getLocalName(); if (!writableAttributes.contains(name)) { Object value = next.getAttribute(name); if (value != null) { String typeName = getSchema().getName().getLocalPart(); if (response == Response.CHALLENGE) { throw SecureCatalogImpl.unauthorizedAccess(typeName); } else { throw new UnsupportedOperationException( "Trying to write on the write protected attribute " + name); } } } } return next; } }
static SimpleFeature retype(SimpleFeature source, SimpleFeatureBuilder builder) throws IllegalAttributeException { SimpleFeatureType target = builder.getFeatureType(); for (int i = 0; i < target.getAttributeCount(); i++) { AttributeDescriptor attributeType = target.getDescriptor(i); Object value = null; if (source.getFeatureType().getDescriptor(attributeType.getName()) != null) { value = source.getAttribute(attributeType.getName()); } builder.add(value); } FeatureId id = reTypeId(source.getIdentifier(), source.getFeatureType(), target); SimpleFeature retyped = builder.buildFeature(id.getID()); retyped.getUserData().putAll(source.getUserData()); return retyped; }
@Test public void testModify() throws Exception { final Query queryAll = new Query(RENAMED); SimpleFeatureStore store; store = (SimpleFeatureStore) rts.getFeatureSource(RENAMED); SimpleFeature original = store.getFeatures(fidFilter).features().next(); String newAddress = ((String) original.getAttribute("ADDRESS")) + " xxx"; store.modifyFeatures( original.getFeatureType().getDescriptor("ADDRESS"), newAddress, fidFilter); SimpleFeature modified = store.getFeatures(fidFilter).features().next(); assertEquals(newAddress, modified.getAttribute("ADDRESS")); }
@Test public void testGetFeaturesFeatureSource() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); assertEquals(primitive, fs.getSchema()); SimpleFeatureCollection fc = fs.getFeatures(); assertEquals(primitive, fc.getSchema()); assertTrue(fc.size() > 0); // make sure the feature schema is good as well FeatureIterator<SimpleFeature> it = fc.features(); SimpleFeature sf = it.next(); it.close(); assertEquals(primitive, sf.getFeatureType()); // check the feature ids have been renamed as well assertTrue( "Feature id has not been renamed, it's still " + sf.getID(), sf.getID().startsWith(RENAMED)); // check mappings occurred assertEquals("description-f001", sf.getAttribute("description")); assertTrue( new WKTReader() .read("MULTIPOINT(39.73245 2.00342)") .equalsExact((Geometry) sf.getAttribute("pointProperty"))); assertEquals(Long.valueOf(155), sf.getAttribute("intProperty")); assertNull(sf.getAttribute("newProperty")); }
@Test public void testModify() throws Exception { final Query queryAll = new Query(RENAMED); SimpleFeatureStore store; store = (SimpleFeatureStore) rts.getFeatureSource(RENAMED); SimpleFeature original = store.getFeatures(fidFilter).features().next(); // test a non mapped attribute String newDescription = ((String) original.getAttribute("description")) + " xxx"; store.modifyFeatures( original.getFeatureType().getDescriptor("description"), newDescription, fidFilter); SimpleFeature modified = store.getFeatures(fidFilter).features().next(); assertEquals(newDescription, modified.getAttribute("description")); // test a mapped attribute MultiPoint mpo = (MultiPoint) original.getAttribute("pointProperty"); MultiPoint mpm = mpo.getFactory().createMultiPoint(new Coordinate[] {new Coordinate(10, 12)}); store.modifyFeatures( original.getFeatureType().getDescriptor("pointProperty"), mpm, fidFilter); modified = store.getFeatures(fidFilter).features().next(); assertTrue(mpm.equalsExact((Geometry) modified.getAttribute("pointProperty"))); }
assertEquals("114", f.getAttribute("FID")); assertEquals("215 Main Street", f.getAttribute("ADDRESS"));