use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class AbstractSLCommand method getDataStore.
/**
* Constructs a new SpatiaLite data store using connection parameters from {@link SLCommonArgs}.
*
* @return the constructed data store
* @throws CommandFailedException
* @see DataStore
*/
protected DataStore getDataStore() {
Map<String, Serializable> params = Maps.newHashMap();
params.put(SpatiaLiteDataStoreFactory.DBTYPE.key, "spatialite");
params.put(SpatiaLiteDataStoreFactory.DATABASE.key, commonArgs.database);
params.put(SpatiaLiteDataStoreFactory.USER.key, commonArgs.username);
try {
DataStore dataStore = dataStoreFactory.createDataStore(params);
if (dataStore == null) {
throw new CommandFailedException("Unable to connect using the specified database parameters.");
}
if (dataStore instanceof JDBCDataStore) {
Connection con = null;
con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
((JDBCDataStore) dataStore).closeSafe(con);
}
return dataStore;
} catch (IOException e) {
throw new CommandFailedException("Unable to connect using the specified database parameters.", e);
} catch (SQLException e) {
throw new CommandFailedException("Unable to validate the database connection.", e);
}
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class AbstractSQLServerCommand method getDataStore.
/**
* Constructs a new SQL Server data store using connection parameters from
* {@link SQLServerCommonArgs}.
*
* @return the constructed data store
* @throws CommandFailedException
* @see DataStore
*/
protected DataStore getDataStore() {
Map<String, Serializable> params = Maps.newHashMap();
params.put(SQLServerDataStoreFactory.DBTYPE.key, "sqlserver");
params.put(SQLServerDataStoreFactory.HOST.key, commonArgs.host);
params.put(SQLServerDataStoreFactory.PORT.key, commonArgs.port.toString());
params.put(SQLServerDataStoreFactory.INTSEC.key, commonArgs.intsec);
params.put(SQLServerDataStoreFactory.SCHEMA.key, commonArgs.schema);
params.put(SQLServerDataStoreFactory.DATABASE.key, commonArgs.database);
params.put(SQLServerDataStoreFactory.USER.key, commonArgs.username);
params.put(SQLServerDataStoreFactory.PASSWD.key, commonArgs.password);
params.put(SQLServerDataStoreFactory.FETCHSIZE.key, 1000);
if (!commonArgs.geometryMetadataTable.equals(""))
params.put(SQLServerDataStoreFactory.GEOMETRY_METADATA_TABLE.key, commonArgs.geometryMetadataTable);
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;
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class AbstractGeoJsonCommand method getDataStore.
protected DataStore getDataStore(String geoJSON) throws FileNotFoundException, IOException {
try {
File geoJsonfile = new File(geoJSON);
checkParameter(geoJsonfile.exists(), "File does not exist '%s'", geoJsonfile);
InputStream in = new FileInputStream(geoJsonfile);
FeatureJSON fjson = new FeatureJSON();
@SuppressWarnings("rawtypes") FeatureCollection fc = fjson.readFeatureCollection(in);
@SuppressWarnings("unchecked") DataStore dataStore = new MemoryDataStore(fc);
return dataStore;
} catch (IOException ioe) {
throw new CommandFailedException("Error opening GeoJSON: " + ioe.getMessage(), ioe);
}
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class AbstractPGCommand method getDataStore.
/**
* Constructs a new PostGIS data store using connection parameters from {@link PGCommonArgs}.
*
* @return the constructed data store
* @throws Exception
* @see DataStore
*/
protected DataStore getDataStore() {
Map<String, Serializable> params = Maps.newHashMap();
params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
params.put(PostgisNGDataStoreFactory.HOST.key, commonArgs.host);
params.put(PostgisNGDataStoreFactory.PORT.key, commonArgs.port.toString());
params.put(PostgisNGDataStoreFactory.SCHEMA.key, commonArgs.schema);
params.put(PostgisNGDataStoreFactory.DATABASE.key, commonArgs.database);
params.put(PostgisNGDataStoreFactory.USER.key, commonArgs.username);
params.put(PostgisNGDataStoreFactory.PASSWD.key, commonArgs.password);
params.put(PostgisNGDataStoreFactory.FETCHSIZE.key, 1000);
params.put(PostgisNGDataStoreFactory.EXPOSE_PK.key, true);
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("Unable to connect using the specified database parameters.");
}
if (dataStore instanceof JDBCDataStore) {
Connection con = null;
try {
con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
} catch (SQLException e) {
throw new CommandFailedException(e.getMessage(), e);
}
((JDBCDataStore) dataStore).closeSafe(con);
}
return dataStore;
}
use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.
the class GeoJsonImport method runInternal.
@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
checkParameter(geoJSONList != null && !geoJSONList.isEmpty(), "No GeoJSON specified");
checkParameter(geomName == null || !geomNameAuto, "Cannot use --geom-name and --geom-name-auto at the same time");
for (String geoJSON : geoJSONList) {
DataStore dataStore = null;
try {
dataStore = getDataStore(geoJSON);
} catch (InvalidParameterException e) {
cli.getConsole().println("The GeoJSON file '" + geoJSON + "' could not be found, skipping...");
continue;
}
if (fidAttribute != null) {
AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
if (attrib == null) {
throw new InvalidParameterException("The specified attribute does not exist in the selected GeoJSON file");
}
}
if (geomNameAuto) {
String destPath = destTable;
if (destPath == null) {
destPath = dataStore.getSchema(dataStore.getNames().get(0)).getTypeName();
}
Optional<RevFeatureType> ft = cli.getGeogig().command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + destPath).call(RevFeatureType.class);
// attribute
if (ft.isPresent()) {
GeometryDescriptor geomDescriptor = ft.get().type().getGeometryDescriptor();
if (geomDescriptor != null) {
geomName = geomDescriptor.getLocalName();
}
}
}
try {
cli.getConsole().println("Importing from GeoJSON " + geoJSON);
ProgressListener progressListener = cli.getProgressListener();
cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setGeometryNameOverride(geomName).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
cli.getConsole().println(geoJSON + " imported successfully.");
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case NO_FEATURES_FOUND:
throw new CommandFailedException("No features were found in the GeoJSON file.", e);
case UNABLE_TO_GET_NAMES:
throw new CommandFailedException("Unable to get feature types from the GeoJSON file.", e);
case UNABLE_TO_GET_FEATURES:
throw new CommandFailedException("Unable to get features from the GeoJSON file.", e);
case UNABLE_TO_INSERT:
throw new CommandFailedException("Unable to insert features into the working tree.", 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);
}
}
}
}
Aggregations