Search in sources :

Example 1 with Mapping

use of org.locationtech.geogig.osm.internal.Mapping 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 2 with Mapping

use of org.locationtech.geogig.osm.internal.Mapping in project GeoGig by boundlessgeo.

the class OSMImport method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(apiUrl != null && apiUrl.size() == 1, "One file must be specified");
    File importFile = new File(apiUrl.get(0));
    checkParameter(importFile.exists(), "The specified OSM data file does not exist");
    checkParameter(!(message != null && noRaw), "cannot use --message if using --no-raw");
    checkParameter(message == null || mappingFile != null, "Cannot use --message if not using --mapping");
    Mapping mapping = null;
    if (mappingFile != null) {
        mapping = Mapping.fromFile(mappingFile);
    }
    try {
        message = message == null ? "Updated OSM data" : message;
        Optional<OSMReport> report = cli.getGeogig().command(OSMImportOp.class).setDataSource(importFile.getAbsolutePath()).setMapping(mapping).setMessage(message).setNoRaw(noRaw).setAdd(add).setProgressListener(cli.getProgressListener()).call();
        if (report.isPresent()) {
            OSMReport rep = report.get();
            String msg;
            if (rep.getUnpprocessedCount() > 0) {
                msg = String.format("\nSome elements returned by the specified filter could not be processed.\n" + "Processed entities: %,d.\nWrong or uncomplete elements: %,d.\nNodes: %,d.\nWays: %,d.\n", rep.getCount(), rep.getUnpprocessedCount(), rep.getNodeCount(), rep.getWayCount());
            } else {
                msg = String.format("\nProcessed entities: %,d.\n Nodes: %,d.\n Ways: %,d\n", rep.getCount(), rep.getNodeCount(), rep.getWayCount());
            }
            cli.getConsole().println(msg);
        }
    } catch (EmptyOSMDownloadException e) {
        throw new IllegalArgumentException("The specified filter did not contain any valid element.\n" + "No changes were made to the repository.\n");
    } catch (RuntimeException e) {
        throw new CommandFailedException("Error importing OSM data: " + e.getMessage(), e);
    }
}
Also used : Mapping(org.locationtech.geogig.osm.internal.Mapping) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) File(java.io.File) OSMReport(org.locationtech.geogig.osm.internal.OSMReport) EmptyOSMDownloadException(org.locationtech.geogig.osm.internal.EmptyOSMDownloadException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 3 with Mapping

use of org.locationtech.geogig.osm.internal.Mapping in project GeoGig by boundlessgeo.

the class OSMExportSL 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");
    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) {
        exportRule(rule, cli);
    }
}
Also used : Mapping(org.locationtech.geogig.osm.internal.Mapping) MappingRule(org.locationtech.geogig.osm.internal.MappingRule)

Example 4 with Mapping

use of org.locationtech.geogig.osm.internal.Mapping 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 5 with Mapping

use of org.locationtech.geogig.osm.internal.Mapping 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

Mapping (org.locationtech.geogig.osm.internal.Mapping)7 File (java.io.File)4 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)4 MappingRule (org.locationtech.geogig.osm.internal.MappingRule)3 Optional (com.google.common.base.Optional)2 IOException (java.io.IOException)2 OSMImportOp (org.locationtech.geogig.osm.internal.OSMImportOp)2 Function (com.google.common.base.Function)1 URL (java.net.URL)1 Nullable (javax.annotation.Nullable)1 ConsoleReader (jline.console.ConsoleReader)1 DataStore (org.geotools.data.DataStore)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)1 Context (org.locationtech.geogig.api.Context)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)1 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)1 EmptyOSMDownloadException (org.locationtech.geogig.osm.internal.EmptyOSMDownloadException)1 OSMReport (org.locationtech.geogig.osm.internal.OSMReport)1