/** * 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); } } }
b.init(old); b.setCRS(getCRS(info.getSRS())); ad = b.buildDescriptor(old.getLocalName());
/** * Builds a geometry descriptor specifying its attribute type. * * <p>Internal state is reset after the descriptor is built. * * @param name The name of the descriptor. * @param type The geometry type referenced by the descriptor. */ public GeometryDescriptor buildDescriptor(String name, GeometryType type) { return buildDescriptor(new NameImpl(name), type); }
/** * Replace the descriptor at the provided index. * * @param index * @param descriptor */ public void set(String attributeName, AttributeTypeBuilder attributeBuilder) { AttributeDescriptor descriptor = attributeBuilder.buildDescriptor(attributeName); set(attributeName, descriptor); }
/** * Builds an attribute descriptor specifying its attribute type. * * <p>Internal state is reset after the descriptor is built. * * @param name The name of the descriptor. * @param type The type referenced by the descriptor. */ public AttributeDescriptor buildDescriptor(String name, AttributeType type) { return buildDescriptor(new NameImpl(name), type); }
/** * Builds an attribute descriptor first building an attribute type from internal state. * * <p>If {@link #crs} has been set via {@link #setCRS(CoordinateReferenceSystem)} the internal * attribute type will be built via {@link #buildGeometryType()}, otherwise it will be built via * {@link #buildType()}. * * <p>This method calls through to {@link #buildDescriptor(String, AttributeType)}. * * @param name The name of the descriptor. * @see #buildDescriptor(String, AttributeType) */ public AttributeDescriptor buildDescriptor(String name) { setName(name); if (binding == null) throw new IllegalStateException("No binding has been provided for this attribute"); if (crs != null || Geometry.class.isAssignableFrom(binding)) { return buildDescriptor(name, buildGeometryType()); } else { return buildDescriptor(name, buildType()); } }
public static AttributeDescriptor[] createType1Choices() { AttributeTypeBuilder ab = new AttributeTypeBuilder(); AttributeDescriptor[] choices = new AttributeDescriptor[3]; ab.setBinding(Byte.class); choices[0] = ab.buildDescriptor("testByte"); ab.setBinding(Double.class); choices[1] = ab.buildDescriptor("testDouble"); ab.setBinding(String.class); choices[2] = ab.buildDescriptor("testString"); return choices; }
/** * Adds a new geometric attribute w/ provided name, class, and coordinate reference system. * * <p>The <tt>crs</tt> parameter may be <code>null</code>. * * @param name The name of the attribute. * @param binding The class that the attribute is bound to. * @param crs The crs of of the geometry, may be <code>null</code>. */ public void add(String name, Class<?> binding, CoordinateReferenceSystem crs) { attributeBuilder.setBinding(binding); attributeBuilder.setName(name); attributeBuilder.setCRS(crs); GeometryType type = attributeBuilder.buildGeometryType(); GeometryDescriptor descriptor = attributeBuilder.buildDescriptor(name, type); attributes().add(descriptor); }
public static AttributeDescriptor getChoiceAttrType2() { AttributeTypeBuilder ab = new AttributeTypeBuilder(); AttributeDescriptor[] choices = new AttributeDescriptor[2]; ab.setBinding(String.class); choices[0] = ab.buildDescriptor("testString"); ab.setBinding(Integer.class); choices[1] = ab.buildDescriptor("testInt"); return createChoiceAttrType("choiceTest2", choices); }
static AttributeDescriptor newAtt(String name, Class c) { AttributeTypeBuilder ab = new AttributeTypeBuilder(); ab.setBinding(c); return ab.buildDescriptor(name); }
static AttributeDescriptor newAtt(String name, Class c, boolean nillable) { AttributeTypeBuilder ab = new AttributeTypeBuilder(); ab.setNillable(nillable); ab.setBinding(c); return ab.buildDescriptor(name); }
/** * Copys a feature type. * * <p>This method does a deep copy in that all individual attributes are copied as well. */ public static SimpleFeatureType copy(SimpleFeatureType original) { SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); // initialize the builder b.init(original); // clear attributes b.attributes().clear(); // add attributes in order for (AttributeDescriptor descriptor : original.getAttributeDescriptors()) { AttributeTypeBuilder ab = new AttributeTypeBuilder(b.factory); ab.init(descriptor); b.add(ab.buildDescriptor(descriptor.getLocalName())); } return b.buildFeatureType(); } }
/** * Create a SimpleFeatureType with the same content; just updating the geometry attribute to * match the provided coordinate reference system. * * @param original SimpleFeatureType * @param crs CoordinateReferenceSystem of result * @return SimpleFeatureType updated with the provided CoordinateReferenceSystem */ public static SimpleFeatureType retype( SimpleFeatureType original, CoordinateReferenceSystem crs) { SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); // initialize the builder b.init(original); // clear the attributes b.attributes().clear(); // add attributes in order for (AttributeDescriptor descriptor : original.getAttributeDescriptors()) { if (descriptor instanceof GeometryDescriptor) { GeometryDescriptor geometryDescriptor = (GeometryDescriptor) descriptor; AttributeTypeBuilder adjust = new AttributeTypeBuilder(b.factory); adjust.init(geometryDescriptor); adjust.setCRS(crs); b.add(adjust.buildDescriptor(geometryDescriptor.getLocalName())); continue; } b.add(descriptor); } return b.buildFeatureType(); }
@Test public void testCustomLengthExpressions() { AttributeTypeBuilder builder = new AttributeTypeBuilder(); FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); Function length = ff.function("LengthFunction", new Expression[] {ff.property(".")}); // strict less builder.addRestriction(ff.less(length, ff.literal(20))); builder.setBinding(String.class); AttributeDescriptor attribute = builder.buildDescriptor("attribute"); assertEquals(19, FeatureTypes.getFieldLength(attribute)); // flip expression builder.addRestriction(ff.greater(ff.literal(20), length)); builder.setBinding(String.class); attribute = builder.buildDescriptor("attribute"); assertEquals(19, FeatureTypes.getFieldLength(attribute)); }
builder.setCRS(schema.getCoordinateReferenceSystem()); AttributeDescriptor intersectionDescriptor = builder.buildDescriptor( schema.getName().getLocalPart() + "_" + descriptor.getName(), descriptor.getType());
@Override protected SimpleFeatureType buildTargetFeatureType() { // create schema SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); for (AttributeDescriptor descriptor : delegate.getSchema().getAttributeDescriptors()) { if (!(descriptor.getType() instanceof GeometryTypeImpl) || (!delegate.getSchema().getGeometryDescriptor().equals(descriptor))) { tb.add(descriptor); } else { AttributeTypeBuilder builder = new AttributeTypeBuilder(); builder.setBinding(MultiPolygon.class); AttributeDescriptor attributeDescriptor = builder.buildDescriptor(descriptor.getLocalName(), builder.buildType()); tb.add(attributeDescriptor); if (tb.getDefaultGeometry() == null) { tb.setDefaultGeometry(descriptor.getLocalName()); } } } tb.setDescription(delegate.getSchema().getDescription()); tb.setCRS(delegate.getSchema().getCoordinateReferenceSystem()); tb.setName(delegate.getSchema().getName()); return tb.buildFeatureType(); }
static AttributeDescriptor getIntersectionType( SimpleFeatureCollection first, SimpleFeatureCollection second) { Class firstGeomType = first.getSchema().getGeometryDescriptor().getType().getBinding(); Class secondGeomType = second.getSchema().getGeometryDescriptor().getType().getBinding(); // figure out the output geometry type Class binding; if (isGeometryTypeIn(secondGeomType, Point.class)) { binding = Point.class; } else if (isGeometryTypeIn(secondGeomType, MultiPoint.class)) { binding = MultiPoint.class; } else if (isGeometryTypeIn(secondGeomType, LineString.class, MultiLineString.class)) { binding = MultiLineString.class; } else if (isGeometryTypeIn(secondGeomType, Polygon.class, MultiPolygon.class)) { if (isGeometryTypeIn(firstGeomType, Polygon.class, MultiPolygon.class)) { binding = MultiPolygon.class; } else { binding = MultiLineString.class; } } else { // we can't be more precise than this binding = Geometry.class; } AttributeTypeBuilder builder = new AttributeTypeBuilder(); builder.setName("the_geom"); builder.setBinding(binding); builder.setCRS(first.features().next().getFeatureType().getCoordinateReferenceSystem()); AttributeDescriptor descriptor = builder.buildDescriptor("the_geom"); return descriptor; }
public void testModifyMadeUpGeometry() throws IOException { // GEOT-2371 SimpleFeatureType t = featureStore.getSchema(); GeometryFactory gf = new GeometryFactory(); Point point = gf.createPoint(new Coordinate(-10, 0)); // make up a fake attribute with the same name, something that might happen // in chains of retyping where attributes are rebuilt AttributeTypeBuilder ab = new AttributeTypeBuilder(); ab.binding(Point.class); AttributeDescriptor madeUp = ab.buildDescriptor(aname("geometry")); featureStore.modifyFeatures( new AttributeDescriptor[] {madeUp}, new Object[] {point}, Filter.INCLUDE); SimpleFeatureCollection features = featureStore.getFeatures(); try (SimpleFeatureIterator i = features.features()) { assertTrue(i.hasNext()); while (i.hasNext()) { SimpleFeature feature = (SimpleFeature) i.next(); assertTrue(point.equalsExact((Geometry) feature.getAttribute(aname("geometry")))); } } }
@Override protected void connect() throws Exception { super.connect(); // we need to use the type builder because the pk has min occurs = 1 on Oracle AttributeTypeBuilder atb = new AttributeTypeBuilder(); atb.setMinOccurs(isPkNillable() ? 0 : 1); atb.setMaxOccurs(1); atb.setNillable(isPkNillable()); atb.setName(FID); atb.setBinding(Integer.class); AttributeDescriptor fidDescriptor = atb.buildDescriptor(FID); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setNamespaceURI(dataStore.getNamespaceURI()); tb.setName(LAKESVIEW); tb.add(fidDescriptor); tb.add(ID, Integer.class); tb.add(GEOM, Polygon.class, (CoordinateReferenceSystem) null); tb.add(NAME, String.class); lakeViewSchema = tb.buildFeatureType(); lakeViewPkSchema = tb.retype(lakeViewSchema, new String[] {ID, GEOM, NAME}); }
public void testAttributeBuilder() { FilterFactory ff = CommonFactoryFinder.getFilterFactory(); AttributeTypeBuilder builder = new AttributeTypeBuilder(); builder.binding(Integer.class); builder.minOccurs(1).maxOccurs(1); builder.defaultValue(0); builder.name("percent").description("Percent between 0 and 100"); builder.restriction(ff.greaterOrEqual(ff.property("."), ff.literal(0))) .restriction(ff.lessOrEqual(ff.property("."), ff.literal(100))); final AttributeType PERCENT = builder.buildType(); builder.minOccurs(1).maxOccurs(1); builder.defaultValue(0); builder.name("percent").description("Percent between 0 and 100"); AttributeDescriptor a = builder.buildDescriptor("a", PERCENT); assertSame(a.getType(), PERCENT); assertEquals(a.getDefaultValue(), 0); Filter restrictions = ff.and(PERCENT.getRestrictions()); assertTrue(restrictions.evaluate(50)); assertFalse(restrictions.evaluate(150)); }