use of org.opengis.feature.FeatureVisitor in project polymap4-core by Polymap4.
the class RDataStore method updateSchema.
@Override
public void updateSchema(Name name, final FeatureType newSchema) throws IOException {
assert name != null && newSchema != null;
final Updater tx = store.prepareUpdate();
try {
// check modified property names
boolean namesModified = false;
for (PropertyDescriptor desc : newSchema.getDescriptors()) {
// set by FeatureTypeEditor/AttributeCellModifier
String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
if (origName != null) {
namesModified = true;
}
}
// find deleted properties
// XXX check complex schemas
FeatureType schema = getSchema(name);
final List<PropertyDescriptor> deleted = new ArrayList();
for (PropertyDescriptor desc : schema.getDescriptors()) {
if (newSchema.getDescriptor(desc.getName()) == null) {
deleted.add(desc);
}
}
// schema name changed or prop deleted? -> update features
final String newName = newSchema.getName().getLocalPart();
if (!name.getLocalPart().equals(newSchema.getName().getLocalPart()) || !deleted.isEmpty() || namesModified) {
FeatureSource fs = getFeatureSource(name);
fs.getFeatures().accepts(new FeatureVisitor() {
public void visit(Feature feature) {
try {
// typeName
((RFeature) feature).state.put(RFeature.TYPE_KEY, newName);
// List<Name> origModifiedNames = new ArrayList();
for (PropertyDescriptor desc : newSchema.getDescriptors()) {
// set by FeatureTypeEditor/AttributeCellModifier
String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
if (origName != null) {
RAttribute prop = (RAttribute) feature.getProperty(origName);
if (prop != null) {
if (prop.getValue() != null) {
((RFeature) feature).state.put(desc.getName().getLocalPart(), prop.getValue());
}
((RFeature) feature).state.remove(prop.key.toString());
}
}
}
// deleted attributes
for (PropertyDescriptor desc : deleted) {
// XXX check complex schemas
RProperty prop = (RProperty) feature.getProperty(desc.getName());
((RFeature) feature).state.remove(prop.key.toString());
}
tx.store(((RFeature) feature).state);
} catch (Exception e) {
// Designing a visitor interface without Exception is not a good idea!
throw new RuntimeException("", e);
}
}
}, null);
}
// update schema record
ResultSet rs = store.find(new SimpleQuery().setMaxResults(1).eq("type", "FeatureType").eq("name", name.getLocalPart()));
IRecordState record = rs.get(0);
String schemaContent = schemaCoder.encode(newSchema);
record.put("content", schemaContent);
record.put("name", newName);
tx.store(record);
log.debug("Current schema: " + schemaCoder.encode(schema));
log.debug("Updated schema: " + schemaContent);
tx.apply();
} catch (Throwable e) {
log.debug("", e);
tx.discard();
throw new RuntimeException(e);
}
}
use of org.opengis.feature.FeatureVisitor in project polymap4-core by Polymap4.
the class RFeatureStoreTests method testCopyFluesse.
public void testCopyFluesse() throws Exception {
File f = new File("/home/falko/Data/WGN_SAX_INFO/Datenuebergabe_Behoerden_Stand_1001/Shapedateien/Chem_Zustand_Fliessgew_WK_Liste_CHEM_0912.shp");
log.debug("opening shapefile: " + f);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", f.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore shapeDs = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
FeatureSource<SimpleFeatureType, SimpleFeature> shapeFs = shapeDs.getFeatureSource();
// creating schema
ds.createSchema(shapeFs.getSchema());
// adding features
RFeatureStore fs = (RFeatureStore) ds.getFeatureSource(shapeFs.getSchema().getName());
fs.addFeatures(shapeFs.getFeatures());
// check size
assertEquals(669, fs.getFeatures().size());
// iterating accept
fs.getFeatures().accepts(new FeatureVisitor() {
public void visit(Feature feature) {
assertTrue(feature.getDefaultGeometryProperty().getValue() instanceof Geometry);
}
}, null);
// check feature
FeatureIterator<SimpleFeature> fsIt = fs.getFeatures().features();
FeatureIterator<SimpleFeature> shapeIt = shapeFs.getFeatures().features();
int count = 0;
for (; fsIt.hasNext() && shapeIt.hasNext(); count++) {
SimpleFeature f1 = fsIt.next();
SimpleFeature f2 = shapeIt.next();
for (Property prop1 : f1.getProperties()) {
Property prop2 = f2.getProperty(prop1.getName());
if (prop1.getValue() instanceof Geometry) {
// skip
} else {
assertTrue("Property don't match: " + prop1.getName() + ": " + prop1.getValue() + "!=" + prop2.getValue(), Utilities.equals(prop1.getValue(), prop2.getValue()));
}
}
// assertTrue( "Features are not equal: \n" + f1 + "\n" + f2, Utilities. );
}
assertEquals(669, count);
}
use of org.opengis.feature.FeatureVisitor 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.opengis.feature.FeatureVisitor in project polymap4-core by Polymap4.
the class RFeatureStore method addFeatures.
// FeatureStore ***************************************
@Override
public List addFeatures(FeatureCollection features) throws IOException {
final List<FeatureId> fids = new ArrayList();
try {
startModification();
List<Exception> exc = new ArrayList();
features.accepts(new FeatureVisitor() {
public void visit(Feature feature) {
// assert feature instanceof RFeature : "Added features must be RFeatures. See RFeatureStore#newFeature().";
try {
// RFeature
if (feature instanceof RFeature) {
txState.updater().store(((RFeature) feature).state);
fids.add(feature.getIdentifier());
} else // SimpleFeature -> convert
if (feature instanceof SimpleFeature) {
RFeature newFeature = newFeature(feature.getIdentifier() != null ? feature.getIdentifier().getID() : null);
for (Property prop : feature.getProperties()) {
newFeature.getProperty(prop.getName()).setValue(prop.getValue());
}
// sanity check: geom
GeometryAttribute geom = feature.getDefaultGeometryProperty();
if (geom != null && geom.getValue() == null) {
throw new RuntimeException("Feature has no geometry: " + feature.getIdentifier().getID());
}
txState.updater().store(newFeature.state);
fids.add(newFeature.getIdentifier());
} else {
throw new UnsupportedOperationException("Added features must be instance of RFeature or SimpleFeature");
}
} catch (Exception e) {
exc.add(e);
}
}
}, null);
if (!exc.isEmpty()) {
throw exc.get(0);
}
completeModification(true);
} catch (IOException e) {
completeModification(false);
throw e;
} catch (Throwable e) {
completeModification(false);
throw new RuntimeException(e);
}
return fids;
}
Aggregations