Search in sources :

Example 36 with DataStore

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);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) FileWriter(java.io.FileWriter) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureCollection(org.geotools.feature.FeatureCollection) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) File(java.io.File) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Example 37 with DataStore

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();
    }
}
Also used : ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 38 with DataStore

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();
    }
}
Also used : ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 39 with DataStore

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();
    }
}
Also used : DataStore(org.geotools.data.DataStore) List(java.util.List) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 40 with DataStore

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;
}
Also used : Serializable(java.io.Serializable) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) DataStore(org.geotools.data.DataStore) Connection(java.sql.Connection) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Aggregations

DataStore (org.geotools.data.DataStore)52 IOException (java.io.IOException)28 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)28 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)23 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)22 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)22 HashMap (java.util.HashMap)10 Map (java.util.Map)10 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Test (org.junit.Test)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)8 Serializable (java.io.Serializable)7 ArrayList (java.util.ArrayList)7 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)6 DataSourceInfo (com.sldeditor.datasource.impl.DataSourceInfo)6 URL (java.net.URL)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)5