use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.
the class InLineFeatureModel method addNewFeature.
/**
* Adds the new feature.
*/
public void addNewFeature() {
SimpleFeatureType featureType = userLayer.getInlineFeatureType();
String typeName = userLayer.getInlineFeatureType().getTypeName();
try {
SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
SimpleFeatureIterator it = featureSource.getFeatures().features();
try {
while (it.hasNext()) {
SimpleFeature sf = it.next();
List<Object> attributeValueList = sf.getAttributes();
sfb.addAll(attributeValueList);
featureList.add(sfb.buildFeature(null));
}
// Add new feature
String wktString = "wkt://POINT(0 0)";
Geometry geometry = WKTConversion.convertToGeometry(wktString, getSelectedCRSCode());
sfb.add(geometry);
featureList.add(sfb.buildFeature(null));
} finally {
it.close();
}
SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
featureCollection = collection;
cachedFeature = null;
lastRow = -1;
DataStore dataStore = DataUtilities.dataStore(collection);
userLayer.setInlineFeatureDatastore(dataStore);
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
}
this.fireTableStructureChanged();
this.fireTableDataChanged();
if (parentObj != null) {
parentObj.inlineFeatureUpdated();
}
}
use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.
the class InLineFeatureModel method addNewColumn.
/**
* Adds the new column.
*/
public void addNewColumn() {
if (featureCollection != null) {
String attributeName = getUniqueAttributeName();
columnList.add(attributeName);
// Populate field names
SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
featureTypeBuilder.init(featureCollection.getSchema());
featureTypeBuilder.add(attributeName, String.class);
SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
String typeName = userLayer.getInlineFeatureType().getTypeName();
try {
SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
SimpleFeatureIterator it = featureSource.getFeatures().features();
try {
while (it.hasNext()) {
SimpleFeature sf = it.next();
sfb.addAll(sf.getAttributes());
sfb.add(new String(""));
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.simple.SimpleFeatureSource in project sldeditor by robward-scisys.
the class InLineFeatureModel method populate.
/**
* Populate.
*
* @param userLayer the user layer
*/
public void populate(UserLayer userLayer) {
this.userLayer = userLayer;
featureCollection = null;
geometryFieldIndex = -1;
columnList.clear();
if (userLayer != null) {
if (userLayer.getInlineFeatureType() != null) {
String typeName = userLayer.getInlineFeatureType().getTypeName();
try {
SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
if (featureSource != null) {
featureCollection = featureSource.getFeatures();
}
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
}
if (featureCollection != null) {
// Populate field names
List<AttributeDescriptor> descriptorList = featureCollection.getSchema().getAttributeDescriptors();
int index = 0;
for (AttributeDescriptor descriptor : descriptorList) {
if (descriptor instanceof GeometryDescriptorImpl) {
geometryFieldIndex = index;
}
columnList.add(descriptor.getLocalName());
index++;
}
}
}
}
this.fireTableStructureChanged();
this.fireTableDataChanged();
// Set up the editor to handle editing the table cells
InlineCellEditor editor = new InlineCellEditor(this);
if (featureTable != null) {
if (featureTable.getColumnModel().getColumnCount() > 0) {
TableColumn column = featureTable.getColumnModel().getColumn(0);
column.setCellEditor(editor);
featureTable.setCellEditor(editor);
}
}
}
use of org.geotools.data.simple.SimpleFeatureSource 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<DataSourceInfo>();
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) {
password = "dummy password";
dataSourceProperties.setPassword(password);
map = dataSourceProperties.getConnectionProperties();
}
}
DataStore dataStore = null;
try {
dataStore = DataStoreFinder.getDataStore(map);
if (dataStore != null) {
// Try connecting to a vector data source
dsInfo.setTypeName(typeName);
SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
SimpleFeatureType schema = source.getSchema();
if (schema.getCoordinateReferenceSystem() == null) {
// No crs found to set a default and reload
if (dataStore instanceof ShapefileDataStore) {
ShapefileDataStore shapeFileDatastore = (ShapefileDataStore) dataStore;
CoordinateReferenceSystem crs = JCRSChooser.showDialog(Localisation.getString(CreateExternalDataSource.class, "CRSPanel.title"), defaultCRS.getIdentifiers().iterator().next().toString());
if (crs != null) {
shapeFileDatastore.forceSchemaCRS(crs);
}
source = dataStore.getFeatureSource(typeName);
schema = source.getSchema();
}
}
dsInfo.setSchema(schema);
determineGeometryType(schema.getGeometryDescriptor().getType());
} else {
// Try connecting to a raster data source
Object rasterFilename = map.get(DataSourceConnectorInterface.FILE_MAP_KEY);
if (rasterFilename != null) {
File rasterFile = new File(ExternalFilenames.convertURLToFile((String) rasterFilename));
ChooseRasterFormatInterface panel = new ChooseRasterFormatPanel(Controller.getInstance().getFrame());
AbstractGridFormat format = DetermineRasterFormat.choose(rasterFile, panel);
AbstractGridCoverage2DReader reader = format.getReader(rasterFile);
dsInfo.setGridCoverageReader(reader);
} else {
logger.error("No matching datastore");
}
}
} 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.simple.SimpleFeatureSource in project sldeditor by robward-scisys.
the class CreateInlineDataSource method connect.
/**
* Creates the data source.
*
* @param typeName the type name
* @param geometryFieldName the geometry field name
* @param editorFile the editor file
* @return the list of data stores
*/
@Override
public List<DataSourceInfo> connect(String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
for (DataSourceInfo dsInfo : dataSourceInfoList) {
dsInfo.reset();
}
dataSourceInfoList.clear();
if (editorFile != null) {
StyledLayerDescriptor sld = editorFile.getSLD();
List<UserLayer> userLayerList = InlineFeatureUtils.extractUserLayers(sld);
for (UserLayer userLayer : userLayerList) {
DataSourceInfo dsInfo = new DataSourceInfo();
dsInfo.setUserLayer(userLayer);
dataSourceInfoList.add(dsInfo);
DataStore dataStore = userLayer.getInlineFeatureDatastore();
if (dataStore == null) {
continue;
}
try {
List<Name> nameList = dataStore.getNames();
if (!nameList.isEmpty()) {
typeName = nameList.get(0).getLocalPart();
}
// Set the type name
dsInfo.setTypeName(typeName);
SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
SimpleFeatureType schema = source.getSchema();
dsInfo.setSchema(schema);
dsInfo.setDataStore(dataStore);
GeometryTypeEnum geometryType = InlineFeatureUtils.determineGeometryType(schema.getGeometryDescriptor(), source.getFeatures());
dsInfo.setGeometryType(geometryType);
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
dsInfo.reset();
}
}
}
return dataSourceInfoList;
}
Aggregations