use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class OSMExportShp method exportRule.
private void exportRule(final MappingRule rule, final GeogigCLI cli, final File shapeFile) throws IOException {
if (shapeFile.exists() && !overwrite) {
throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
}
Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {
@Override
@Nullable
public Optional<Feature> apply(@Nullable Feature feature) {
Optional<Feature> mapped = rule.apply(feature);
return mapped;
}
};
SimpleFeatureType outputFeatureType = rule.getFeatureType();
String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, shapeFile.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
DataStore dataStore = dataStoreFactory.createNewDataStore(params);
try {
dataStore.createSchema(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
checkParameter(source instanceof SimpleFeatureStore, "Could not create feature store. Shapefile may be read only");
final SimpleFeatureStore store = (SimpleFeatureStore) source;
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(store).setPath(path).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
cli.getConsole().println("OSM data exported successfully to " + shapeFile);
} catch (IllegalArgumentException iae) {
shapeFile.delete();
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
shapeFile.delete();
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
} finally {
dataStore.dispose();
}
}
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<SimpleFeature>();
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 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.DataStore 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.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<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;
}
Aggregations