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; } }
List<String> props = new ArrayList(); for (int i = 0; i < schema.getAttributeCount(); i++) { AttributeDescriptor att = schema.getDescriptor(i); if (Feature.class.isAssignableFrom(att.getType().getBinding()) && !query.getJoins().isEmpty()) { continue; props.add(att.getLocalName()); if (schema.getDescriptor(queriedAtts[i]) != null) { allowedAtts.add(queriedAtts[i]); } else {
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; }
public void modifyFeatures(AttributeDescriptor[] type, Object[] values, Filter filter) throws IOException { SimpleFeatureType original = typeMap.getOriginalFeatureType(); // map back attribute types and values to the original values AttributeDescriptor[] originalTypes = new AttributeDescriptor[type.length]; Object[] originalValues = new Object[values.length]; for (int i = 0; i < values.length; i++) { originalTypes[i] = original.getDescriptor(type[i].getName()); if (values[i] != null) { Class<?> target = originalTypes[i].getType().getBinding(); originalValues[i] = Converters.convert(values[i], target); if (originalValues[i] == null) throw new IOException("Could not map back " + values[i] + " to type " + target); } } featureStore() .modifyFeatures(originalTypes, originalValues, store.retypeFilter(filter, typeMap)); }
/** * Recursively scans a SimpleFeature for SimpleFeature attributes in order to build a * "flattened" list of attributes * * @param attributeDescriptors A List of attribute descriptors, populated recursively * @param featuretype The feature type to scan * @param attrAlias An alias for adding as a prefix to the simple attribute names */ private static void scanAttributeDescriptors( List<AttributeDescriptor> attributeDescriptors, SimpleFeatureType featureType, String attrAlias) { List<AttributeDescriptor> descriptors = featureType.getAttributeDescriptors(); for (int i = 0; i < descriptors.size(); i++) { AttributeDescriptor ad = descriptors.get(i); SimpleFeatureType joinedSchema = (SimpleFeatureType) ad.getUserData().get(ContentDataStore.JOINED_FEATURE_TYPE); String name = (attrAlias != null ? attrAlias + "." : "") + ad.getLocalName(); if (joinedSchema != null) { // go forth and harvest feature attribute types scanAttributeDescriptors(attributeDescriptors, joinedSchema, name); } else { // this is a common (non-feature) attribute type AttributeTypeBuilder build = new AttributeTypeBuilder(); build.init(ad); AttributeDescriptor descriptor = build.buildDescriptor(name); attributeDescriptors.add(descriptor); } } }
for (AttributeDescriptor ad : schema.getAttributeDescriptors()) { if (ad instanceof GeometryDescriptor) { GeometryDescriptor gd = (GeometryDescriptor) ad; Class<?> binding = ad.getType().getBinding(); if (Point.class.isAssignableFrom(binding) || GeometryCollection.class.isAssignableFrom(binding)) { + binding.getCanonicalName()); tb.minOccurs(ad.getMinOccurs()); tb.maxOccurs(ad.getMaxOccurs()); tb.nillable(ad.isNillable()); tb.add(ad.getLocalName(), target, gd.getCoordinateReferenceSystem()); tb.setName(schema.getName()); return tb.buildFeatureType();
/** * Checks if the feature type specified is a GridCoverage wrapper * * @param featureType * @return */ public static boolean isWrappedCoverage(SimpleFeatureType featureType) { if (!"GridCoverage".equals(featureType.getName().getLocalPart())) return false; if (featureType.getAttributeCount() != 2) return false; AttributeDescriptor polyDescriptor = featureType.getDescriptor("geom"); if (polyDescriptor == null || !Polygon.class.equals(polyDescriptor.getType().getBinding())) return false; AttributeDescriptor gridDescriptor = featureType.getDescriptor("grid"); if (gridDescriptor == null || !GridCoverage.class.equals(gridDescriptor.getType().getBinding())) return false; return true; }
for (AttributeDescriptor ad : f.getFeatureType().getAttributeDescriptors()) { Object attribute = f.getAttribute(ad.getName()); if (ad instanceof GeometryDescriptor) { Class target = ad.getType().getBinding(); attribute = clipGeometry( (Geometry) attribute, target, ((GeometryDescriptor) ad).getCoordinateReferenceSystem()); if (attribute == null && f.getFeatureType().getGeometryDescriptor() == ad) { fb.reset(); clippedOut = true; break; fb.add(attribute); next = fb.buildFeature(f.getID()); fb.reset();
List<String> retainedAttributes, SimpleFeatureTypeBuilder tb) { for (AttributeDescriptor descriptor : schema.getAttributeDescriptors()) { isInRetainList = retainedAttributes.contains(descriptor.getLocalName()); logger.fine("Checking " + descriptor.getLocalName() + " --> " + isInRetainList); if (!isInRetainList || schema.getGeometryDescriptor() == descriptor) { continue; builder.setName(schema.getName().getLocalPart() + "_" + descriptor.getName()); builder.setNillable(descriptor.isNillable()); builder.setBinding(descriptor.getType().getBinding()); builder.setMinOccurs(descriptor.getMinOccurs()); builder.setMaxOccurs(descriptor.getMaxOccurs()); builder.setDefaultValue(descriptor.getDefaultValue()); builder.setCRS(schema.getCoordinateReferenceSystem()); AttributeDescriptor intersectionDescriptor = builder.buildDescriptor( schema.getName().getLocalPart() + "_" + descriptor.getName(), descriptor.getType()); tb.add(intersectionDescriptor); tb.addBinding(descriptor.getType());
/** * Create a new {@link OracleFeatureTypeMapper} on top of the original featureType provided * * @param featureType * @throws CQLException */ public OracleFeatureTypeMapper(SimpleFeatureType featureType) throws CQLException { wrappedFeatureType = featureType; originalName = featureType.getName(); mappedName = originalName.getLocalPart(); mappedName = remap(mappedName); List<AttributeDescriptor> attributes = featureType.getAttributeDescriptors(); definitions = new LinkedList<Definition>(); definitionsMapping = new HashMap<Name, Definition>(); // Loop over attributes and prepare the definitions for (AttributeDescriptor attribute : attributes) { final String originalAttribute = attribute.getLocalName(); final AttributeType type = attribute.getType(); final Class<?> binding = type.getBinding(); String attributeName = remap(originalAttribute); // Create the definition to map the original attribute to the Oracle specific one final Definition definition = new Definition(originalAttribute, ECQL.toExpression(attributeName), binding); definitions.add(definition); definitionsMapping.put(attribute.getName(), definition); } remapFeatureType(); }
public SimpleFeature next() { SimpleFeature next = (SimpleFeature) delegate.next(); String id = next.getID(); try { for (int i = 0; i < types.length; i++) { final String xpath = types[i].getLocalName(); builder.add(next.getAttribute(xpath)); } return builder.buildFeature(id); } catch (IllegalAttributeException e) { throw new RuntimeException(e); } }
/** * Same as DataUtilities.reType, but without the cloning that uselessly wastes CPU cycles... * * @param featureType * @param feature * @return * @throws IllegalAttributeException */ public static SimpleFeature reType(SimpleFeatureType featureType, SimpleFeature feature) throws IllegalAttributeException { FeatureType origional = feature.getFeatureType(); if (featureType.equals(origional)) { return SimpleFeatureBuilder.copy(feature); } String id = feature.getID(); int numAtts = featureType.getAttributeCount(); Object[] attributes = new Object[numAtts]; for (int i = 0; i < numAtts; i++) { AttributeDescriptor curAttType = featureType.getDescriptor(i); attributes[i] = feature.getAttribute(curAttType.getLocalName()); } return SimpleFeatureBuilder.build(featureType, attributes, id); }
public static DBObject convert(SimpleFeatureType ft) { DBObject ftDBO = new BasicDBObject(KEY_typeName, ft.getTypeName()); Map<String, String> ftUserData = typeCheck(ft.getUserData()); if (!ftUserData.isEmpty()) { ftDBO.put(KEY_userData, new BasicDBObject(ftUserData)); for (AttributeDescriptor ad : ft.getAttributeDescriptors()) { String adLocalName = ad.getLocalName(); DBObject adDBO = new BasicDBObject(KEY_localName, adLocalName); if (!adLocalName.equals(gdLocalName)) { Object dv = ad.getDefaultValue(); if (dv != null) { adDBO.put(KEY_defaultValue, dv); adDBO.put(KEY_minOccurs, ad.getMinOccurs()); adDBO.put(KEY_maxOccurs, ad.getMaxOccurs()); ad instanceof GeometryDescriptor ? Geometry.class : ad.getType().getBinding(); adDBO.put(KEY_type, new BasicDBObject(KEY_binding, binding.getName())); Map<String, String> adUserData = typeCheck(ad.getUserData()); if (!adUserData.isEmpty()) { adDBO.put(KEY_userData, new BasicDBObject(adUserData));
@Override public SimpleFeature next() throws IOException { SimpleFeature f = wrapped.next(); for (AttributeDescriptor ad : target.getAttributeDescriptors()) { Expression ex = transformer.getExpression(ad.getLocalName()); if (ex != null) { Object value = ex.evaluate(f, ad.getType().getBinding()); fb.add(value); } else { fb.add(null); } } return fb.buildFeature(transformer.transformFid(f)); }
public static SimpleFeature feature(SimpleFeatureType fType, String fid, Node node) throws Exception { SimpleFeatureBuilder b = new SimpleFeatureBuilder(fType); int attributeCount = fType.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { AttributeDescriptor att = fType.getDescriptor(i); AttributeType attType = att.getType(); Object attValue = node.getChildValue(att.getLocalName()); if ((attValue != null) && !attType.getBinding().isAssignableFrom(attValue.getClass())) { // type mismatch, to try convert Object converted = Converters.convert(attValue, attType.getBinding()); if (converted != null) { attValue = converted; } } b.add(attValue); } // create the feature return b.buildFeature(fid); }
/** * Copies an existing feature, retyping it in the process. * * <p>Be warned, this method will create its own SimpleFeatureBuilder, which will trigger a scan * of the SPI looking for the current default feature factory, which is expensive and has * scalability issues. * * <p>If you need good performance consider using {@link * SimpleFeatureBuilder#retype(SimpleFeature, SimpleFeatureBuilder)} instead. * * <p>If the feature type contains attributes in which the original feature does not have a * value for, the value in the resulting feature is set to <code>null</code>. * * @param feature The original feature. * @param featureType The target feature type. * @return The copied feature, with a new type. */ public static SimpleFeature retype(SimpleFeature feature, SimpleFeatureType featureType) { SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType); for (AttributeDescriptor att : featureType.getAttributeDescriptors()) { Object value = feature.getAttribute(att.getName()); builder.set(att.getName(), value); } return builder.buildFeature(feature.getID()); }
/** * Remaps a SimpleFeature, using the given mappings (oldname -> mappedname). The builder uses * the mapped schema. * * @param source * @param attributeMappings * @param builder * @return */ static SimpleFeature remap( SimpleFeature source, Map<String, String> attributeMappings, SimpleFeatureBuilder builder) { SimpleFeatureType target = builder.getFeatureType(); for (int i = 0; i < target.getAttributeCount(); i++) { AttributeDescriptor attributeType = target.getDescriptor(i); Object value = null; String mappedName = attributeMappings.get(attributeType.getLocalName()); if (source.getFeatureType().getDescriptor(mappedName) != null) { value = source.getAttribute(mappedName); } builder.add(value); } return builder.buildFeature(source.getIdentifier().getID()); }
DataUtilities.createType("cities", "the_geom:Point:srid=4326,name:String"); SimpleFeatureType after = DataUtilities.createSubType(before, new String[] {"the_geom"}); assertEquals(1, after.getAttributeCount()); DataUtilities.createSubType( before, new String[] {"the_geom"}, DefaultGeographicCRS.WGS84, "now", here); assertEquals(here.toString(), after.getName().getNamespaceURI()); assertEquals("now", after.getName().getLocalPart()); assertEquals(DefaultGeographicCRS.WGS84, after.getCoordinateReferenceSystem()); assertEquals(1, after.getAttributeCount()); assertEquals("the_geom", after.getDescriptor(0).getLocalName()); assertNotNull(after.getGeometryDescriptor());
public void testFixture() throws Exception { SimpleFeatureType type = DataUtilities.createType( "namespace.typename", "name:String,id:0,geom:MultiLineString"); assertEquals("namespace", "namespace", type.getName().getNamespaceURI()); assertEquals("typename", "typename", type.getTypeName()); assertEquals("attributes", 3, type.getAttributeCount()); assertEquals("a1", "name", type.getDescriptor(0).getLocalName()); assertEquals("a1", String.class, type.getDescriptor(0).getType().getBinding()); assertEquals("a2", "id", type.getDescriptor(1).getLocalName()); assertEquals("a2", Integer.class, type.getDescriptor(1).getType().getBinding()); assertEquals("a3", "geom", type.getDescriptor(2).getLocalName()); assertEquals("a3", MultiLineString.class, type.getDescriptor(2).getType().getBinding()); }
/** * Copies an existing feature, retyping it in the process. * * <p>If the feature type contains attributes in which the original feature does not have a * value for, the value in the resulting feature is set to <code>null</code>. * * @param feature The original feature. * @param SimpleFeatureBuilder A builder for the target feature type * @return The copied feature, with a new type. * @since 2.5.3 */ public static SimpleFeature retype(SimpleFeature feature, SimpleFeatureBuilder builder) { builder.reset(); for (AttributeDescriptor att : builder.getFeatureType().getAttributeDescriptors()) { Object value = feature.getAttribute(att.getName()); builder.set(att.getName(), value); } return builder.buildFeature(feature.getID()); }