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();
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
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?
}
}
Aggregations