Search in sources :

Example 1 with GeoToolsOpException

use of org.locationtech.geogig.geotools.plumbing.GeoToolsOpException in project GeoGig by boundlessgeo.

the class ShpExportDiff method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.size() != 4) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String commitOld = args.get(0);
    String commitNew = args.get(1);
    String path = args.get(2);
    String shapefile = args.get(3);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    File file = new File(shapefile);
    if (file.exists() && !overwrite) {
        throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
    }
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
    params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
    params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
    ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    SimpleFeatureType outputFeatureType;
    try {
        outputFeatureType = getFeatureType(path, cli);
    } catch (GeoToolsOpException e) {
        cli.getConsole().println("No features to export.");
        return;
    }
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.add("geogig_fid", String.class);
    for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
        builder.add(descriptor);
    }
    builder.setName(outputFeatureType.getName());
    builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
    outputFeatureType = builder.buildFeatureType();
    dataStore.createSchema(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;
    Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
    ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old).setTransactional(false).setFeatureTypeConversionFunction(function);
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } 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.", e);
            default:
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ExportDiffOp(org.locationtech.geogig.geotools.plumbing.ExportDiffOp) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File)

Example 2 with GeoToolsOpException

use of org.locationtech.geogig.geotools.plumbing.GeoToolsOpException in project GeoGig by boundlessgeo.

the class ShpImport method runInternal.

/**
     * Executes the import command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(shapeFile != null && !shapeFile.isEmpty(), "No shapefile specified");
    for (String shp : shapeFile) {
        DataStore dataStore = null;
        try {
            dataStore = getDataStore(shp);
        } catch (InvalidParameterException e) {
            cli.getConsole().println("The shapefile '" + shp + "' 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 shapefile");
            }
        }
        try {
            cli.getConsole().println("Importing from shapefile " + shp);
            ProgressListener progressListener = cli.getProgressListener();
            ImportOp command = cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setAdaptToDefaultFeatureType(!forceFeatureType);
            // force the import not to use paging due to a bug in the shapefile datastore
            command.setUsePaging(false);
            command.setProgressListener(progressListener).call();
            cli.getConsole().println(shp + " imported successfully.");
        } catch (GeoToolsOpException e) {
            switch(e.statusCode) {
                case NO_FEATURES_FOUND:
                    throw new CommandFailedException("No features were found in the shapefile.", e);
                case UNABLE_TO_GET_NAMES:
                    throw new CommandFailedException("Unable to get feature types from the shapefile.", e);
                case UNABLE_TO_GET_FEATURES:
                    throw new CommandFailedException("Unable to get features from the shapefile.", 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);
            }
        } finally {
            dataStore.dispose();
            cli.getConsole().flush();
        }
    }
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 3 with GeoToolsOpException

use of org.locationtech.geogig.geotools.plumbing.GeoToolsOpException in project GeoGig by boundlessgeo.

the class OSMExportPG method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) {
    Preconditions.checkNotNull(mappingFile != null, "A data mapping file must be specified");
    final Mapping mapping = Mapping.fromFile(mappingFile);
    List<MappingRule> rules = mapping.getRules();
    checkParameter(!rules.isEmpty(), "No rules are defined in the specified mapping");
    for (final MappingRule rule : mapping.getRules()) {
        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);
                if (mapped.isPresent()) {
                    return Optional.of(mapped.get());
                }
                return Optional.absent();
            }
        };
        SimpleFeatureType outputFeatureType = rule.getFeatureType();
        String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
        DataStore dataStore = null;
        try {
            dataStore = getDataStore();
            String tableName = outputFeatureType.getName().getLocalPart();
            if (Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
                if (!overwrite) {
                    throw new CommandFailedException("A table named '" + tableName + "'already exists. Use -o to overwrite");
                }
            } else {
                try {
                    dataStore.createSchema(outputFeatureType);
                } catch (IOException e) {
                    throw new CommandFailedException("Cannot create new table in database", e);
                }
            }
            final SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
            if (!(featureSource instanceof SimpleFeatureStore)) {
                throw new CommandFailedException("Could not create feature store. Data source is read only.");
            }
            final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            if (overwrite) {
                featureStore.removeFeatures(Filter.INCLUDE);
            }
            ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFeatureTypeConversionFunction(function);
            try {
                op.setProgressListener(cli.getProgressListener()).call();
                cli.getConsole().println("OSM data exported successfully to " + tableName);
            } catch (IllegalArgumentException iae) {
                throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Cannot connect to database: " + e.getMessage(), e);
        } finally {
            if (dataStore != null) {
                dataStore.dispose();
            }
        }
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Mapping(org.locationtech.geogig.osm.internal.Mapping) IOException(java.io.IOException) MappingRule(org.locationtech.geogig.osm.internal.MappingRule) 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) Nullable(javax.annotation.Nullable)

Example 4 with GeoToolsOpException

use of org.locationtech.geogig.geotools.plumbing.GeoToolsOpException in project GeoGig by boundlessgeo.

the class OSMExportSL method exportRule.

private void exportRule(final MappingRule rule, final GeogigCLI cli) throws IOException {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        public Optional<Feature> apply(Feature feature) {
            Optional<Feature> mapped = rule.apply(feature);
            return mapped;
        }
    };
    SimpleFeatureType outputFeatureType = rule.getFeatureType();
    String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
    DataStore dataStore = getDataStore();
    try {
        final String tableName = ensureTableExists(outputFeatureType, dataStore);
        final SimpleFeatureSource source = dataStore.getFeatureSource(tableName);
        if (!(source instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }
        final SimpleFeatureStore store = (SimpleFeatureStore) source;
        if (overwrite) {
            try {
                store.removeFeatures(Filter.INCLUDE);
            } catch (IOException e) {
                throw new CommandFailedException("Error truncating table " + tableName, e);
            }
        }
        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 " + tableName);
        } catch (IllegalArgumentException iae) {
            throw new InvalidParameterException(iae.getMessage(), iae);
        } catch (GeoToolsOpException e) {
            throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp)

Example 5 with GeoToolsOpException

use of org.locationtech.geogig.geotools.plumbing.GeoToolsOpException in project GeoGig by boundlessgeo.

the class SQLServerExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String path = args.get(0);
    String tableName = args.get(1);
    checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
    DataStore dataStore = getDataStore();
    ObjectId featureTypeId = null;
    if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
        SimpleFeatureType outputFeatureType;
        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 {
                SimpleFeatureType sft = getFeatureType(path, cli);
                outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("No features to export.", e);
            }
        }
        try {
            dataStore.createSchema(outputFeatureType);
        } catch (IOException e) {
            throw new CommandFailedException("Cannot create new table in database", e);
        }
    } else {
        if (!overwrite) {
            throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
        }
    }
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
        throw new CommandFailedException("Can't write to the selected table");
    }
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    if (overwrite) {
        try {
            featureStore.removeFeatures(Filter.INCLUDE);
        } catch (IOException e) {
            throw new CommandFailedException("Error accessing table: " + e.getMessage(), e);
        }
    }
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
    if (defaultType) {
        op.exportDefaultFeatureType();
    }
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case MIXED_FEATURE_TYPES:
                throw new CommandFailedException("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);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + tableName);
}
Also used : NameImpl(org.geotools.feature.NameImpl) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) SimpleFeatureTypeImpl(org.geotools.feature.simple.SimpleFeatureTypeImpl) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Aggregations

CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)24 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)24 DataStore (org.geotools.data.DataStore)22 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)10 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)10 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)10 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 IOException (java.io.IOException)6 ObjectId (org.locationtech.geogig.api.ObjectId)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 TYPE (org.locationtech.geogig.api.RevObject.TYPE)6 Optional (com.google.common.base.Optional)5 Feature (org.opengis.feature.Feature)5 List (java.util.List)4 Map (java.util.Map)4 NameImpl (org.geotools.feature.NameImpl)4 SimpleFeatureTypeImpl (org.geotools.feature.simple.SimpleFeatureTypeImpl)4 DescribeOp (org.locationtech.geogig.geotools.plumbing.DescribeOp)4 ImportOp (org.locationtech.geogig.geotools.plumbing.ImportOp)4