Search in sources :

Example 26 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project polymap4-core by Polymap4.

the class UnitOfWork method isFeatureModified.

private boolean isFeatureModified(Feature feature, Feature original) throws IOException {
    // XXX complex features
    SimpleFeatureType schema = ((SimpleFeature) original).getType();
    for (AttributeDescriptor attribute : schema.getAttributeDescriptors()) {
        Object value1 = ((SimpleFeature) feature).getAttribute(attribute.getName());
        Object value2 = ((SimpleFeature) original).getAttribute(attribute.getName());
        if (isPropertyModified(value1, value2)) {
            return true;
        }
    }
    return false;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 27 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor 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 28 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExport method buildFeatureType.

private SimpleFeatureType buildFeatureType() {
    SimpleFeatureType featureType = null;
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(namePrefix);
    builder.setCRS(this.crs);
    builder.add(getGeometryDescriptor());
    if (downloadAll) {
        SimpleFeatureType unwrapped = GMLStreamingFeatureCollection.unwrapSchema(simpleFeatureCollection.getSchema());
        List<AttributeDescriptor> attributeDescriptors = unwrapped.getAttributeDescriptors();
        for (AttributeDescriptor attrDesc : attributeDescriptors) {
            if (attrDesc != null && !IGNORE_LENGTH.equalsIgnoreCase(attrDesc.getLocalName())) {
                builder.add(attrDesc);
            }
        }
    } else {
        for (String name : attributes) {
            AttributeDescriptor descriptor = getDescriptorFromPrototype(name);
            if (descriptor != null) {
                builder.add(descriptor);
            }
        }
    }
    featureType = builder.buildFeatureType();
    return featureType;
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor)

Example 29 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project coastal-hazards by USGS-CIDA.

the class GMLStreamingFeatureCollection method unwrapSchema.

public static SimpleFeatureType unwrapSchema(SimpleFeatureType wrappedFeatureType) {
    List<AttributeDescriptor> allowable = new ArrayList<>();
    List<String> disallowed = new ArrayList<>();
    disallowed.add("metaDataProperty");
    disallowed.add("boundedBy");
    disallowed.add("name");
    disallowed.add("location");
    disallowed.add("description");
    disallowed.add("descriptio");
    for (AttributeDescriptor desc : wrappedFeatureType.getAttributeDescriptors()) {
        if (!disallowed.contains(desc.getLocalName())) {
            allowable.add(desc);
        }
    }
    SimpleFeatureType unwrapped = new SimpleFeatureTypeImpl(wrappedFeatureType.getName(), allowable, wrappedFeatureType.getGeometryDescriptor(), wrappedFeatureType.isAbstract(), wrappedFeatureType.getRestrictions(), wrappedFeatureType.getSuper(), wrappedFeatureType.getDescription());
    return unwrapped;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) SimpleFeatureTypeImpl(org.geotools.feature.simple.SimpleFeatureTypeImpl)

Example 30 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor 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)

Aggregations

AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)40 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)22 SimpleFeature (org.opengis.feature.simple.SimpleFeature)10 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)9 ArrayList (java.util.ArrayList)8 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)7 HashMap (java.util.HashMap)6 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)6 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)6 IOException (java.io.IOException)5 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)5 Test (org.junit.Test)5 File (java.io.File)4 Map (java.util.Map)4 DefaultTransaction (org.geotools.data.DefaultTransaction)4 FileDataStoreFactorySpi (org.geotools.data.FileDataStoreFactorySpi)4 Transaction (org.geotools.data.Transaction)4 GeometryDescriptor (org.opengis.feature.type.GeometryDescriptor)4