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();
}
}
}
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);
}
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"));
}
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));
}
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;
}
Aggregations