Package org.geotools.data

Examples of org.geotools.data.Transaction


            FileUtils.deleteDirectory(parentLocation);
        }
        assertTrue(parentLocation.mkdir());
        final String databaseName = "test";
        CoverageSlicesCatalog sliceCat = null;
        final Transaction t = new DefaultTransaction(Long.toString(System.nanoTime()));
        try {
            sliceCat = new CoverageSlicesCatalog(databaseName, parentLocation);
            String[] typeNames = sliceCat.getTypeNames();
            assertNull(typeNames);

            // create new schema 1
            final String schemaDef1 = "the_geom:Polygon,coverage:String,imageindex:Integer,cloud_formations:Integer";
            sliceCat.createType("1", schemaDef1);
            typeNames = sliceCat.getTypeNames();
            assertNotNull(typeNames);
            assertEquals(1, typeNames.length);

            // add features to it
            SimpleFeatureType schema = DataUtilities.createType("1", schemaDef1);
            SimpleFeature feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "a");
            feat.setAttribute("imageindex", Integer.valueOf(0));
            feat.setAttribute("cloud_formations", Integer.valueOf(3));
            ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(-180, 180, -90, 90,
                    DefaultGeographicCRS.WGS84);
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("1", feat, t);

            feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "a");
            feat.setAttribute("imageindex", Integer.valueOf(1));
            feat.setAttribute("cloud_formations", Integer.valueOf(2));
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("1", feat, t);
            t.commit();

            // read back
            CountVisitor cv = new CountVisitor();
            Query q = new Query("1");
            q.setFilter(Filter.INCLUDE);
            sliceCat.computeAggregateFunction(q, cv);
            assertEquals(2, cv.getCount());

            // create new schema 2
            final String schemaDef2 = "the_geom:Polygon,coverage:String,imageindex:Integer,new:Double";
            sliceCat.createType("2", schemaDef2);
            typeNames = sliceCat.getTypeNames();
            assertNotNull(typeNames);
            assertEquals(2, typeNames.length);

            // add features to it
            schema = DataUtilities.createType("2", schemaDef2);
            feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "b");
            feat.setAttribute("imageindex", Integer.valueOf(0));
            feat.setAttribute("new", Double.valueOf(3.22));
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("2", feat, t);

            feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "b");
            feat.setAttribute("imageindex", Integer.valueOf(1));
            feat.setAttribute("new", Double.valueOf(1.12));
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("2", feat, t);

            feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "b");
            feat.setAttribute("imageindex", Integer.valueOf(2));
            feat.setAttribute("new", Double.valueOf(1.32));
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("2", feat, t);

            t.commit();

            // read back
            cv = new CountVisitor();
            q = new Query("2");
            q.setFilter(Filter.INCLUDE);
            sliceCat.computeAggregateFunction(q, cv);
            assertEquals(3, cv.getCount());
           
            // Get the CoverageSlices
            List<CoverageSlice> slices = sliceCat.getGranules(q);
            double[] news = new double[]{3.22, 1.12, 1.32};
            for(int i = 0; i < news.length; i++){
                CoverageSlice slice = slices.get(i);
                assertTrue(slice.getGranuleBBOX().contains(referencedEnvelope));
                double newAttr = (double) slice.getOriginator().getAttribute("new");
                assertEquals(newAttr, news[i], DELTA);
            }
           
            // Creating a CoverageSliceCatalogSource and check if it behaves correctly
            CoverageSlicesCatalogSource src = new CoverageSlicesCatalogSource(sliceCat, "2");
            assertEquals(3, src.getCount(q));
            SimpleFeatureCollection coll = src.getGranules(q);
            cv.reset();
            coll.accepts(cv, null);
            assertEquals(3, cv.getCount());
            assertTrue(src.getBounds(q).contains(
                    referencedEnvelope.toBounds(referencedEnvelope.getCoordinateReferenceSystem())));
            assertEquals(src.getSchema(), schema);

            // remove
            sliceCat.removeGranules("1", Filter.INCLUDE, t);
            sliceCat.removeGranules("2", Filter.INCLUDE, t);
            t.commit();
            // check
            q = new Query("1");
            q.setFilter(Filter.INCLUDE);
            sliceCat.computeAggregateFunction(q, cv);
            assertEquals(0, cv.getCount());
            q = new Query("2");
            sliceCat.computeAggregateFunction(q, cv);
            assertEquals(0, cv.getCount());

        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            t.rollback();
        } finally {
            if (sliceCat != null) {
                sliceCat.dispose();
            }

            t.close();

            FileUtils.deleteDirectory(parentLocation);
        }

    }
View Full Code Here


        // Reader Catalog
        CoverageSlicesCatalog sliceCat = reader.getCatalog();
        assertNotNull(sliceCat);

        final Transaction t = new DefaultTransaction(Long.toString(System.nanoTime()));
        try {
            String[] typeNames = sliceCat.getTypeNames();
            assertNull(typeNames);

            // create new schema 1
            final String schemaDef1 = "the_geom:Polygon,coverage:String,imageindex:Integer";
            sliceCat.createType("1", schemaDef1);
            typeNames = sliceCat.getTypeNames();
            assertNotNull(typeNames);
            assertEquals(1, typeNames.length);

            // add features to it
            SimpleFeatureType schema = DataUtilities.createType("1", schemaDef1);
            SimpleFeature feat = DataUtilities.template(schema);
            feat.setAttribute("coverage", "a");
            feat.setAttribute("imageindex", Integer.valueOf(0));
            ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(-180, 180, -90, 90,
                    DefaultGeographicCRS.WGS84);
            feat.setAttribute("the_geom", GEOM_FACTORY.toGeometry(referencedEnvelope));
            sliceCat.addGranule("1", feat, t);
            t.commit();
           
            // Check if present
           
            Query q = new Query("1");
            q.setFilter(Filter.INCLUDE);
            // Get ImageIndexes
            List<Integer> indexes = reader.getImageIndex(q);
            assertNotNull(indexes);
            assertTrue(!indexes.isEmpty());
        } catch (Exception e) {
            t.rollback();
        } finally {
            if (sliceCat != null) {
                sliceCat.dispose();
            }

            t.close();

            FileUtils.deleteDirectory(parentLocation);
        }

        String auxiliaryFilesPath = "file:/path";
View Full Code Here

        // 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

       
        // 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

        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

        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

TOP

Related Classes of org.geotools.data.Transaction

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.