Search in sources :

Example 1 with FeatureVisitor

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);
    }
}
Also used : FeatureType(org.opengis.feature.type.FeatureType) FeatureSource(org.geotools.data.FeatureSource) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleQuery(org.polymap.recordstore.SimpleQuery) ArrayList(java.util.ArrayList) FeatureVisitor(org.opengis.feature.FeatureVisitor) Feature(org.opengis.feature.Feature) IOException(java.io.IOException) Updater(org.polymap.recordstore.IRecordStore.Updater) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState)

Example 2 with FeatureVisitor

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);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) FeatureVisitor(org.opengis.feature.FeatureVisitor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) Property(org.opengis.feature.Property)

Example 3 with FeatureVisitor

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"));
}
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 4 with FeatureVisitor

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;
}
Also used : ArrayList(java.util.ArrayList) FeatureVisitor(org.opengis.feature.FeatureVisitor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FeatureId(org.opengis.filter.identity.FeatureId) GeometryAttribute(org.opengis.feature.GeometryAttribute) Property(org.opengis.feature.Property)

Aggregations

Feature (org.opengis.feature.Feature)4 FeatureVisitor (org.opengis.feature.FeatureVisitor)4 SimpleFeature (org.opengis.feature.simple.SimpleFeature)3 Point (com.vividsolutions.jts.geom.Point)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Property (org.opengis.feature.Property)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 File (java.io.File)1 Serializable (java.io.Serializable)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 FeatureSource (org.geotools.data.FeatureSource)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 ShapefileDataStoreFactory (org.geotools.data.shapefile.ShapefileDataStoreFactory)1 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 GeometryBuilder (org.geotools.geometry.jts.GeometryBuilder)1 GeometryAttribute (org.opengis.feature.GeometryAttribute)1