Search in sources :

Example 1 with MappingRule

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

the class WriteOSMMappingEntries method _call.

@Override
protected Void _call() {
    Preconditions.checkNotNull(entry);
    Preconditions.checkNotNull(mapping);
    final File osmMapFolder = command(ResolveOSMMappingLogFolder.class).call();
    for (MappingRule rule : mapping.getRules()) {
        File file = new File(osmMapFolder, rule.getName());
        try {
            Files.write(entry.toString(), file, Charsets.UTF_8);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    File file = new File(osmMapFolder, entry.getPostMappingId().toString());
    try {
        Files.write(mapping.toString(), file, Charsets.UTF_8);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    return null;
}
Also used : IOException(java.io.IOException) File(java.io.File) MappingRule(org.locationtech.geogig.osm.internal.MappingRule)

Example 2 with MappingRule

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

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

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

Aggregations

MappingRule (org.locationtech.geogig.osm.internal.MappingRule)4 Mapping (org.locationtech.geogig.osm.internal.Mapping)3 File (java.io.File)2 IOException (java.io.IOException)2 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)2 Function (com.google.common.base.Function)1 Optional (com.google.common.base.Optional)1 Nullable (javax.annotation.Nullable)1 DataStore (org.geotools.data.DataStore)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)1 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)1 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)1 Feature (org.opengis.feature.Feature)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1