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();
}
}
}
}
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;
}
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());
}
}
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());
}
}
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());
}
}
Aggregations