Examples of FeatureWriter


Examples of org.geotools.data.FeatureWriter

            features[i] = SimpleFeatureBuilder.build(schema,
                    new Object[] { gf.createPoint(new Coordinate(i, i)), new Integer(i), "" + i },
                    null);
        }

        FeatureWriter writer = ds.getFeatureWriterAppend("testw", Transaction.AUTO_COMMIT);
        for (int i = 0; i < features.length; i++) {
            assertFalse(writer.hasNext());
            SimpleFeature f = (SimpleFeature) writer.next();
            f.setAttributes(features[i].getAttributes());
            writer.write();
        }
        writer.close();

        FeatureReader reader = ds.getFeatureReader(new Query("testw"), null);
        for (int i = 0; i < features.length; i++) {
            assertTrue(reader.hasNext());
            SimpleFeature f = (SimpleFeature) reader.next();
View Full Code Here

Examples of org.geotools.data.FeatureWriter

    }

    private void writeFeatures(DataStore s, FeatureCollection<SimpleFeatureType, SimpleFeature> fc) throws Exception {
        SimpleFeatureType schema = fc.getSchema();
        s.createSchema(schema);
        FeatureWriter fw = s.getFeatureWriter(s.getTypeNames()[0], Transaction.AUTO_COMMIT);
        FeatureIterator it = fc.features();
        while (it.hasNext()) {
            SimpleFeature sf = (SimpleFeature) it.next();
            ((SimpleFeature) fw.next()).setAttributes(sf.getAttributes());
            fw.write();
        }
        it.close();
        fw.close();
    }
View Full Code Here

Examples of org.geotools.data.FeatureWriter

        tb.add("price", Double.class);
        tb.add("geom", Point.class);
        SimpleFeatureType ft = tb.buildFeatureType();
        ds.createSchema(ft);
       
        FeatureWriter fw = ds.getFeatureWriter("widgets", Transaction.AUTO_COMMIT);
       
        WKTReader wkt = new WKTReader();
       
        fw.hasNext();
        SimpleFeature next = (SimpleFeature) fw.next();
        next.setAttribute("type", "anvil");
        next.setAttribute("price", 10.99);
        next.setAttribute("geom", wkt.read("POINT(12.5 13.7)"));
        fw.write();
       
        fw.hasNext();
        next = (SimpleFeature) fw.next();
        next.setAttribute("type", "dynamite");
        next.setAttribute("price", 99.99);
        next.setAttribute("geom", wkt.read("POINT(11.8 16.7)"));
        fw.write();
       
        fw.close();
       
        Catalog cat = createNiceMock(Catalog.class);
       
        //workspaces
        WorkspaceInfo ws = createNiceMock(WorkspaceInfo.class);
View Full Code Here

Examples of org.geotools.data.FeatureWriter

        tb.add("price", Double.class);
        tb.add("geom", Point.class);
        SimpleFeatureType ft = tb.buildFeatureType();
        ds.createSchema(ft);
       
        FeatureWriter fw = ds.getFeatureWriter("widgets", Transaction.AUTO_COMMIT);
       
        WKTReader wkt = new WKTReader();
       
        fw.hasNext();
        SimpleFeature next = (SimpleFeature) fw.next();
        next.setAttribute("type", "anvil");
        next.setAttribute("price", 10.99);
        next.setAttribute("geom", wkt.read("POINT(12.5 13.7)"));
        fw.write();
       
        fw.hasNext();
        next = (SimpleFeature) fw.next();
        next.setAttribute("type", "dynamite");
        next.setAttribute("price", 99.99);
        next.setAttribute("geom", wkt.read("POINT(11.8 16.7)"));
        fw.write();
       
        fw.close();
       
        Catalog cat = createNiceMock(Catalog.class);
       
        //workspaces
        WorkspaceInfo ws = createNiceMock(WorkspaceInfo.class);
View Full Code Here

Examples of org.geotools.data.FeatureWriter

        tb.add("price", Double.class);
        tb.add("geom", Point.class);
        SimpleFeatureType ft = tb.buildFeatureType();
        ds.createSchema(ft);
       
        FeatureWriter fw = ds.getFeatureWriter("widgets", Transaction.AUTO_COMMIT);
       
        WKTReader wkt = new WKTReader();
       
        fw.hasNext();
        SimpleFeature next = (SimpleFeature) fw.next();
        next.setAttribute("type", "anvil");
        next.setAttribute("price", 10.99);
        next.setAttribute("geom", wkt.read("POINT(12.5 13.7)"));
        fw.write();
       
        fw.hasNext();
        next = (SimpleFeature) fw.next();
        next.setAttribute("type", "dynamite");
        next.setAttribute("price", 99.99);
        next.setAttribute("geom", wkt.read("POINT(11.8 16.7)"));
        fw.write();
       
        fw.close();
       
        Catalog cat = createNiceMock(Catalog.class);
       
        //workspaces
        WorkspaceInfo ws = createNiceMock(WorkspaceInfo.class);
View Full Code Here

Examples of org.geotools.data.FeatureWriter

                SimpleFeatureType featureType = (SimpleFeatureType) fc.getSchema()
                        ;
                //create a feature type
                dataStore.createSchema(featureType);

                FeatureWriter fw = dataStore.getFeatureWriterAppend(
                    featureType.getTypeName(), Transaction.AUTO_COMMIT);

                //Start populating the table: tbl_name.
                SimpleFeatureIterator it = (SimpleFeatureIterator) fc.features();
                while(it.hasNext()) {
                    SimpleFeature f = it.next();
                    SimpleFeature g = (SimpleFeature) fw.next();

                    for (AttributeDescriptor att : f.getFeatureType().getAttributeDescriptors()) {
                        String attName = att.getLocalName();
                        g.setAttribute(attName, f.getAttribute(attName));
                    }
                    fw.write();
                }
            }
        }
        finally {
            dataStore.dispose();
View Full Code Here

Examples of org.geotools.data.FeatureWriter

    void loadIntoDataStore(ImportTask task, DataStoreInfo store, VectorFormat format,
        VectorTransformChain tx) throws Exception {

        ImportData data = task.getData();
        FeatureReader reader = null;
        FeatureWriter writer = null;
        // using this exception to throw at the end
        Exception error = null;
        try {
            reader = format.read(data, task);

            SimpleFeatureType featureType = (SimpleFeatureType) reader.getFeatureType();
            final String featureTypeName = featureType.getName().getLocalPart();
   
            DataStore dataStore = (DataStore) store.getDataStore(null);
            FeatureDataConverter featureDataConverter = FeatureDataConverter.DEFAULT;
            if (isShapefileDataStore(dataStore)) {
                featureDataConverter = FeatureDataConverter.TO_SHAPEFILE;
            }
            else if (isOracleDataStore(dataStore)) {
                featureDataConverter = FeatureDataConverter.TO_ORACLE;
            }
            else if (isPostGISDataStore(dataStore)) {
                featureDataConverter = FeatureDataConverter.TO_POSTGIS;
            }
           
            featureType = featureDataConverter.convertType(featureType, format, data, task);
            UpdateMode updateMode = task.getUpdateMode();
            final String uniquifiedFeatureTypeName;
            if (updateMode == UpdateMode.CREATE) {
                //find a unique type name in the target store
                uniquifiedFeatureTypeName = findUniqueNativeFeatureTypeName(featureType, store);
                task.setOriginalLayerName(featureTypeName);
   
                if (!uniquifiedFeatureTypeName.equals(featureTypeName)) {
                    //update the metadata
                    task.getLayer().getResource().setName(uniquifiedFeatureTypeName);
                    task.getLayer().getResource().setNativeName(uniquifiedFeatureTypeName);
                   
                    //retype
                    SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
                    typeBuilder.setName(uniquifiedFeatureTypeName);
                    typeBuilder.addAll(featureType.getAttributeDescriptors());
                    featureType = typeBuilder.buildFeatureType();
                }
   
                // @todo HACK remove this at some point when timezone issues are fixed
                // this will force postgis to create timezone w/ timestamp fields
                if (dataStore instanceof JDBCDataStore) {
                    JDBCDataStore ds = (JDBCDataStore) dataStore;
                    // sniff for postgis (h2 is used in tests and will cause failure if this occurs)
                    if (ds.getSqlTypeNameToClassMappings().containsKey("timestamptz")) {
                        ds.getSqlTypeToSqlTypeNameOverrides().put(java.sql.Types.TIMESTAMP, "timestamptz");
                    }
                }
   
                //apply the feature type transform
                featureType = tx.inline(task, dataStore, featureType);
   
                dataStore.createSchema(featureType);
            } else {
                // @todo what to do if featureType transform is present?
               
                // @todo implement me - need to specify attribute used for id
                if (updateMode == UpdateMode.UPDATE) {
                    throw new UnsupportedOperationException("updateMode UPDATE is not supported yet");
                }
                uniquifiedFeatureTypeName = featureTypeName;
            }
               
            Transaction transaction = new DefaultTransaction();
           
            if (updateMode == UpdateMode.REPLACE) {
               
                FeatureStore fs = (FeatureStore) dataStore.getFeatureSource(featureTypeName);
                fs.setTransaction(transaction);
                fs.removeFeatures(Filter.INCLUDE);
            }
           
            //start writing features
            // @todo ability to collect transformation errors for use in a dry-run (auto-rollback)
           
            ProgressMonitor monitor = task.progress();
           
            // @todo need better way to communicate to client
            int skipped = 0;
            int cnt = 0;
            // metrics
            long startTime = System.currentTimeMillis();
            task.clearMessages();
           
            task.setTotalToProcess(format.getFeatureCount(task.getData(), task));
           
            LOGGER.info("begining import");
            try {
                writer = dataStore.getFeatureWriterAppend(uniquifiedFeatureTypeName, transaction);
               
                while(reader.hasNext()) {
                    if (monitor.isCanceled()){
                        break;
                    }
                    SimpleFeature feature = (SimpleFeature) reader.next();
                    SimpleFeature next = (SimpleFeature) writer.next();
   
                    //(JD) TODO: some formats will rearrange the geometry type (like shapefile) which
                    // makes the goemetry the first attribute reagardless, so blindly copying over
                    // attributes won't work unless the source type also  has the geometry as the
                    // first attribute in the schema
                    featureDataConverter.convert(feature, next);
                   
                    // @hack #45678 - mask empty geometry or postgis will complain
                    Geometry geom = (Geometry) next.getDefaultGeometry();
                    if (geom != null && geom.isEmpty()) {
                        next.setDefaultGeometry(null);
                    }
                   
                    //apply the feature transform
                    next = tx.inline(task, dataStore, feature, next);
                   
                    if (next == null) {
                        skipped++;
                    } else {
                        writer.write();
                    }
                    task.setNumberProcessed(++cnt);
                }
                transaction.commit();
                if (skipped > 0) {
                    task.addMessage(Level.WARNING,skipped + " features were skipped.");
                }
                LOGGER.info("load to target took " + (System.currentTimeMillis() - startTime));
            }
            catch (Exception e) {
                error = e;
            }
            // no finally block, there is too much to do
           
            if (error != null || monitor.isCanceled()) {
                // all sub exceptions in this catch block should be logged, not thrown
                // as the triggering exception will be thrown
   
                //failure, rollback transaction
                try {
                    transaction.rollback();
                } catch (Exception e1) {
                    LOGGER.log(Level.WARNING, "Error rolling back transaction",e1);
                }
   
                //attempt to drop the type that was created as well
                try {
                    dropSchema(dataStore,featureTypeName);
                } catch(Exception e1) {
                    LOGGER.log(Level.WARNING, "Error dropping schema in rollback",e1);
                }
            }
   
            // try to cleanup, but if an error occurs here and one hasn't already been set, set the error
            try {
                transaction.close();
            } catch (Exception e) {
                if (error != null) {
                    error = e;
                }
                LOGGER.log(Level.WARNING, "Error closing transaction",e);
            }
   
            // @revisit - when this gets disposed, any following uses seem to
            // have a problem where later users of the dataStore get an NPE
            // since the dataStore gets cached by the ResourcePool but is in a
            // closed state???
           
            // do this last in case we have to drop the schema
    //        try {
    //            dataStore.dispose();
    //        } catch (Exception e) {
    //            LOGGER.log(Level.WARNING, "Error closing dataStore",e);
    //        }
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                    if (error != null) {
                        error = e;
                    }
                    LOGGER.log(Level.WARNING, "Error closing writer",e);
View Full Code Here

Examples of org.geotools.data.FeatureWriter

        tb.add( "g", Point.class );
        tb.add( "name", String.class );
       
        ds.createSchema( tb.buildFeatureType() );
       
        FeatureWriter fw = ds.getFeatureWriterAppend( "widgets", Transaction.AUTO_COMMIT );
        fw.hasNext();
        SimpleFeature sf = (SimpleFeature) fw.next();
        sf.setAttribute("g", new GeometryFactory().createPoint( new Coordinate(1,1)));
        sf.setAttribute( "name", "one");
        fw.write();
       
        fw.hasNext();
        sf = (SimpleFeature) fw.next();
        sf.setAttribute("g", new GeometryFactory().createPoint( new Coordinate(2,2)));
        sf.setAttribute( "name", "two");
        fw.write();
       
        fw.close();
        ds.dispose();
    }
View Full Code Here

Examples of org.geotools.data.FeatureWriter

        tb.setName( "widgetsNG" );
        tb.add( "name", String.class );
       
        ds.createSchema( tb.buildFeatureType() );
       
        FeatureWriter fw = ds.getFeatureWriterAppend( "widgetsNG", Transaction.AUTO_COMMIT );
        fw.hasNext();
        SimpleFeature sf = (SimpleFeature) fw.next();
        sf.setAttribute( "name", "one");
        fw.write();
       
        fw.hasNext();
        sf = (SimpleFeature) fw.next();
        sf.setAttribute( "name", "two");
        fw.write();
       
        fw.close();
       
        String xml =
            "<featureType>" +
              "<name>widgetsNG</name>" +
            "</featureType>";
View Full Code Here

Examples of org.geotools.data.FeatureWriter

                            // extra work when doing release mode ALL.
                            //
                            DataStore data = store.getDataStore();
                            FilterFactory factory = FilterFactory
                                .createFilterFactory();
                            FeatureWriter writer;                           
                            writer = data.getFeatureWriter(typeName, filter,
                                    transaction);

                            try {
                                while (writer.hasNext()) {
                                    String fid = writer.next().getID();
                                    locking.unLockFeatures(factory
                                        .createFidFilter(fid));
                                    writer.remove();
                                }
                            } finally {
                                writer.close();
                            }

                            store.removeFeatures(filter);
                        }
                    } else {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.