Search in sources :

Example 46 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class VectorReader method createSLDData.

/**
 * Creates the SLD data.
 *
 * @param map the map
 * @return the styled layer descriptor
 */
private StyledLayerDescriptor createSLDData(Map<String, Object> map, String featureClass) {
    StyledLayerDescriptor sld = null;
    DataStore dataStore = null;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
        return null;
    }
    if (dataStore != null) {
        // Try connecting to a vector data source
        String typeName;
        GeometryTypeEnum geometryTypeEnum = GeometryTypeEnum.UNKNOWN;
        try {
            if (featureClass == null) {
                typeName = dataStore.getTypeNames()[0];
            } else {
                typeName = featureClass;
            }
            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
            SimpleFeatureType schema = source.getSchema();
            GeometryType geometryType = schema.getGeometryDescriptor().getType();
            Class<?> bindingType = geometryType.getBinding();
            geometryTypeEnum = GeometryTypeMapping.getGeometryType(bindingType);
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
            return null;
        } finally {
            dataStore.dispose();
        }
        switch(geometryTypeEnum) {
            case POINT:
                sld = DefaultSymbols.createNewPoint();
                break;
            case LINE:
                sld = DefaultSymbols.createNewLine();
                break;
            case POLYGON:
                sld = DefaultSymbols.createNewPolygon();
                break;
            default:
                break;
        }
    }
    return sld;
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) IOException(java.io.IOException) GeometryTypeEnum(com.sldeditor.datasource.impl.GeometryTypeEnum)

Example 47 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class DataSourceInfoTest method testGetFeatureStore.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatureStore()}. Test
 * method for
 * {@link com.sldeditor.datasource.impl.DataSourceInfo#setSchema(org.opengis.feature.type.FeatureType)}.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testGetFeatureStore() {
    URL url = SLDEditorFile.class.getClassLoader().getResource("point/sld/shp/sld_cookbook_point.shp");
    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo dsInfo = new DataSourceInfo();
        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        dsInfo.setSchema(schema);
        assertNull(dsInfo.getFeatureStore());
        dsInfo.setDataStore(dataStore);
        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = dsInfo.getFeatureStore();
        assertTrue(featureStore != null);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 48 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class DataSourceInfoTest method testGetPropertyDescriptorList.

/**
 * Test method for
 * {@link com.sldeditor.datasource.impl.DataSourceInfo#getPropertyDescriptorList()}.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testGetPropertyDescriptorList() {
    URL url = SLDEditorFile.class.getClassLoader().getResource("point/sld/shp/sld_cookbook_point.shp");
    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo dsInfo = new DataSourceInfo();
        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        assertNull(dsInfo.getPropertyDescriptorList());
        dsInfo.setSchema(schema);
        Collection<PropertyDescriptor> fieldList = dsInfo.getPropertyDescriptorList();
        assertTrue(fieldList.size() == 3);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 49 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project mkgmap by openstreetmap.

the class PrecompSeaGenerator method createShapefileAccess.

private void createShapefileAccess() throws IOException {
    Map<String, URL> map = new HashMap<String, URL>();
    map.put("url", shapeFile.toURI().toURL());
    DataStore dataStore = DataStoreFinder.getDataStore(map);
    String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
    shapeCollection = source.getFeatures();
}
Also used : HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) URL(java.net.URL)

Example 50 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project activityinfo by bedatadriven.

the class ImportView method updateGeometry.

private void updateGeometry(GeoAdminClient client, Map<ResourceId, ResourceId> idMap) throws IOException {
    FeatureSourceStorageProvider catalog = new FeatureSourceStorageProvider();
    FeatureSourceStorage formStorage = (FeatureSourceStorage) catalog.getForm(getModel().getSourceFormId().get()).get();
    ResourceId targetFormId = getModel().getTargetFormId().get();
    FieldProfile targetField = getTargetProfile().get().getGeometryField();
    if (targetField == null) {
        System.err.println("No geometry field to update.");
        return;
    }
    int sourceIndex = formStorage.getGeometryAttributeIndex();
    if (sourceIndex == -1) {
        System.err.println("No source geometry field.");
        return;
    }
    SimpleFeatureSource featureSource = formStorage.getFeatureSource();
    GeometryType geometryType = (GeometryType) featureSource.getSchema().getDescriptor(sourceIndex).getType();
    GeometryConverter converter = new GeometryConverter(geometryType);
    SimpleFeatureIterator it = featureSource.getFeatures().features();
    while (it.hasNext()) {
        SimpleFeature feature = it.next();
        ResourceId sourceId = ResourceId.valueOf(feature.getID());
        ResourceId targetId = idMap.get(sourceId);
        if (targetId != null) {
            Geometry geometry = converter.toWgs84(feature.getAttribute(sourceIndex));
            System.out.print("Updating geometry for " + targetId + " [" + geometry.getGeometryType() + "] ... ");
            try {
                client.updateGeometry(targetFormId, targetId, targetField.getId(), geometry);
                System.out.println("OK");
            } catch (Exception e) {
                System.out.println("ERROR: " + e.getMessage());
            }
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) FeatureSourceStorage(org.activityinfo.geoadmin.source.FeatureSourceStorage) GeometryType(org.opengis.feature.type.GeometryType) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ResourceId(org.activityinfo.model.resource.ResourceId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) FieldProfile(org.activityinfo.geoadmin.merge2.view.profile.FieldProfile) GeometryConverter(org.activityinfo.geoadmin.source.GeometryConverter) FeatureSourceStorageProvider(org.activityinfo.geoadmin.source.FeatureSourceStorageProvider) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException)

Aggregations

SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)54 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)29 IOException (java.io.IOException)28 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)25 SimpleFeature (org.opengis.feature.simple.SimpleFeature)23 DataStore (org.geotools.data.DataStore)22 Test (org.junit.Test)21 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)19 Feature (org.opengis.feature.Feature)16 HashMap (java.util.HashMap)15 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)13 URL (java.net.URL)10 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)10 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8