Search in sources :

Example 46 with DataStore

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

the class InLineFeatureModel method removeColumn.

/**
 * Removes the column.
 *
 * @param columnName the column name
 */
public void removeColumn(String columnName) {
    if (featureCollection != null) {
        if (columnList.contains(columnName)) {
            columnList.remove(columnName);
            // Find field name to remote
            SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
            featureTypeBuilder.init(featureCollection.getSchema());
            featureTypeBuilder.remove(columnName);
            SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
            int attributeToRemoveIndex = 0;
            for (AttributeDescriptor descriptor : newFeatureType.getAttributeDescriptors()) {
                if (descriptor.getLocalName().compareTo(columnName) == 0) {
                    break;
                }
                attributeToRemoveIndex++;
            }
            String typeName = userLayer.getInlineFeatureType().getTypeName();
            try {
                SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
                SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
                ArrayList<SimpleFeature> featureList = new ArrayList<>();
                SimpleFeatureIterator it = featureSource.getFeatures().features();
                try {
                    while (it.hasNext()) {
                        SimpleFeature sf = it.next();
                        List<Object> attributes = sf.getAttributes();
                        attributes.remove(attributeToRemoveIndex);
                        sfb.addAll(attributes);
                        featureList.add(sfb.buildFeature(null));
                    }
                } finally {
                    it.close();
                }
                SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
                featureCollection = collection;
                cachedFeature = null;
                lastRow = -1;
                DataStore dataStore = DataUtilities.dataStore(collection);
                userLayer.setInlineFeatureDatastore(dataStore);
                userLayer.setInlineFeatureType(newFeatureType);
            } catch (IOException e) {
                ConsoleManager.getInstance().exception(this, e);
            }
            this.fireTableStructureChanged();
            this.fireTableDataChanged();
            if (parentObj != null) {
                parentObj.inlineFeatureUpdated();
            }
        }
    }
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 47 with DataStore

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

the class CreateExternalDataSource method connect.

/**
 * Connect.
 *
 * @param typeName the type name
 * @param geometryFieldName the geometry field name
 * @param editorFile the editor file
 * @return the list of datastores
 */
@Override
public List<DataSourceInfo> connect(String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
    List<DataSourceInfo> dataSourceInfoList = new ArrayList<>();
    dataSourceInfoList.add(dsInfo);
    dsInfo.reset();
    if (editorFile != null) {
        SLDDataInterface sldData = editorFile.getSLDData();
        DataSourcePropertiesInterface dataSourceProperties = sldData.getDataSourceProperties();
        Map<String, Object> map = dataSourceProperties.getConnectionProperties();
        if (dataSourceProperties.hasPassword()) {
            String password = dataSourceProperties.getPassword();
            if (password == null) {
                dataSourceProperties.setPassword("dummy password");
                map = dataSourceProperties.getConnectionProperties();
            }
        }
        DataStore dataStore = null;
        try {
            dataStore = DataStoreFinder.getDataStore(map);
            if (dataStore != null) {
                // Try connecting to a vector data source
                connectToVectorDataSource(typeName, dataStore);
            } else {
                // Try connecting to a raster data source
                connectToRasterDataSource(map);
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        dsInfo.setDataStore(dataStore);
        if (!dsInfo.hasData()) {
            ConsoleManager.getInstance().error(this, Localisation.getField(CreateExternalDataSource.class, "CreateExternalDataSource.failedToConnect") + dataSourceProperties.getDebugConnectionString());
        }
    }
    return dataSourceInfoList;
}
Also used : SLDDataInterface(com.sldeditor.common.SLDDataInterface) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) DataStore(org.geotools.data.DataStore) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface)

Example 48 with DataStore

use of org.geotools.data.DataStore 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.jupiter.api.Test)

Example 49 with DataStore

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

the class DataSourceInfoTest method testDataStore.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getDataStore()}. Test
 * method for {@link
 * com.sldeditor.datasource.impl.DataSourceInfo#setDataStore(org.geotools.data.DataStore)}. Test
 * method for {@link com.sldeditor.datasource.impl.DataSourceInfo#unloadDataStore()}.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testDataStore() {
    File file = new File("example.shp");
    Map map = new HashMap();
    try {
        map.put("url", file.toURI().toURL());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    try {
        DataStore dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo ds = new DataSourceInfo();
        ds.setDataStore(dataStore);
        assertEquals(dataStore, ds.getDataStore());
        ds.unloadDataStore();
        ds.reset();
        assertNull(ds.getDataStore());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) DataStore(org.geotools.data.DataStore) IOException(java.io.IOException) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 50 with DataStore

use of org.geotools.data.DataStore 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.jupiter.api.Test)

Aggregations

DataStore (org.geotools.data.DataStore)52 IOException (java.io.IOException)28 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)28 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)22 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)22 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)21 HashMap (java.util.HashMap)10 Map (java.util.Map)10 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)8 Serializable (java.io.Serializable)7 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)7 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)6 DataSourceInfo (com.sldeditor.datasource.impl.DataSourceInfo)6 URL (java.net.URL)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)5