static SimpleFeatureType makeContourSchema() { /* Create the output feature schema. */ SimpleFeatureTypeBuilder tbuilder = new SimpleFeatureTypeBuilder(); tbuilder.setName("contours"); tbuilder.setCRS(DefaultGeographicCRS.WGS84); // Do not use "geom" or "geometry" below, it seems to broke shapefile generation tbuilder.add("the_geom", MultiPolygon.class); tbuilder.add("time", Integer.class); // TODO change to something more descriptive and lowercase return tbuilder.buildFeatureType(); }
SimpleFeatureType sft = (SimpleFeatureType) ft; SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init(sft); tb.setName(info.getName()); tb.setNamespaceURI(info.getNamespace().getURI()); tb.setAttributes((AttributeDescriptor[]) null); if (info.getAttributes() == null || info.getAttributes().isEmpty()) { ad = handleDescriptor(ad, info); tb.add(ad); tb.add(ad); ft = tb.buildFeatureType();
/** * Flattens a SimpleFeatureCollection that may contain SimpleFeatures as attributes of other * features. * * @param collection The input SimpleFeatureCollection * @return A SimpleFeatureCollection whose features have no SimpleFeature attributes, or the * original one, if no SimpleFeature attributes were found */ public static SimpleFeatureCollection flatten(SimpleFeatureCollection collection) { SimpleFeatureType schema = collection.getSchema(); // collect the attributes List<AttributeDescriptor> attributeDescriptors = new ArrayList<AttributeDescriptor>(); scanAttributeDescriptors(attributeDescriptors, schema, null); // build the flattened feature type SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(schema.getName()); for (AttributeDescriptor desc : attributeDescriptors) builder.add(desc); SimpleFeatureType flattenedType = builder.buildFeatureType(); // if the number of attributes is the same, we did not encounter a new attribute if (collection.getSchema().getAttributeCount() == flattenedType.getAttributeCount()) { return collection; } return new FlatteningFeatureCollection(collection, flattenedType); }
/** * * Takes into account eventual joins * * @param query * @return */ public SimpleFeatureType getFeatureType(Query query) { SimpleFeatureType result; if (query.getPropertyNames() != Query.ALL_NAMES) { result = SimpleFeatureTypeBuilder.retype(featureType, query.getPropertyNames()); } else { result = featureType; } // add back the joined features in case of join if (!query.getJoins().isEmpty()) { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init(result); for (Join join : query.getJoins()) { String joinedFeatureAttribute = join.getAlias(); if (joinedFeatureAttribute == null) { joinedFeatureAttribute = join.getTypeName(); } tb.add(joinedFeatureAttribute, SimpleFeature.class); } result = tb.buildFeatureType(); } return result; }
final GeometryFactory gf = new GeometryFactory(pm, 0); final Rectangle2D rect = gridCoverageReader.getOriginalEnvelope() .toRectangle2D(); coord[0] = new Coordinate(rect.getMinX(), rect.getMinY()); coord[1] = new Coordinate(rect.getMaxX(), rect.getMinY()); coord[2] = new Coordinate(rect.getMaxX(), rect.getMaxY()); coord[3] = new Coordinate(rect.getMinX(), rect.getMaxY()); coord[4] = new Coordinate(rect.getMinX(), rect.getMinY()); final LinearRing ring = gf.createLinearRing(coord); final Polygon bounds = new Polygon(ring, null, gf); SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder(getTypeFactory()); ftb.setName("GridCoverage"); ftb.add("geom", Polygon.class, sourceCrs); ftb.add("grid", AbstractGridCoverage2DReader.class); ftb.add("params", GeneralParameterValue[].class); SimpleFeatureType schema = ftb.buildFeatureType(); SimpleFeatureBuilder fb = new SimpleFeatureBuilder(schema, getFeatureFactory()); fb.add(bounds); fb.add(gridCoverageReader); fb.add(params); SimpleFeature feature = fb.buildFeature(null);
@Override protected SimpleFeatureType buildTargetFeatureType() { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); for (AttributeDescriptor ad : delegate.getSchema().getAttributeDescriptors()) { if (ad instanceof GeometryDescriptor) { GeometryDescriptor gd = (GeometryDescriptor) ad; Class<?> binding = ad.getType().getBinding(); if (Point.class.isAssignableFrom(binding)) { tb.add(ad); } else { tb.minOccurs(ad.getMinOccurs()); tb.maxOccurs(ad.getMaxOccurs()); tb.nillable(ad.isNillable()); tb.add(ad.getLocalName(), Point.class, gd.getCoordinateReferenceSystem()); } } else { tb.add(ad); } } tb.setName(delegate.getSchema().getName()); return tb.buildFeatureType(); }
@Test public void testShapeFileWriterWithSelfCreatedContent() throws IOException { String outFile = utils.getOutputDirectory() + "/test.shp"; SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); b.setName("EvacuationArea"); b.setCRS(DefaultGeographicCRS.WGS84); b.add("the_geom", MultiPolygon.class); b.add("name", String.class); SimpleFeatureType ft = b.buildFeatureType(); GeometryFactory geofac = new GeometryFactory(); LinearRing lr = geofac.createLinearRing(new Coordinate[]{new Coordinate(0,0),new Coordinate(0,1),new Coordinate(1,1),new Coordinate(0,0)}); Polygon p = geofac.createPolygon(lr,null); MultiPolygon mp = geofac.createMultiPolygon(new Polygon[]{p}); Collection<SimpleFeature> features = new ArrayList<SimpleFeature>(); features.add(SimpleFeatureBuilder.build(ft, new Object[]{mp,"test_name"},"fid"));
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(featureType.getName()); builder.setAbstract(featureType.isAbstract()); builder.setDescription(featureType.getDescription()); if (featureType.getSuper() instanceof SimpleFeatureType) { builder.setSuperType((SimpleFeatureType) featureType.getSuper()); List<AttributeDescriptor> attributes = featureType.getAttributeDescriptors(); for (AttributeDescriptor descriptor : attributes) { if (descriptor instanceof GeometryDescriptor) { String name = descriptor.getLocalName(); Class<?> binding = descriptor.getType().getBinding(); builder.add(name, binding, crs); } else { builder.add(descriptor); builder.setDefaultGeometry(defaultGeometry.getLocalName()); featureType = builder.buildFeatureType();
protected void setUp() throws Exception { SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.setName("test"); typeBuilder.setNamespaceURI("http://www.geotools.org/test"); typeBuilder.add("foo", Integer.class); typeBuilder.add("bar", Double.class); typeBuilder.add(COMPLEX_PROPERTY, Double.class); type = (SimpleFeatureType) typeBuilder.buildFeatureType(); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type); builder.add(Integer.valueOf(1)); builder.add(new Double(2.0)); builder.add(new Double(3.0)); feature = (SimpleFeature) builder.buildFeature("fid"); accessor = SimpleFeaturePropertyAccessorFactory.ATTRIBUTE_ACCESS; }
public ZonalStatisticsCollection( SimpleFeatureCollection data, String dataAttribute, SimpleFeatureCollection zones) { super(zones); this.dataAttribute = dataAttribute; this.data = data; SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); for (AttributeDescriptor att : zones.getSchema().getAttributeDescriptors()) { tb.minOccurs(att.getMinOccurs()); tb.maxOccurs(att.getMaxOccurs()); tb.restrictions(att.getType().getRestrictions()); if (att instanceof GeometryDescriptor) { GeometryDescriptor gatt = (GeometryDescriptor) att; tb.crs(gatt.getCoordinateReferenceSystem()); } tb.add("z_" + att.getLocalName(), att.getType().getBinding()); } tb.add("count", Long.class); tb.add("min", Double.class); tb.add("max", Double.class); tb.add("sum", Double.class); tb.add("avg", Double.class); tb.add("stddev", Double.class); tb.setName(zones.getSchema().getName()); targetSchema = tb.buildFeatureType(); }
public void testBounds() throws Exception { PrecisionModel pm = new PrecisionModel(); Geometry[] g = new Geometry[4]; GeometryFactory gf = new GeometryFactory(pm); g[0] = gf.createPoint(new Coordinate(0, 0)); g[1] = gf.createPoint(new Coordinate(0, 10)); g[2] = gf.createPoint(new Coordinate(10, 0)); g[3] = gf.createPoint(new Coordinate(10, 10)); GeometryCollection gc = gf.createGeometryCollection(g); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("bounds"); tb.setCRS(null); tb.add("p1", Point.class); SimpleFeatureType t = tb.buildFeatureType(); SimpleFeatureCollection fc = FeatureCollections.newCollection(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(t); for (int i = 0; i < g.length; i++) { b.add(g[i]); fc.add(b.buildFeature(null)); } assertEquals(gc.getEnvelopeInternal(), fc.getBounds()); }
@Test public void testCreateWithCorrelativeData() throws Exception { SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder(); ftb.setName("Track"); ftb.add("point", Point.class); ftb.add("data1", Double.class); ftb.add("data2", Double.class); ftb.setDefaultGeometry("point"); SimpleFeatureType ft = ftb.buildFeatureType(); GeometryFactory gf = new GeometryFactory(); SimpleFeatureBuilder sbb = new SimpleFeatureBuilder(ft); track.getFeatureCollection().add(sbb.buildFeature("id1", new Object[]{ gf.createPoint(new Coordinate(0, 0)), 1.2, 2.3 })); track.getFeatureCollection().add(sbb.buildFeature("id2", new Object[]{ gf.createPoint(new Coordinate(3, 0)), 3.4, 4.5 })); track.getFeatureCollection().add(sbb.buildFeature("id3", new Object[]{ gf.createPoint(new Coordinate(0, 3)), 5.6, 6.7
/** * Create the modified feature type. * * @param sourceFeatureType the source feature type * @return the modified feature type * @throws ProcessException errror */ private SimpleFeatureType createTargetFeatureType(FeatureType sourceFeatureType) throws ProcessException { try { SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.setName(sourceFeatureType.getName().getLocalPart()); typeBuilder.setNamespaceURI(sourceFeatureType.getName().getNamespaceURI()); AttributeDescriptor geomAttbType = sourceFeatureType.getGeometryDescriptor(); for (PropertyDescriptor attbType : sourceFeatureType.getDescriptors()) { if (attbType.equals(geomAttbType)) { typeBuilder.add(geomAttbType.getLocalName(), Point.class); } else { typeBuilder.add((AttributeDescriptor) attbType); } } typeBuilder.setDefaultGeometry( sourceFeatureType.getGeometryDescriptor().getLocalName()); return typeBuilder.buildFeatureType(); } catch (Exception e) { LOGGER.warning("Error creating type: " + e); throw new ProcessException("Error creating type: " + e, e); } }
List<AttributeDescriptor> atts = features.getSchema().getAttributeDescriptors(); for (int i = 0; i < atts.size(); i++) { if (atts.get(i).getLocalName().equals(attribute)) { attIndex = i; break; List uniqueValues = visitor.getResult().toList(); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.add("value", features.getSchema().getDescriptor(attIndex).getType().getBinding()); tb.setName("UniqueValue"); SimpleFeatureType ft = tb.buildFeatureType(); SimpleFeatureBuilder fb = new SimpleFeatureBuilder(ft); fb.add(value); result.add(fb.buildFeature(null));
@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(); }
CoordinateReferenceSystem incomingCRS = incoming.getCoordinateReferenceSystem(); if (incomingCRS == null) { incoming.getGeometryDescriptor().getCoordinateReferenceSystem(); SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.init(incoming); builder.setName(name(incoming.getTypeName())); incoming = builder.buildFeatureType(); String gdName = incoming.getGeometryDescriptor().getLocalName(); for (AttributeDescriptor ad : incoming.getAttributeDescriptors()) { String adName = ad.getLocalName(); if (gdName.equals(adName)) { ad.getUserData().put(KEY_mapping, geometryMapping); ad.getUserData().put(KEY_encoding, "GeoJSON"); } else { ad.getUserData().put(KEY_mapping, "properties." + adName);
SimpleFeature createFeatureSized(int sizeNum) { SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder(); ftb.setName("geotools"); ftb.add("size", Integer.class); SimpleFeatureBuilder fb = new SimpleFeatureBuilder(ftb.buildFeatureType()); fb.set("size", sizeNum); SimpleFeature feature = fb.buildFeature(null); return feature; } }
@Test public void testImportAllWithDifferentFeatureTypesAndDestPathAndAdd() throws Exception { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setCRS(CRS.decode("EPSG:4326")); builder.add("geom", Point.class); builder.add("label", String.class); builder.setName("dest"); SimpleFeatureType type = builder.buildFeatureType(); GeometryFactory gf = new GeometryFactory(); SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(0, 0)), "feature0" }, "feature");
if (storeWriter == null) { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); for (AttributeDescriptor d : original.getAttributeDescriptors()) { if (Geometry.class.isAssignableFrom(d.getType().getBinding()) && multiWriter) { GeometryDescriptor gd = (GeometryDescriptor) d; builder.add(gd.getLocalName(), target, gd.getCoordinateReferenceSystem()); builder.setDefaultGeometry(gd.getLocalName()); } else { builder.add(d); builder.setNamespaceURI(original.getName().getURI()); fileName = getShapeName(original, null); builder.setName(fileName); SimpleFeatureType retyped = builder.buildFeatureType();
/** * Gets a new schema, built remapping attribute names via the attributeMappings map. * * @param schema * @return */ private SimpleFeatureType remapSchema(SimpleFeatureType schema) { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(schema.getName()); for (AttributeDescriptor attDesc : schema.getAttributeDescriptors()) { if (attDesc instanceof GeometryDescriptor) { GeometryDescriptor geoDesc = (GeometryDescriptor) attDesc; builder.add( attributesMapping.get(attDesc.getLocalName()), attDesc.getType().getBinding(), geoDesc.getCoordinateReferenceSystem()); } else { List<Filter> filters = attDesc.getType().getRestrictions(); if (filters != null && !filters.isEmpty()) { builder.restrictions(filters); } builder.add( attributesMapping.get(attDesc.getLocalName()), attDesc.getType().getBinding()); } } return builder.buildFeatureType(); }