Search in sources :

Example 51 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException 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 52 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException 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)

Example 53 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException 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();
    }
}
Also used : Serializable(java.io.Serializable) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) Nullable(javax.annotation.Nullable)

Example 54 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class OSMExportShp method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    Preconditions.checkNotNull(mappingFile != null, "A data mapping file must be specified");
    if (args == null || args.isEmpty() || args.size() != 1) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String shapefile = args.get(0);
    final File file = new File(shapefile);
    final Mapping mapping = Mapping.fromFile(mappingFile);
    List<MappingRule> rules = mapping.getRules();
    checkParameter(!rules.isEmpty(), "No rules are defined in the specified mapping");
    for (MappingRule rule : rules) {
        File targetFile = file;
        if (rules.size() > 1) {
            String name = getNameWithoutExtension(file.getName()) + "_" + rule.getName() + ".shp";
            targetFile = new File(file.getParentFile(), name);
        }
        exportRule(rule, cli, targetFile);
    }
}
Also used : Mapping(org.locationtech.geogig.osm.internal.Mapping) File(java.io.File) MappingRule(org.locationtech.geogig.osm.internal.MappingRule) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 55 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class OSMMap method runInternal.

/**
     * Executes the map command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args == null || args.isEmpty() || args.size() != 1) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    checkState(cli.getGeogig().getRepository().index().isClean() && cli.getGeogig().getRepository().workingTree().isClean(), "Working tree and index are not clean");
    String mappingFilepath = args.get(0);
    Mapping mapping = Mapping.fromFile(mappingFilepath);
    geogig = cli.getGeogig();
    ObjectId oldTreeId = geogig.getRepository().workingTree().getTree().getId();
    message = message == null ? "Applied mapping " + new File(mappingFilepath).getName() : message;
    ObjectId newTreeId = geogig.command(OSMMapOp.class).setMapping(mapping).setMessage(message).call().getId();
    ConsoleReader console = cli.getConsole();
    if (newTreeId.equals(oldTreeId)) {
        console.println("No features matched the specified filter, or they provided no updated data.\n" + "No changes have been made to the working tree");
    } else {
    // print something?
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) ObjectId(org.locationtech.geogig.api.ObjectId) Mapping(org.locationtech.geogig.osm.internal.Mapping) File(java.io.File) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Aggregations

CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)58 DataStore (org.geotools.data.DataStore)28 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)24 File (java.io.File)16 GeoGIG (org.locationtech.geogig.api.GeoGIG)16 ObjectId (org.locationtech.geogig.api.ObjectId)15 IOException (java.io.IOException)14 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)12 ConsoleReader (jline.console.ConsoleReader)10 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)10 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)10 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 Serializable (java.io.Serializable)8 Optional (com.google.common.base.Optional)7 TYPE (org.locationtech.geogig.api.RevObject.TYPE)7 Map (java.util.Map)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 Connection (java.sql.Connection)4 Feature (org.opengis.feature.Feature)4