Search in sources :

Example 31 with AttributeDescriptor

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

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

Example 33 with AttributeDescriptor

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

the class WFSIntrospector method getAttrs.

public static List<String> getAttrs(WFSService service) throws IOException {
    List<String> attrs = new LinkedList<>();
    WFSDataStore wfs = createDs(service);
    SimpleFeatureType schema = wfs.getSchema(service.getTypeName());
    List<AttributeDescriptor> attributeDescriptors = schema.getAttributeDescriptors();
    for (AttributeDescriptor desc : attributeDescriptors) {
        String localName = desc.getLocalName();
        attrs.add(localName);
    }
    return attrs;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) WFSDataStore(org.geotools.data.wfs.WFSDataStore) LinkedList(java.util.LinkedList)

Example 34 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project sldeditor by robward-scisys.

the class ExtendedSimpleFeatureTypeBuilder method createAttributeDescriptor.

/**
 * Creates the attribute descriptor.
 *
 * @param name the name
 * @param binding the binding
 * @return the attribute descriptor
 */
public AttributeDescriptor createAttributeDescriptor(String name, Class<?> binding) {
    AttributeDescriptor descriptor = null;
    attributeBuilder.setBinding(binding);
    attributeBuilder.setName(name);
    // 
    if ((defaultGeometry != null && defaultGeometry.equals(name)) || Geometry.class.isAssignableFrom(binding)) {
        // if no crs was set, set to defaultCRS
        if (!attributeBuilder.isCRSSet()) {
            if (defaultCrs == null && !defaultCrsSet) {
                String message = String.format("Creating %s with null CoordinateReferenceSystem - did you mean to setCRS?", name);
                LOGGER.fine(message);
            }
            attributeBuilder.setCRS(defaultCrs);
        }
        GeometryType type = attributeBuilder.buildGeometryType();
        descriptor = attributeBuilder.buildDescriptor(name, type);
    } else {
        AttributeType type = attributeBuilder.buildType();
        descriptor = attributeBuilder.buildDescriptor(name, type);
    }
    return (descriptor);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryType(org.opengis.feature.type.GeometryType) AttributeType(org.opengis.feature.type.AttributeType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor)

Example 35 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project sldeditor by robward-scisys.

the class SLDEditorBufferedImageLegendGraphicBuilder method cloneWithDimensionality.

/**
 * Clones the given schema, changing the geometry attribute to match the given dimensionality.
 *
 * @param schema schema to clone
 * @param dimensionality dimensionality for the geometry 1= points, 2= lines, 3= polygons
 */
private FeatureType cloneWithDimensionality(FeatureType schema, int dimensionality) {
    SimpleFeatureType simpleFt = (SimpleFeatureType) schema;
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(schema.getName());
    builder.setCRS(schema.getCoordinateReferenceSystem());
    for (AttributeDescriptor desc : simpleFt.getAttributeDescriptors()) {
        if (isMixedGeometry(desc)) {
            GeometryDescriptor geomDescriptor = (GeometryDescriptor) desc;
            GeometryType geomType = geomDescriptor.getType();
            Class<?> geometryClass = getGeometryForDimensionality(dimensionality);
            GeometryType gt = new GeometryTypeImpl(geomType.getName(), geometryClass, geomType.getCoordinateReferenceSystem(), geomType.isIdentified(), geomType.isAbstract(), geomType.getRestrictions(), geomType.getSuper(), geomType.getDescription());
            builder.add(new GeometryDescriptorImpl(gt, geomDescriptor.getName(), geomDescriptor.getMinOccurs(), geomDescriptor.getMaxOccurs(), geomDescriptor.isNillable(), geomDescriptor.getDefaultValue()));
        } else {
            builder.add(desc);
        }
    }
    schema = builder.buildFeatureType();
    return schema;
}
Also used : GeometryDescriptorImpl(org.geotools.feature.type.GeometryDescriptorImpl) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) GeometryType(org.opengis.feature.type.GeometryType) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) GeometryTypeImpl(org.geotools.feature.type.GeometryTypeImpl)

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