Search in sources :

Example 16 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project hale by halestudio.

the class ShapefileInstanceWriter method writeToFile.

/**
 * Final step to write to the shape file using transaction.
 *
 * @param schemaDataStoreMap data store for the shape file.
 * @param schemaFtMap used as a template to describe the file contents.
 * @param schemaFeaturesMap for each schema, each geom list of features to
 *            be written to the shape file.
 * @throws IOException if any.
 */
private void writeToFile(Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap, Map<String, Map<String, SimpleFeatureType>> schemaFtMap, Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap) throws IOException {
    // extract each schema
    for (Entry<String, Map<String, ShapefileDataStore>> schemaEntry : schemaDataStoreMap.entrySet()) {
        String localPart = schemaEntry.getKey();
        // extract each geometry.
        for (Entry<String, ShapefileDataStore> geomEntry : schemaEntry.getValue().entrySet()) {
            Transaction transaction = new DefaultTransaction(ShapefileConstants.CREATE_CONSTANT);
            String typeName = geomEntry.getValue().getTypeNames()[0];
            SimpleFeatureSource geomSpecificFeatureSource = geomEntry.getValue().getFeatureSource(typeName);
            if (geomSpecificFeatureSource instanceof SimpleFeatureStore) {
                SimpleFeatureStore geomSpecificFeatureStore = (SimpleFeatureStore) geomSpecificFeatureSource;
                // create collection to write to the shape file.
                SimpleFeatureCollection collection = new ListFeatureCollection(schemaFtMap.get(localPart).get(geomEntry.getKey()), schemaFeaturesMap.get(localPart).get(geomEntry.getKey()));
                geomSpecificFeatureStore.setTransaction(transaction);
                try {
                    geomSpecificFeatureStore.addFeatures(collection);
                    transaction.commit();
                } catch (IOException e) {
                    transaction.rollback();
                    throw e;
                } finally {
                    transaction.close();
                }
            } else {
                // throw exception
                transaction.close();
                throw new IOException(typeName + " does not support read/write access");
            }
        }
    }
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection)

Example 17 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExport method writeToShapefile.

public boolean writeToShapefile() throws MalformedURLException, IOException {
    boolean success = false;
    // SimpleFeatureIterator features = simpleFeatureCollection.features();
    SimpleFeatureType type = buildFeatureType();
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
    File shpFile = checkAndCreateFile();
    Map datastoreConfig = new HashMap<>();
    datastoreConfig.put("url", shpFile.toURI().toURL());
    ShapefileDataStore shpfileDataStore = (ShapefileDataStore) factory.createNewDataStore(datastoreConfig);
    shpfileDataStore.createSchema(type);
    shpfileDataStore.forceSchemaCRS(this.crs);
    // DataStore dataStore = factory.createNewDataStore(datastoreConfig);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) shpfileDataStore.getFeatureSource(type.getName());
    Transaction t = new DefaultTransaction();
    SimpleFeatureIterator fi = null;
    try {
        // Copied directly from Import process
        featureStore.setTransaction(t);
        fi = simpleFeatureCollection.features();
        SimpleFeatureBuilder fb = new SimpleFeatureBuilder(type);
        while (fi.hasNext()) {
            SimpleFeature source = fi.next();
            fb.reset();
            for (AttributeDescriptor desc : type.getAttributeDescriptors()) {
                Name attributeName = desc.getName();
                Object attributeValue = source.getAttribute(attributeName);
                if (null == attributeValue) {
                    attributeValue = NULL_PLACEHOLDER;
                }
                fb.set(attributeName, attributeValue);
            }
            SimpleFeature target = fb.buildFeature(null);
            featureStore.addFeatures(DataUtilities.collection(target));
        }
        // successful if it made it this far
        success = true;
    } finally {
        t.commit();
        t.close();
        IOUtils.closeQuietly(fi);
    }
    return success;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Name(org.opengis.feature.type.Name) FileDataStoreFactorySpi(org.geotools.data.FileDataStoreFactorySpi) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 18 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExportTest method testHttpComponentsGetter.

@Test
@Ignore
public void testHttpComponentsGetter() throws Exception {
    HttpComponentsWFSClient wfs = new HttpComponentsWFSClient();
    wfs.setupDatastoreFromEndpoint("http://cida-wiwsc-cchdev:8081/geoserver/wfs");
    // FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
    SimpleFeatureCollection featureCollection = wfs.getFeatureCollection("proxied:atl_cvi", null);
    SimpleFeatureType schema = GMLStreamingFeatureCollection.unwrapSchema(featureCollection.getSchema());
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
    Map datastoreConfig = new HashMap<>();
    datastoreConfig.put("url", FileUtils.getFile(FileUtils.getTempDirectory(), "test3.shp").toURI().toURL());
    ShapefileDataStore shpfileDataStore = (ShapefileDataStore) factory.createNewDataStore(datastoreConfig);
    shpfileDataStore.createSchema(schema);
    shpfileDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) shpfileDataStore.getFeatureSource();
    Transaction t = new DefaultTransaction();
    // Copied directly from Import process
    featureStore.setTransaction(t);
    Query query = new Query();
    query.setCoordinateSystem(DefaultGeographicCRS.WGS84);
    SimpleFeatureIterator fi = featureCollection.features();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder(schema);
    while (fi.hasNext()) {
        SimpleFeature source = fi.next();
        fb.reset();
        for (AttributeDescriptor desc : schema.getAttributeDescriptors()) {
            fb.set(desc.getName(), source.getAttribute(desc.getName()));
        }
        SimpleFeature target = fb.buildFeature(null);
        target.setDefaultGeometry(source.getDefaultGeometry());
        featureStore.addFeatures(DataUtilities.collection(target));
    }
    t.commit();
    t.close();
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Query(org.geotools.data.Query) HashMap(java.util.HashMap) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) FileDataStoreFactorySpi(org.geotools.data.FileDataStoreFactorySpi) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExportTest method splitterTest.

@Test
// for now
@Ignore
public void splitterTest() throws Exception {
    // get geometry
    HttpComponentsWFSClient wfs1 = new HttpComponentsWFSClient();
    wfs1.setupDatastoreFromEndpoint("http://cida-wiwsc-cchdev:8081/geoserver/wfs");
    FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
    PropertyIsEqualTo equals = filterFactory.equals(filterFactory.property("STATEFP"), filterFactory.literal(37));
    SimpleFeatureCollection featureCollection = wfs1.getFeatureCollection("splitter:tl_2013_coastal_states", equals);
    SimpleFeatureIterator features = featureCollection.features();
    // only deal with one
    Geometry geom = null;
    if (features.hasNext()) {
        SimpleFeature next = features.next();
        geom = (Geometry) next.getDefaultGeometry();
    }
    // then use geometry as filter
    HttpComponentsWFSClient wfs2 = new HttpComponentsWFSClient();
    wfs2.setupDatastoreFromEndpoint("http://cida-wiwsc-cchdev:8081/geoserver/wfs");
    FilterFactory2 filterFactory2 = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
    Contains contains = filterFactory2.contains(filterFactory2.property("the_geom"), filterFactory2.literal(geom));
    SimpleFeatureCollection featureCollection2 = wfs2.getFeatureCollection("proxied:atl_cvi", contains);
    SimpleFeatureType schema = GMLStreamingFeatureCollection.unwrapSchema(featureCollection2.getSchema());
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
    Map datastoreConfig = new HashMap<>();
    datastoreConfig.put("url", FileUtils.getFile(FileUtils.getTempDirectory(), "splitter.shp").toURI().toURL());
    ShapefileDataStore shpfileDataStore = (ShapefileDataStore) factory.createNewDataStore(datastoreConfig);
    shpfileDataStore.createSchema(schema);
    shpfileDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) shpfileDataStore.getFeatureSource();
    Transaction t = new DefaultTransaction();
    // Copied directly from Import process
    featureStore.setTransaction(t);
    Query query = new Query();
    query.setCoordinateSystem(DefaultGeographicCRS.WGS84);
    SimpleFeatureIterator fi = featureCollection2.features();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder(schema);
    while (fi.hasNext()) {
        SimpleFeature source = fi.next();
        fb.reset();
        for (AttributeDescriptor desc : schema.getAttributeDescriptors()) {
            fb.set(desc.getName(), source.getAttribute(desc.getName()));
        }
        SimpleFeature target = fb.buildFeature(null);
        target.setDefaultGeometry(source.getDefaultGeometry());
        featureStore.addFeatures(DataUtilities.collection(target));
    }
    t.commit();
    t.close();
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Query(org.geotools.data.Query) HashMap(java.util.HashMap) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Geometry(com.vividsolutions.jts.geom.Geometry) FileDataStoreFactorySpi(org.geotools.data.FileDataStoreFactorySpi) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) PropertyIsEqualTo(org.opengis.filter.PropertyIsEqualTo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) Contains(org.opengis.filter.spatial.Contains) FilterFactory2(org.opengis.filter.FilterFactory2) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExportTest method testWriteSolo.

@Test
@Ignore
public void testWriteSolo() throws Exception {
    WFSDataStoreFactory datastore = new WFSDataStoreFactory();
    Map params = new HashMap<>();
    params.put(WFSDataStoreFactory.URL.key, new URL("http://coastalmap.marine.usgs.gov/cmgp/National/cvi_WFS/MapServer/WFSServer?service=WFS&request=GetCapabilities&version=1.0.0"));
    params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "arcgis");
    params.put(WFSDataStoreFactory.TIMEOUT.key, 15000);
    params.put(WFSDataStoreFactory.TRY_GZIP.key, "true");
    WFSDataStore wfs = datastore.createDataStore(params);
    String[] typeNames = wfs.getTypeNames();
    SimpleFeatureSource featureSource = wfs.getFeatureSource(typeNames[2]);
    SimpleFeatureType schema = featureSource.getSchema();
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
    Map datastoreConfig = new HashMap<>();
    datastoreConfig.put("url", FileUtils.getFile(FileUtils.getTempDirectory(), "test3.shp").toURI().toURL());
    ShapefileDataStore shpfileDataStore = (ShapefileDataStore) factory.createNewDataStore(datastoreConfig);
    shpfileDataStore.createSchema(schema);
    shpfileDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) shpfileDataStore.getFeatureSource();
    Transaction t = new DefaultTransaction();
    // Copied directly from Import process
    featureStore.setTransaction(t);
    Query query = new Query();
    query.setCoordinateSystem(DefaultGeographicCRS.WGS84);
    SimpleFeatureIterator fi = featureSource.getFeatures(query).features();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder(schema);
    while (fi.hasNext()) {
        SimpleFeature source = fi.next();
        fb.reset();
        for (AttributeDescriptor desc : schema.getAttributeDescriptors()) {
            fb.set(desc.getName(), source.getAttribute(desc.getName()));
        }
        SimpleFeature target = fb.buildFeature(null);
        target.setDefaultGeometry(source.getDefaultGeometry());
        featureStore.addFeatures(DataUtilities.collection(target));
    }
    t.commit();
    t.close();
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Query(org.geotools.data.Query) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) WFSDataStore(org.geotools.data.wfs.WFSDataStore) URL(java.net.URL) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FileDataStoreFactorySpi(org.geotools.data.FileDataStoreFactorySpi) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) WFSDataStoreFactory(org.geotools.data.wfs.WFSDataStoreFactory) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DefaultTransaction (org.geotools.data.DefaultTransaction)20 Transaction (org.geotools.data.Transaction)17 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Test (org.junit.Test)12 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)9 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 FeatureId (org.opengis.filter.identity.FeatureId)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Query (org.geotools.data.Query)5 ObjectId (org.locationtech.geogig.api.ObjectId)5 IOException (java.io.IOException)4 FileDataStoreFactorySpi (org.geotools.data.FileDataStoreFactorySpi)4 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)4 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)4 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)4 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)4 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 Ignore (org.junit.Ignore)3