use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class GeoJsonExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String geojson = args.get(1);
File file = new File(geojson);
if (file.exists() && !overwrite) {
throw new CommandFailedException("The selected GeoJSON file already exists. Use -o to overwrite");
}
SimpleFeatureType outputFeatureType;
ObjectId featureTypeId;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
outputFeatureType = getFeatureType(path, cli);
featureTypeId = null;
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
}
DataStore dataStore = new MemoryDataStore(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
op.setTransactional(false);
if (defaultType) {
op.exportDefaultFeatureType();
}
FileWriter writer = null;
try {
op.setProgressListener(cli.getProgressListener()).call();
FeatureJSON fjson = new FeatureJSON();
@SuppressWarnings("rawtypes") FeatureCollection fc = featureSource.getFeatures();
writer = new FileWriter(file);
fjson.writeFeatureCollection(fc, writer);
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
file.delete();
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("Error: The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
} finally {
writer.flush();
writer.close();
}
cli.getConsole().println(path + " exported successfully to " + geojson);
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class SQLServerImport method runInternal.
/**
* Executes the import command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
DataStore dataStore = getDataStore();
try {
cli.getConsole().println("Importing from database " + commonArgs.database);
ProgressListener progressListener = cli.getProgressListener();
cli.getGeogig().command(ImportOp.class).setAll(all).setTable(table).setAlter(alter).setDestinationPath(destTable).setOverwrite(!add).setDataStore(dataStore).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
cli.getConsole().println("Import successful.");
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case TABLE_NOT_DEFINED:
throw new CommandFailedException("No tables specified for import. Specify --all or --table <table>.", e);
case ALL_AND_TABLE_DEFINED:
throw new CommandFailedException("Specify --all or --table <table>, both cannot be set.", e);
case NO_FEATURES_FOUND:
throw new CommandFailedException("No features were found in the database.", e);
case TABLE_NOT_FOUND:
throw new CommandFailedException("Could not find the specified table.", e);
case UNABLE_TO_GET_NAMES:
throw new CommandFailedException("Unable to get feature types from the database.", e);
case UNABLE_TO_GET_FEATURES:
throw new CommandFailedException("Unable to get features from the database.", e);
case UNABLE_TO_INSERT:
throw new CommandFailedException("Unable to insert features into the working tree.", e);
case ALTER_AND_ALL_DEFINED:
throw new CommandFailedException("Alter cannot be used with --all option and more than one table.", e);
case INCOMPATIBLE_FEATURE_TYPE:
throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
default:
throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
}
} finally {
dataStore.dispose();
cli.getConsole().flush();
}
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class PGImport method runInternal.
/**
* Executes the import command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
DataStore dataStore = getDataStore();
try {
cli.getConsole().println("Importing from database " + commonArgs.database);
ProgressListener progressListener = cli.getProgressListener();
cli.getGeogig().command(ImportOp.class).setAll(all).setTable(table).setAlter(alter).setDestinationPath(destTable).setOverwrite(!add).setDataStore(dataStore).setAdaptToDefaultFeatureType(!forceFeatureType).setFidAttribute(fidAttribute).setProgressListener(progressListener).call();
cli.getConsole().println("Import successful.");
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case TABLE_NOT_DEFINED:
throw new CommandFailedException("No tables specified for import. Specify --all or --table <table>.", e);
case ALL_AND_TABLE_DEFINED:
throw new CommandFailedException("Specify --all or --table <table>, both cannot be set.", e);
case NO_FEATURES_FOUND:
throw new CommandFailedException("No features were found in the database.", e);
case TABLE_NOT_FOUND:
throw new CommandFailedException("Could not find the specified table.", e);
case UNABLE_TO_GET_NAMES:
throw new CommandFailedException("Unable to get feature types from the database.", e);
case UNABLE_TO_GET_FEATURES:
throw new CommandFailedException("Unable to get features from the database.", e);
case UNABLE_TO_INSERT:
throw new CommandFailedException("Unable to insert features into the working tree.", e);
case ALTER_AND_ALL_DEFINED:
throw new CommandFailedException("Alter cannot be used with --all option and more than one table.", e);
case INCOMPATIBLE_FEATURE_TYPE:
throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
default:
throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
}
} finally {
dataStore.dispose();
cli.getConsole().flush();
}
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class PGList method runInternal.
/**
* Executes the list command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
DataStore dataStore = getDataStore();
try {
cli.getConsole().println("Fetching feature types...");
Optional<List<String>> features = cli.getGeogig().command(ListOp.class).setDataStore(dataStore).call();
if (features.isPresent()) {
for (String featureType : features.get()) {
cli.getConsole().println(" - " + featureType);
}
} else {
cli.getConsole().println("No features types were found in the specified database.");
}
} catch (GeoToolsOpException e) {
throw new CommandFailedException("Unable to get feature types from the database.", e);
} finally {
dataStore.dispose();
cli.getConsole().flush();
}
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class AbstractOracleCommand method getDataStore.
/**
* Constructs a new Oracle data store using connection parameters from {@link OracleCommonArgs}.
*
* @return the constructed data store
* @throws Exception
* @see DataStore
*/
protected DataStore getDataStore() {
Map<String, Serializable> params = Maps.newHashMap();
params.put(OracleNGDataStoreFactory.DBTYPE.key, "oracle");
params.put(OracleNGDataStoreFactory.HOST.key, commonArgs.host);
params.put(OracleNGDataStoreFactory.PORT.key, commonArgs.port.toString());
params.put(OracleNGDataStoreFactory.SCHEMA.key, commonArgs.schema);
params.put(OracleNGDataStoreFactory.DATABASE.key, commonArgs.database);
params.put(OracleNGDataStoreFactory.USER.key, commonArgs.username);
params.put(OracleNGDataStoreFactory.PASSWD.key, commonArgs.password);
// params.put(OracleNGDataStoreFactory.ESTIMATED_EXTENTS.key, commonArgs.estimatedExtent);
// params.put(OracleNGDataStoreFactory.LOOSEBBOX.key, commonArgs.looseBbox);
// if (!commonArgs.geometryMetadataTable.equals(""))
// params.put(OracleNGDataStoreFactory.GEOMETRY_METADATA_TABLE.key,
// commonArgs.geometryMetadataTable);
// params.put(OracleNGDataStoreFactory.FETCHSIZE.key, 1000);
DataStore dataStore;
try {
dataStore = dataStoreFactory.createDataStore(params);
} catch (IOException e) {
throw new CommandFailedException("Unable to connect using the specified database parameters.", e);
}
if (dataStore == null) {
throw new CommandFailedException("No suitable data store found for the provided parameters");
}
if (dataStore instanceof JDBCDataStore) {
Connection con = null;
try {
con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
} catch (Exception e) {
throw new CommandFailedException("Error validating the database connection", e);
}
((JDBCDataStore) dataStore).closeSafe(con);
}
return dataStore;
}
Aggregations