Search in sources :

Example 46 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project sldeditor by robward-scisys.

the class InLineFeatureModel method addNewColumn.

/**
 * Adds the new column.
 */
public void addNewColumn() {
    if (featureCollection != null) {
        String attributeName = getUniqueAttributeName();
        columnList.add(attributeName);
        // Populate field names
        SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
        featureTypeBuilder.init(featureCollection.getSchema());
        featureTypeBuilder.add(attributeName, String.class);
        SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        try {
            SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
            ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
            SimpleFeatureIterator it = featureSource.getFeatures().features();
            try {
                while (it.hasNext()) {
                    SimpleFeature sf = it.next();
                    sfb.addAll(sf.getAttributes());
                    sfb.add(new String(""));
                    featureList.add(sfb.buildFeature(null));
                }
            } finally {
                it.close();
            }
            SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
            featureCollection = collection;
            cachedFeature = null;
            lastRow = -1;
            DataStore dataStore = DataUtilities.dataStore(collection);
            userLayer.setInlineFeatureDatastore(dataStore);
            userLayer.setInlineFeatureType(newFeatureType);
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        this.fireTableStructureChanged();
        this.fireTableDataChanged();
        if (parentObj != null) {
            parentObj.inlineFeatureUpdated();
        }
    }
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 47 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project incubator-rya by apache.

the class GeoWaveGTQueryTest method buildSimpleFeature.

private static SimpleFeature buildSimpleFeature(final String locationName, final Coordinate coordinate) {
    final SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getPointSimpleFeatureType());
    builder.set("locationName", locationName);
    builder.set("geometry", GeometryUtils.GEOMETRY_FACTORY.createPoint(coordinate));
    return builder.buildFeature(locationName);
}
Also used : SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 48 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project polymap4-core by Polymap4.

the class RFeatureStoreTests method testCreateSimpleSchemaAndFeature.

public void testCreateSimpleSchemaAndFeature() throws Exception {
    log.debug("creating schema...");
    SimpleFeatureType schema = createSimpleSchema();
    ds.createSchema(schema);
    RFeatureStore fs = (RFeatureStore) ds.getFeatureSource(schema.getName());
    assertEquals(0, Iterables.size(iterable(fs.getFeatures())));
    // add feature
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder(schema);
    Point point = new GeometryBuilder().point(10, 100);
    fb.set("name", "value");
    fb.set("geom", point);
    DefaultFeatureCollection features = new DefaultFeatureCollection();
    features.add(fb.buildFeature(null));
    fs.addFeatures(features);
    // check size
    assertEquals(1, Iterables.size(iterable(fs.getFeatures())));
    // check properties
    fs.getFeatures().accepts(new FeatureVisitor() {

        public void visit(Feature feature) {
            log.debug("Feature: " + feature);
            assertEquals("value", ((SimpleFeature) feature).getAttribute("name"));
            assertEquals(point, ((SimpleFeature) feature).getAttribute("geom"));
            assertEquals(point, ((SimpleFeature) feature).getDefaultGeometry());
        }
    }, null);
    // modify property
    Feature feature = Iterables.getOnlyElement(iterable(fs.getFeatures()));
    fs.modifyFeatures((AttributeDescriptor) feature.getProperty("name").getDescriptor(), "changed", ff.id(Collections.singleton(feature.getIdentifier())));
    Feature feature2 = Iterables.getOnlyElement(iterable(fs.getFeatures()));
    assertEquals("changed", ((SimpleFeature) feature2).getAttribute("name"));
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureVisitor(org.opengis.feature.FeatureVisitor) Point(com.vividsolutions.jts.geom.Point) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 49 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project polymap4-core by Polymap4.

the class FeatureRenameProcessor method featuresResponse.

@Produces(GetFeaturesResponse.class)
public void featuresResponse(GetFeaturesResponse response, ProcessorContext context) throws Exception {
    assert schema != null : "Target schema is not yet initialized. Call getSchema() first.";
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) schema);
    // log.debug( "       received features: " + chunk.count() );
    List<Feature> result = new ArrayList(response.count());
    for (Feature feature : response) {
        for (PropertyDescriptor prop : schema.getDescriptors()) {
            Property featureProp = feature.getProperty(prop.getName());
            // the feature may not contain the property if it was not requested
            if (featureProp != null) {
                builder.set(prop.getName(), featureProp.getValue());
            }
        }
        result.add(builder.buildFeature(feature.getIdentifier().getID()));
    }
    // log.debug( "       sending features: " + result.size() );
    context.sendResponse(new GetFeaturesResponse(result));
}
Also used : GetFeaturesResponse(org.polymap.core.data.feature.GetFeaturesResponse) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Produces(org.polymap.core.data.pipeline.Produces)

Example 50 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExport method writeToShapefile.

public boolean writeToShapefile() throws MalformedURLException, IOException {
    boolean success = false;
    // SimpleFeatureIterator features = simpleFeatureCollection.features();
    SimpleFeatureType type = buildFeatureType();
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
    File shpFile = checkAndCreateFile();
    Map datastoreConfig = new HashMap<>();
    datastoreConfig.put("url", shpFile.toURI().toURL());
    ShapefileDataStore shpfileDataStore = (ShapefileDataStore) factory.createNewDataStore(datastoreConfig);
    shpfileDataStore.createSchema(type);
    shpfileDataStore.forceSchemaCRS(this.crs);
    // DataStore dataStore = factory.createNewDataStore(datastoreConfig);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) shpfileDataStore.getFeatureSource(type.getName());
    Transaction t = new DefaultTransaction();
    SimpleFeatureIterator fi = null;
    try {
        // Copied directly from Import process
        featureStore.setTransaction(t);
        fi = simpleFeatureCollection.features();
        SimpleFeatureBuilder fb = new SimpleFeatureBuilder(type);
        while (fi.hasNext()) {
            SimpleFeature source = fi.next();
            fb.reset();
            for (AttributeDescriptor desc : type.getAttributeDescriptors()) {
                Name attributeName = desc.getName();
                Object attributeValue = source.getAttribute(attributeName);
                if (null == attributeValue) {
                    attributeValue = NULL_PLACEHOLDER;
                }
                fb.set(attributeName, attributeValue);
            }
            SimpleFeature target = fb.buildFeature(null);
            featureStore.addFeatures(DataUtilities.collection(target));
        }
        // successful if it made it this far
        success = true;
    } finally {
        t.commit();
        t.close();
        IOUtils.closeQuietly(fi);
    }
    return success;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Name(org.opengis.feature.type.Name) FileDataStoreFactorySpi(org.geotools.data.FileDataStoreFactorySpi) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)55 SimpleFeature (org.opengis.feature.simple.SimpleFeature)33 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)24 Optional (com.google.common.base.Optional)17 ArrayList (java.util.ArrayList)14 RevFeature (org.locationtech.geogig.api.RevFeature)14 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 Test (org.junit.Test)11 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)10 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)8 Point (com.vividsolutions.jts.geom.Point)8 File (java.io.File)8 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)8 Geometry (com.vividsolutions.jts.geom.Geometry)7 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)7 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)7