Package org.geotools.data

Examples of org.geotools.data.DefaultTransaction


        // transactionExample start
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("directory", directory);
        DataStore store = DataStoreFinder.getDataStore(params);

        Transaction t1 = new DefaultTransaction("transaction 1");
        Transaction t2 = new DefaultTransaction("transactoin 2");

        SimpleFeatureType type = store.getSchema("example");
        SimpleFeatureStore featureStore = (SimpleFeatureStore) store
                .getFeatureSource("example");
        SimpleFeatureStore featureStore1 = (SimpleFeatureStore) store
                .getFeatureSource("example");
        SimpleFeatureStore featureStore2 = (SimpleFeatureStore) store
                .getFeatureSource("example");

        featureStore1.setTransaction(t1);
        featureStore2.setTransaction(t2);
       
        System.out.println("Step 1");
        System.out.println("------");
        System.out.println("start     auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("start              t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("start              t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );
       
        // select feature to remove
        FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
        Filter filter1 = ff.id(Collections.singleton(ff.featureId("fid1")));
        featureStore1.removeFeatures(filter1); // road1 removes fid1 on t1
       
        System.out.println();
        System.out.println("Step 2 transaction 1 removes feature 'fid1'");
        System.out.println("------");
        System.out.println("t1 remove auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t1 remove          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 remove          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );
        // new feature to add!
        SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] {
                5, "chris", null }, "fid5");
        feature.getUserData().put(Hints.USE_PROVIDED_FID,true);
        feature.getUserData().put(Hints.PROVIDED_FID, "fid5");
       
        SimpleFeatureCollection collection = DataUtilities.collection(feature);
        featureStore2.addFeatures(collection);

        System.out.println();
        System.out.println("Step 3 transaction 2 adds a new feature '"+feature.getID()+"'");
        System.out.println("------");
        System.out.println("t2 add    auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t2 add             t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 add             t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        // commit transaction one
        t1.commit();
       
        System.out.println();
        System.out.println("Step 4 transaction 1 commits the removal of feature 'fid1'");
        System.out.println("------");
        System.out.println("t1 commit auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t1 commit          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 commit          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        // commit transaction two
        t2.commit();

        System.out.println();
        System.out.println("Step 5 transaction 2 commits the addition of '"+feature.getID()+"'");
        System.out.println("------");
        System.out.println("t2 commit auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t2 commit          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t2 commit          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        t1.close();
        t2.close();
        store.dispose(); // clear out any listeners
        // transactionExample end
        System.out.println("\ntransactionExample end\n");
    }
View Full Code Here


        // removeAllExample start
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("directory", directory);
        DataStore store = DataStoreFinder.getDataStore(params);

        Transaction t = new DefaultTransaction("transaction");
        try {
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = store
                    .getFeatureWriter("example", Filter.INCLUDE, t);

            SimpleFeature feature;
            try {
                while (writer.hasNext()) {
                    feature = writer.next();
                    System.out.println("remove " + feature.getID());
                    writer.remove(); // marking contents for removal
                }
            } finally {
                writer.close();
            }
            System.out.println("commit " + t); // now the contents are removed
            t.commit();
        } catch (Throwable eek) {
            t.rollback();
        } finally {
            t.close();
            store.dispose();
        }
        // removeAllExample end
        System.out.println("\nremoveAllExample end\n");
    }
View Full Code Here

        selectFid1 = ff.id(Collections.singleton(ff.featureId("fid1")));
        reader = store.getFeatureReader(new Query("road", selectFid1), Transaction.AUTO_COMMIT);
        assertEquals(1, count(reader));

        Transaction transaction = new DefaultTransaction();
        reader = store.getFeatureReader(roadQuery, transaction);
        assertEquals(5, count(reader));

        reader = store.getFeatureReader(roadQuery, transaction);
        List<String> list = new ArrayList<String>();
View Full Code Here

                new Integer(5), "chris" }, "fid5");

        SimpleFeatureStore roadAuto = (SimpleFeatureStore) store.getFeatureSource("road");

        SimpleFeatureStore roadFromClient1 = (SimpleFeatureStore) store.getFeatureSource("road");
        Transaction transaction1 = new DefaultTransaction("Transaction Used by Client 1");
        roadFromClient1.setTransaction(transaction1);

        SimpleFeatureStore roadFromClient2 = (SimpleFeatureStore) store.getFeatureSource("road");
        Transaction transaction2 = new DefaultTransaction("Transaction Used by Client 2");
        roadFromClient2.setTransaction(transaction2);

        FilterFactory2 ff = (FilterFactory2) CommonFactoryFinder.getFilterFactory(null);
        Filter selectFid1 = ff.id(Collections.singleton(ff.featureId("fid1")));

        // Before we edit everything should be the same
        assertEquals("auto before", 5, roadAuto.getFeatures().size());
        assertEquals("client 1 before", 5, roadFromClient1.getFeatures().size());
        assertEquals("client 2 before", 5, roadFromClient2.getFeatures().size());

        // Remove Feature with Fid1
        roadFromClient1.removeFeatures(selectFid1); // road1 removes fid1 on t1

        assertEquals("auto after client 1 removes fid1", 5, roadAuto.getFeatures().size());
        assertEquals("client 1 after client 1 removes fid1", 4, roadFromClient1.getFeatures()
                .size());
        assertEquals("client 2 after client 1 removes fid1", 5, roadFromClient2.getFeatures()
                .size());

        roadFromClient2.addFeatures(DataUtilities.collection(chrisFeature)); // road2 adds fid5 on t2
        assertEquals("auto after client 1 removes fid1 and client 2 adds fid5", 5, roadAuto
                .getFeatures().size());
        assertEquals("client 1 after client 1 removes fid1 and client 2 adds fid5", 4,
                roadFromClient1.getFeatures().size());
        assertEquals("cleint 2 after client 1 removes fid1 and client 2 adds fid5", 6,
                roadFromClient2.getFeatures().size());

        transaction1.commit();
        assertEquals("auto after client 1 commits removal of fid1 (client 2 has added fid5)", 4,
                roadAuto.getFeatures().size());
        assertEquals("client 1 after commiting removal of fid1 (client 2 has added fid5)", 4,
                roadFromClient1.getFeatures().size());
        assertEquals("client 2 after client 1 commits removal of fid1 (client 2 has added fid5)",
                5, roadFromClient2.getFeatures().size());

        transaction2.commit();
        assertEquals("auto after client 2 commits addition of fid5 (fid1 previously removed)", 5,
                roadAuto.getFeatures().size());
        assertEquals("client 1 after client 2 commits addition of fid5 (fid1 previously removed)",
                5, roadFromClient1.getFeatures().size());
        assertEquals("client 2 after commiting addition of fid5 (fid1 previously removed)", 5,
View Full Code Here

            MemoryDataStore dsStreams_5_10_20_50 = createMemStoreHorizontal(ds.getSchema(),
                    "dsStreams_5_10_20_50", "streams_5_10_20_50");

            // StreamsFeatureSource = ds.getFeatureSource(typeName);
            Transaction t = new DefaultTransaction();
            Query query = new Query(typeName, Filter.INCLUDE);
            FeatureReader<SimpleFeatureType, SimpleFeature> reader = ds.getFeatureReader(query, t);
            while (reader.hasNext()) {

                SimpleFeature stream = reader.next();

                POINTMAP.get(0.0).put(stream.getID(),
                        ((Geometry) stream.getDefaultGeometry()).getNumPoints());

                addGeneralizedFeatureVertical(stream, dsStreams_5, 5.0);
                addGeneralizedFeatureVertical(stream, dsStreams_10, 10.0);
                addGeneralizedFeatureVertical(stream, dsStreams_20, 20.0);
                addGeneralizedFeatureVertical(stream, dsStreams_50, 50.0);

                addGeneralizedFeatureMixed(stream, dsStreams_5_10, 5.0, 10.0);
                addGeneralizedFeatureMixed(stream, dsStreams_20_50, 20.0, 50.0);
                addGeneralizedFeatureHorizontal(stream, dsStreams_5_10_20_50);
            }

            reader.close();
            t.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        initialized = true;
View Full Code Here

        DataStore dataStore = factory.createNewDataStore(create);
        SimpleFeatureType featureType = SimpleFeatureTypeBuilder.retype(schema, worldCRS);
        dataStore.createSchema(featureType);

        // carefully open an iterator and writer to process the results
        Transaction transaction = new DefaultTransaction("Reproject");
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
                        dataStore.getFeatureWriterAppend(featureType.getTypeName(), transaction);
        SimpleFeatureIterator iterator = featureCollection.features();
        try {
            while (iterator.hasNext()) {
                // copy the contents of each feature and transform the geometry
                SimpleFeature feature = iterator.next();
                SimpleFeature copy = writer.next();
                copy.setAttributes(feature.getAttributes());

                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                Geometry geometry2 = JTS.transform(geometry, transform);

                copy.setDefaultGeometry(geometry2);
                writer.write();
            }
            transaction.commit();
            JOptionPane.showMessageDialog(null, "Export to shapefile complete");
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
            JOptionPane.showMessageDialog(null, "Export to shapefile failed");
        } finally {
            writer.close();
            iterator.close();
            transaction.close();
        }
    }
View Full Code Here

       
        // docs break transaction
        /*
         * Write the features to the shapefile
         */
        Transaction transaction = new DefaultTransaction("create");

        String typeName = newDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
        SimpleFeatureType SHAPE_TYPE = featureSource.getSchema();
        /*
         * The Shapefile format has a couple limitations:
         * - "the_geom" is always first, and used for the geometry attribute name
         * - "the_geom" must be of type Point, MultiPoint, MuiltiLineString, MultiPolygon
         * - Attribute names are limited in length
         * - Not all data types are supported (example Timestamp represented as Date)
         *
         * Each data store has different limitations so check the resulting SimpleFeatureType.
         */
        System.out.println("SHAPE:"+SHAPE_TYPE);

        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            /*
             * SimpleFeatureStore has a method to add features from a
             * SimpleFeatureCollection object, so we use the ListFeatureCollection
             * class to wrap our list of features.
             */
            SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
            featureStore.setTransaction(transaction);
            try {
                featureStore.addFeatures(collection);
                transaction.commit();
            } catch (Exception problem) {
                problem.printStackTrace();
                transaction.rollback();
            } finally {
                transaction.close();
            }
            System.exit(0); // success!
        } else {
            System.out.println(typeName + " does not support read/write access");
            System.exit(1);
View Full Code Here

        create.put("url", file.toURI().toURL());
        create.put("create spatial index", Boolean.TRUE);
        DataStore newDataStore = factory.createNewDataStore(create);

        newDataStore.createSchema(featureCollection.getSchema());
        Transaction transaction = new DefaultTransaction("Reproject");
        SimpleFeatureStore featureStore;
        featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);
        featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(featureCollection);
            transaction.commit();
            JOptionPane.showMessageDialog(null, "Export to shapefile complete", "Export",
                            JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception problem) {
            transaction.rollback();
            problem.printStackTrace();
            JOptionPane.showMessageDialog(null, "Export to shapefile failed", "Export",
                            JOptionPane.ERROR_MESSAGE);
        } finally {
            transaction.close();
        }
    }
View Full Code Here

        ShapefileDataStore sds = createDataStore();

        int idx = loadFeatures(sds).size();

        while (idx > 0) {
            Transaction t = new DefaultTransaction();
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;

            try {
                writer = sds.getFeatureWriter(sds.getTypeNames()[0],
                        Filter.INCLUDE, t);
                writer.next();
                writer.remove();
            } finally {
                if (writer != null) {
                    writer.close();
                    writer = null;
                }
            }
            t.commit();
            t.close();
            assertEquals(--idx, loadFeatures(sds).size());
        }
        sds.dispose();
    }
View Full Code Here

        int idx = sds.getFeatureSource().getCount(Query.ALL);

        SimpleFeatureStore store = (SimpleFeatureStore) sds.getFeatureSource(sds
                .getTypeNames()[0]);

        Transaction transaction = new DefaultTransaction();
        store.setTransaction(transaction);
        SimpleFeature[] newFeatures1 = new SimpleFeature[1];
        SimpleFeature[] newFeatures2 = new SimpleFeature[2];
        GeometryFactory fac = new GeometryFactory();
        newFeatures1[0] = DataUtilities.template(sds.getSchema());
        newFeatures1[0].setDefaultGeometry(fac
                .createPoint(new Coordinate(0, 0)));
        newFeatures2[0] = DataUtilities.template(sds.getSchema());
        newFeatures2[0].setDefaultGeometry(fac
                .createPoint(new Coordinate(0, 0)));
        newFeatures2[1] = DataUtilities.template(sds.getSchema());
        newFeatures2[1].setDefaultGeometry(fac
                .createPoint(new Coordinate(0, 0)));

        store.addFeatures(DataUtilities.collection(newFeatures1));
        store.addFeatures(DataUtilities.collection(newFeatures2));
        transaction.commit();
        transaction.close();
        assertEquals(idx + 3, sds.getCount(Query.ALL));
        sds.dispose();

    }
View Full Code Here

TOP

Related Classes of org.geotools.data.DefaultTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.