Package org.geotools.data.simple

Examples of org.geotools.data.simple.SimpleFeatureStore


     * @throws FileException
     */
    @Test
    public void testWipesOutInvalidFidsFromFilters() throws Exception {
        final ShapefileDataStore ds = createDataStore();
        SimpleFeatureStore store;
        store = (SimpleFeatureStore) ds.getFeatureSource();
       
        final String validFid1, validFid2, invalidFid1, invalidFid2;
        {
            SimpleFeatureIterator features = store.getFeatures().features();
            validFid1 = features.next().getID();
            validFid2 = features.next().getID();
            invalidFid1 = "_" + features.next().getID();
            invalidFid2 = features.next().getID() + "abc";
            features.close();
        }
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
        Set<Identifier> ids = new HashSet<Identifier>();
        ids.add(ff.featureId(validFid1));
        ids.add(ff.featureId(validFid2));
        ids.add(ff.featureId(invalidFid1));
        ids.add(ff.featureId(invalidFid2));
        Filter fidFilter = ff.id(ids);

        final SimpleFeatureType schema = store.getSchema();
        final String typeName = schema.getTypeName();
        //get a property of type String to update its value by the given filter
        final AttributeDescriptor attribute = schema.getDescriptor("f");
       
        assertEquals(2, count(ds, typeName, fidFilter));

        store.modifyFeatures(attribute, "modified", fidFilter);
        Filter modifiedFilter = ff.equals(ff.property("f"), ff.literal("modified"));
        assertEquals(2, count(ds, typeName, modifiedFilter));
       
        final int initialCount = store.getCount(Query.ALL);
        store.removeFeatures(fidFilter);
        final int afterCount = store.getCount(Query.ALL);
        assertEquals(initialCount - 2, afterCount);
        ds.dispose();
    }
View Full Code Here


        return "topp_states";
    }

    @Test
    public void testInfo() throws IOException {
        SimpleFeatureStore store1 = (SimpleFeatureStore) data.getFeatureSource(getRoadTypeName());
       
        ResourceInfo info = store1.getInfo();
      
        assertEquals("roads", info.getName());
        assertEquals("Generated from sfRoads", info.getDescription());
        assertTrue(info.getKeywords().contains("sfRoads roads"));
        assertEquals("roads_Type", info.getTitle());
View Full Code Here

   
    public void testAddFeature() throws Exception{
        SimpleFeature f1=SimpleFeatureBuilder.build(type,new Object[]{ "one",geom },null);
        SimpleFeature f2=SimpleFeatureBuilder.build(type,new Object[]{ "two", geom },null);
       
        SimpleFeatureStore store=(SimpleFeatureStore) ds.getFeatureSource("default");
        store.setTransaction(new DefaultTransaction());
        store.addFeatures( DataUtilities.collection( f1 ) );
        store.addFeatures( DataUtilities.collection(f2) );
       
        count( store, 3);
//        assertEquals("Number of known feature as obtained from getCount",3, store.getCount(Query.ALL));
    }
View Full Code Here

    }

    public void testRemoveFeature() throws Exception{
        SimpleFeature f1=SimpleFeatureBuilder.build(type,new Object[]{ "one",geom },null);
       
        SimpleFeatureStore store=(SimpleFeatureStore) ds.getFeatureSource("default");
        store.setTransaction(new DefaultTransaction());
        List<FeatureId> fid=store.addFeatures( DataUtilities.collection(f1) );

        count(store, 2);
        FeatureId identifier = fid.iterator().next();
        String next = identifier.getID();
        FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
        Filter f = filterFactory.id(Collections.singleton(filterFactory.featureId(next)));
        store.removeFeatures(f);
       
        count(store, 1);
//        assertEquals("Number of known feature as obtained from getCount",3, store.getCount(Query.ALL));
    }
View Full Code Here

     *
     * @return Transacstion this FeatureResults opperates against
     */
    protected Transaction getTransaction() {
        if (featureSource instanceof FeatureStore) {
            SimpleFeatureStore featureStore;
            featureStore = (SimpleFeatureStore) featureSource;

            return featureStore.getTransaction();
        } else {
            return Transaction.AUTO_COMMIT;
        }
    }
View Full Code Here

        SimpleFeatureSource featureSource = store.getFeatureSource("locations");
        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

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

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

        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!
        // 45.52, -122.681944, Portland, 800, 2014
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
        Point portland = gf.createPoint(new Coordinate( 45.52, -122.681944));
        SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] { portland, "Portland", 800, 2014 }, "locations.1");
        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
View Full Code Here

    }
   
    public static void doDelete(DataStore ds,SimpleFeatureType ft, Id ff) throws NoSuchElementException, IllegalAttributeException, IOException{
      assertNotNull("doInsertFailed?",ff);
      Transaction t = new DefaultTransaction();
      SimpleFeatureStore fs = (SimpleFeatureStore)ds.getFeatureSource(ft.getTypeName());
      fs.setTransaction(t);
     
      System.out.println("Delete Read 1");
      SimpleFeatureIterator fr = fs.getFeatures().features();
      int count1 = 0;
      while(fr.hasNext()){
        count1 ++; fr.next();
      }
        fr.close();

      System.out.println("Delete Remove "+ff);
      fs.removeFeatures(ff);

      System.out.println("Delete Read 2");
      fr = fs.getFeatures().features();
      int count2 = 0;
      while(fr.hasNext()){
        count2 ++;
        if(count2<5)
          System.out.println("# == "+count2+" "+fr.next().getID());
        else
          fr.next();
      }
        fr.close();
      assertTrue("Read 1 == "+count1+" Read 2 == "+count2,count2<count1);

      System.out.println("Delete Commit");
      t.commit();

      System.out.println("Delete Read 3");
      fr = fs.getFeatures().features();
      int count3 = 0;
      while(fr.hasNext()){
        count3 ++; fr.next();
      }
        fr.close();
View Full Code Here

      assertTrue(count2==count3);
    }
   
    public static void doUpdate(DataStore ds,SimpleFeatureType ft, String attributeToChange, Object newValue ) throws IllegalFilterException, FactoryRegistryException, NoSuchElementException, IOException, IllegalAttributeException{
      Transaction t = new DefaultTransaction();
      SimpleFeatureStore fs = (SimpleFeatureStore)ds.getFeatureSource(ft.getTypeName());
      fs.setTransaction(t);
     
      AttributeDescriptor at = ft.getDescriptor(attributeToChange);
      assertNotNull("Attribute "+attributeToChange+" does not exist",at);
     
      FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
        Filter f = filterFactory.equals(filterFactory.property(at.getLocalName()), filterFactory
                .literal(newValue));

      System.out.println("Update Read 1");
      SimpleFeatureIterator fr = fs.getFeatures(f).features();
     
      int count1 = 0;
      Object oldValue=null;
        if(fr!=null)
      while(fr.hasNext()){
        count1 ++; oldValue=fr.next().getAttribute(attributeToChange);
      }

        fr.close();
      System.out.println("Update Modify");
      fs.modifyFeatures(at,newValue,Filter.INCLUDE);

      System.out.println("Update Read 2");
      fr = fs.getFeatures(f).features();
      int count2 = 0;
      while(fr.hasNext()){
        count2 ++;
//        System.out.println(fr.next());
        fr.next();
      }
        fr.close();
//System.out.println("Read 1 == "+count1+" Read 2 == "+count2);
      assertTrue("Read 1 == "+count1+" Read 2 == "+count2,count2>count1);

      System.out.println("Update Commit");
        try {
            t.commit();

            assertTrue(((WFSTransactionState) t.getState(ds)).getFids(ft.getTypeName()) != null);

            System.out.println("Update Read 3");
            fr = fs.getFeatures(f).features();
            int count3 = 0;
            while( fr.hasNext() ) {
                count3++;
                fr.next();
            }
            fr.close();
            assertEquals(count2, count3);
        } finally {
            // cleanup
            fs.modifyFeatures(at, oldValue, Filter.INCLUDE);
            t.commit();
        }
    }
View Full Code Here

        }

        data.createSchema(roadType);
        data.createSchema(riverType);
       
        SimpleFeatureStore roads;
        roads = ((SimpleFeatureStore) data.getFeatureSource(roadType
                .getTypeName()));
       
        roads.addFeatures(DataUtilities.collection(roadFeatures));
       
        SimpleFeatureStore rivers = ((SimpleFeatureStore) data
                .getFeatureSource(riverType.getTypeName()));
       
        rivers.addFeatures(DataUtilities.collection(riverFeatures));
       
        // Now that we have seeded the contents we need to
        // set up our arrays in the same order
        //       
        roadFeatures = grabArray( roads.getFeatures(), roadFeatures.length );
        riverFeatures = grabArray( rivers.getFeatures(), riverFeatures.length );
    }
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.