Package org.geotools.data.simple

Examples of org.geotools.data.simple.SimpleFeatureStore


        final String typeName = testData.getTempTableName(); // "SDE.CJ_TST_1";
        final ArcSDEDataStore dataStore = testData.getDataStore();
        // String[] typeNames = dataStore.getTypeNames();
        // System.err.println(typeNames);
        final SimpleFeatureStore store;
        store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
        final SimpleFeatureType schema = store.getSchema();
        GeometryDescriptor defaultGeometry = schema.getGeometryDescriptor();
        String fid1;
        String fid2;
        // insert polygons p1, p2 and grab the fids for later retrieval
        {
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore
                    .getFeatureWriterAppend(typeName, Transaction.AUTO_COMMIT);
            SimpleFeature feature;
            try {
                feature = writer.next();
                // set this attribute as its the only non nillable one
                feature.setAttribute("INT32_COL", Integer.valueOf(0));
                // now set the geometry
                feature.setAttribute(defaultGeometry.getName(), p1);
                writer.write();
                fid1 = feature.getID();

                feature = writer.next();
                // set this attribute as its the only non nillable one
                feature.setAttribute("INT32_COL", Integer.valueOf(0));
                // now set the geometry
                feature.setAttribute(defaultGeometry.getName(), p2);
                writer.write();
                fid2 = feature.getID();
            } finally {
                writer.close();
            }
        }

        final Transaction transaction = new DefaultTransaction("testUpdateAdjacentPolygons");
        store.setTransaction(transaction);
        final FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
        Filter fid1Filter = ff.id(Collections.singleton(ff.featureId(fid1)));
        Filter fid2Filter = ff.id(Collections.singleton(ff.featureId(fid2)));
        try {
            store.modifyFeatures(defaultGeometry.getName(), modif2, fid2Filter);
            store.modifyFeatures(defaultGeometry.getName(), modif1, fid1Filter);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            throw e;
        } finally {
            transaction.close();
        }
        store.setTransaction(Transaction.AUTO_COMMIT);

        try {
            SimpleFeatureCollection features;
            SimpleFeatureIterator iterator;

            features = store.getFeatures(fid1Filter);
            iterator = features.features();
            final SimpleFeature feature1 = iterator.next();
            iterator.close();

            features = store.getFeatures(fid2Filter);
            iterator = features.features();
            final SimpleFeature feature2 = iterator.next();
            iterator.close();

            // Note that for tables that are ambiguous about what types of geometries
            // they store (as this table is), ArcSDE will "compress" a stored geometry
            // to it's simplest representation. So in case the defaultGeometry.getBinding()
            // returns "Geometry", do instanceof checks to verify what kind of geometry
            // you're getting back
            Geometry actual1 = (Geometry) feature1.getAttribute(defaultGeometry.getLocalName());
            Geometry actual2 = (Geometry) feature2.getAttribute(defaultGeometry.getLocalName());
            System.out.println(actual1);
            System.out.println(modif1);

            // there's some rounding that goes on inside SDE. Need to do some simple buffering to
            // make sure
            // we're not getting rounding errors
            assertTrue(modif1.buffer(.01).contains(actual1));
            assertTrue(modif2.buffer(.01).contains(actual2));
        } finally {
            try {
                store.removeFeatures(fid1Filter);
                store.removeFeatures(fid2Filter);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
View Full Code Here


        testData.createTempTable(true);
        // testData.insertTestData();

        final DataStore ds = testData.getDataStore();
        final String typeName = testData.getTempTableName();
        final SimpleFeatureStore transFs;
        transFs = (SimpleFeatureStore) ds.getFeatureSource(typeName);
        final SimpleFeatureType schema = transFs.getSchema();

        // once the transaction is set to the FeatureStore, it lasts until
        // another transaction
        // is set. Calling transaction.close() closes Transaction.State
        // held on it, allowing State objects to release resources. After
        // close() the transaction
        // is no longer valid.
        final Transaction transaction = new DefaultTransaction("test_handle");
        transFs.setTransaction(transaction);

        try {
            // create a feature to add
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
            builder.set("INT32_COL", Integer.valueOf(1000));
            builder.set("STRING_COL", "inside transaction");
            SimpleFeature feature = builder.buildFeature(null);

            // add the feature
            transFs.addFeatures(DataUtilities.collection(feature));

            // now confirm for that transaction the feature is fetched, and outside
            // it it's not.
            final Filter filterNewFeature = CQL.toFilter("INT32_COL = 1000");
            final Query newFeatureQuery = new Query(typeName, filterNewFeature);

            SimpleFeatureCollection features;
            features = transFs.getFeatures(filterNewFeature);
            int size = features.size();
            assertEquals(1, size);

            // ok transaction respected, assert the feature does not exist outside
            // it (except is the db is MS SQL Server)
            {
                FeatureReader<SimpleFeatureType, SimpleFeature> autoCommitReader;
                autoCommitReader = ds.getFeatureReader(newFeatureQuery, Transaction.AUTO_COMMIT);
                try {
                    if (databaseIsMsSqlServer) {
                        // SQL Server always is at READ UNCOMMITTED isolation level...
                        assertTrue(autoCommitReader.hasNext());
                    } else {
                        assertFalse(autoCommitReader.hasNext());
                    }
                } finally {
                    autoCommitReader.close();
                }
            }

            // ok, but what if we ask for a feature reader with the same transaction
            {
                FeatureReader<SimpleFeatureType, SimpleFeature> transactionReader;
                transactionReader = ds.getFeatureReader(newFeatureQuery, transaction);
                try {
                    assertTrue(transactionReader.hasNext());
                    transactionReader.next();
                    assertFalse(transactionReader.hasNext());
                } finally {
                    transactionReader.close();
                }
            }

            // now commit, and Transaction.AUTO_COMMIT should carry it over
            // do not close the transaction, we'll keep using it
            try {
                transaction.commit();
            } catch (IOException e) {
                transaction.rollback();
                throw e;
            }

            {
                FeatureReader<SimpleFeatureType, SimpleFeature> autoCommitReader;
                autoCommitReader = ds.getFeatureReader(newFeatureQuery, Transaction.AUTO_COMMIT);
                try {
                    assertTrue(autoCommitReader.hasNext());
                } finally {
                    autoCommitReader.close();
                }
            }

            // now keep using the transaction, it should still work
            transFs.removeFeatures(filterNewFeature);

            // no features removed yet outside the transaction
            {
                FeatureReader<SimpleFeatureType, SimpleFeature> autoCommitReader;
                autoCommitReader = ds.getFeatureReader(newFeatureQuery, Transaction.AUTO_COMMIT);
View Full Code Here

        testData.insertTestData();
        final SimpleFeatureCollection featuresToSet = testData.createTestFeatures(Point.class, 5);
        final DataStore ds = testData.getDataStore();
        final String typeName = testData.getTempTableName();

        final SimpleFeatureStore store;
        store = (SimpleFeatureStore) ds.getFeatureSource(typeName);

        final int initialCount = store.getCount(Query.ALL);
        assertTrue(initialCount > 0);
        assertTrue(initialCount != 5);

        store.setFeatures(DataUtilities.reader(featuresToSet));

        final int newCount = store.getCount(Query.ALL);
        assertEquals(5, newCount);
    }
View Full Code Here

        final SimpleFeatureCollection featuresToSet = testData.createTestFeatures(Point.class, 5);
        final DataStore ds = testData.getDataStore();
        final String typeName = testData.getTempTableName();

        final Transaction transaction = new DefaultTransaction("testSetFeaturesTransaction handle");
        final SimpleFeatureStore store;
        store = (SimpleFeatureStore) ds.getFeatureSource(typeName);
        store.setTransaction(transaction);

        final int initialCount = store.getCount(Query.ALL);
        assertTrue(initialCount > 0);
        assertTrue(initialCount != 5);

        try {
            store.setFeatures(DataUtilities.reader(featuresToSet));
            final int countInsideTransaction = store.getCount(Query.ALL);
            assertEquals(5, countInsideTransaction);

            final SimpleFeatureSource sourceNoTransaction;
            sourceNoTransaction = ds.getFeatureSource(typeName);
            int countNoTransaction = sourceNoTransaction.getCount(Query.ALL);
View Full Code Here

        final int featureCount = 2;
        final SimpleFeatureCollection testFeatures = testData.createTestFeatures(LineString.class,
                featureCount);

        final DataStore ds = testData.getDataStore();
        final SimpleFeatureStore fStore = (SimpleFeatureStore) ds.getFeatureSource(typeName);
        final Transaction transaction = new DefaultTransaction("testTransactionMultithreadAccess");
        fStore.setTransaction(transaction);

        final boolean[] done = { false, false };
        final Throwable[] errors = new Throwable[2];

        Runnable worker1 = new Runnable() {
            public void run() {
                try {
                    System.err.println("adding..");
                    List<FeatureId> addedFids = fStore.addFeatures(testFeatures);
                    System.err.println("got " + addedFids);
                    final FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);

                    final Set<FeatureId> fids = new HashSet<FeatureId>();
                    for (FeatureId fid : addedFids) {
                        fids.add(fid);
                    }
                    final Id newFidsFilter = ff.id(fids);

                    System.err.println("querying..");
                    SimpleFeatureCollection features = fStore.getFeatures(newFidsFilter);
                    System.err.println("querying returned...");

                    int size = features.size();
                    System.err.println("Collection Size: " + size);
                    assertEquals(2, size);

                    System.err.println("commiting...");
                    transaction.commit();
                    System.err.println("commited.");

                    size = fStore.getCount(new Query(typeName, newFidsFilter));
                    System.err.println("Size: " + size);
                    assertEquals(2, size);
                } catch (Throwable e) {
                    errors[0] = e;
                    try {
                        System.err.println("rolling back!.");
                        transaction.rollback();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } finally {
                    done[0] = true;
                }
            }
        };

        Runnable worker2 = new Runnable() {
            public void run() {
                try {
                    System.err.println("worker2 calling getFeartures()");
                    SimpleFeatureCollection collection = fStore.getFeatures();
                    System.err.println("worker2 opening iterator...");
                    SimpleFeatureIterator features = collection.features();
                    try {
                        System.err.println("worker2 iterating...");
                        while (features.hasNext()) {
View Full Code Here

                }
            }

            final ArcSDEDataStore dataStore = testData.getDataStore();
            final SimpleFeatureSource source;
            final SimpleFeatureStore store;

            source = dataStore.getFeatureSource(tableName);
            store = (SimpleFeatureStore) dataStore.getFeatureSource(tableName);

            Transaction transaction = new DefaultTransaction();
            store.setTransaction(transaction);

            ArcSdeResourceInfo info = (ArcSdeResourceInfo) store.getInfo();
            assertTrue(info.isVersioned());

            final SimpleFeatureType schema = store.getSchema();

            final int initialCount = store.getCount(Query.ALL);
            assertEquals(0, initialCount);

            final WKTReader reader = new WKTReader();
            Object[] content = new Object[2];
            SimpleFeature feature;
            SimpleFeatureCollection collection;
            int count;

            content[0] = "Feature name 1";
            content[1] = reader.read("POINT (10 10)");
            feature = SimpleFeatureBuilder.build(schema, content, (String) null);
            collection = DataUtilities.collection(feature);

            store.addFeatures(collection);

            count = store.getCount(Query.ALL);
            assertEquals(1, count);
            assertEquals(0, source.getCount(Query.ALL));

            {
                SimpleFeatureIterator features = store.getFeatures().features();
                SimpleFeature f = features.next();
                features.close();
                Object obj = f.getDefaultGeometry();
                assertTrue(obj instanceof Point);
                Point p = (Point) obj;
                double x = p.getX();
                double y = p.getY();
                assertEquals(10D, x, 1E-5);
                assertEquals(10D, y, 1E-5);
            }

            transaction.commit();
            assertEquals(1, source.getCount(Query.ALL));

            content[0] = "Feature name 2";
            content[1] = reader.read("POINT (2 2)");
            feature = SimpleFeatureBuilder.build(schema, content, (String) null);
            collection = DataUtilities.collection(feature);

            store.addFeatures(collection);

            count = store.getCount(Query.ALL);
            assertEquals(2, count);

            assertEquals(1, source.getCount(Query.ALL));
            transaction.rollback();
            assertEquals(1, store.getCount(Query.ALL));

            transaction.close();

            {
                SimpleFeatureIterator features = source.getFeatures().features();
View Full Code Here

        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put(ShapefileDataStoreFactory.URLP.key, shpFile.toURI().toURL());
        shapeFileDataStore = dataStoreFactory.createNewDataStore(params);
        shapeFileDataStore.createSchema(feature.getFeatureType());
        SimpleFeatureStore featureStore = (SimpleFeatureStore) shapeFileDataStore
                .getFeatureSource(shapeFileDataStore.getTypeNames()[0]);
        featureStore.addFeatures(DataUtilities.collection(feature));

        FontCache.getDefaultInstance().registerFont(
                Font.createFont(Font.TRUETYPE_FONT, TestData.getResource(this, "Vera.ttf")
                        .openStream()));
    }
View Full Code Here

            MalformedURLException {
        DataStore s;
        s = createDataStore(maker, tmp.toURI().toURL(), memorymapped);

        s.createSchema(type);
        SimpleFeatureStore store = (SimpleFeatureStore) s.getFeatureSource(s.getTypeNames()[0]);

        assertEquals(0, store.getCount(Query.ALL));
        store.addFeatures(one);
        assertEquals(one.size(), store.getCount(Query.ALL));
        store.addFeatures(one);

        assertEquals(one.size() * 2, store.getCount(Query.ALL));
        s.dispose();
    }
View Full Code Here

        String typeName;
        s = createDataStore(maker, tmp.toURI().toURL(), memorymapped);

        s.createSchema(type);

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

        store.addFeatures(one);
        s.dispose();

        s = createDataStore(new ShapefileDataStoreFactory(), tmp.toURI().toURL(), true);
        typeName = s.getTypeNames()[0];
View Full Code Here

        SimpleFeatureSource featureSource = datastore
                .getFeatureSource("example");
        if (!(featureSource instanceof SimpleFeatureStore)) {
            throw new IllegalStateException("Modification not supported");
        }
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

        // featureStoreExample end
        System.out.println("\nfeatureStoreExample end\n");
    }
View Full Code Here

TOP

Related Classes of org.geotools.data.simple.SimpleFeatureStore

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.