Search in sources :

Example 1 with Changeset

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

the class OSMHistoryImport method importOsmHistory.

private void importOsmHistory(GeogigCLI cli, ConsoleReader console, HistoryDownloader downloader, @Nullable Envelope featureFilter) throws IOException {
    Iterator<Changeset> changesets = downloader.fetchChangesets();
    GeoGIG geogig = cli.getGeogig();
    WorkingTree workingTree = geogig.getContext().workingTree();
    while (changesets.hasNext()) {
        Changeset changeset = changesets.next();
        if (changeset.isOpen()) {
            throw new CommandFailedException("Can't import past changeset " + changeset.getId() + " as it is still open.");
        }
        String desc = String.format("obtaining osm changeset %,d...", changeset.getId());
        console.print(desc);
        console.flush();
        Optional<Iterator<Change>> opchanges = changeset.getChanges().get();
        if (!opchanges.isPresent()) {
            updateBranchChangeset(geogig, changeset.getId());
            console.println(" does not apply.");
            console.flush();
            continue;
        }
        Iterator<Change> changes = opchanges.get();
        console.print("applying...");
        console.flush();
        ObjectId workTreeId = workingTree.getTree().getId();
        long changeCount = insertChanges(cli, changes, featureFilter);
        console.print(String.format("Applied %,d changes, staging...", changeCount));
        console.flush();
        ObjectId afterTreeId = workingTree.getTree().getId();
        DiffObjectCount diffCount = geogig.command(DiffCount.class).setOldVersion(workTreeId.toString()).setNewVersion(afterTreeId.toString()).call();
        geogig.command(AddOp.class).call();
        console.println(String.format("done. %,d changes actually applied.", diffCount.featureCount()));
        console.flush();
        commit(cli, changeset);
    }
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ObjectId(org.locationtech.geogig.api.ObjectId) Change(org.locationtech.geogig.osm.internal.history.Change) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) DiffObjectCount(org.locationtech.geogig.api.plumbing.diff.DiffObjectCount) Iterator(java.util.Iterator) Changeset(org.locationtech.geogig.osm.internal.history.Changeset) DiffCount(org.locationtech.geogig.api.plumbing.DiffCount) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 2 with Changeset

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

the class OSMHistoryImport method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(args.numThreads > 0 && args.numThreads < 7, "numthreads must be between 1 and 6");
    ConsoleReader console = cli.getConsole();
    final String osmAPIUrl = resolveAPIURL();
    final long startIndex;
    final long endIndex = args.endIndex;
    if (args.resume) {
        GeoGIG geogig = cli.getGeogig();
        long lastChangeset = getCurrentBranchChangeset(geogig);
        startIndex = 1 + lastChangeset;
    } else {
        startIndex = args.startIndex;
    }
    console.println(String.format("Obtaining OSM changesets %,d to %,d from %s", startIndex, args.endIndex, osmAPIUrl));
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("osm-history-fetch-thread-%d").build();
    final ExecutorService executor = Executors.newFixedThreadPool(args.numThreads, threadFactory);
    final File targetDir = resolveTargetDir();
    console.println("Downloading to " + targetDir.getAbsolutePath());
    console.flush();
    HistoryDownloader downloader;
    downloader = new HistoryDownloader(osmAPIUrl, targetDir, startIndex, endIndex, executor);
    Envelope env = parseBbox();
    Predicate<Changeset> filter = parseFilter(env);
    downloader.setChangesetFilter(filter);
    try {
        importOsmHistory(cli, console, downloader, env);
    } finally {
        executor.shutdownNow();
        try {
            executor.awaitTermination(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new CommandFailedException(e);
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) HistoryDownloader(org.locationtech.geogig.osm.internal.history.HistoryDownloader) ConsoleReader(jline.console.ConsoleReader) Envelope(com.vividsolutions.jts.geom.Envelope) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) File(java.io.File) Changeset(org.locationtech.geogig.osm.internal.history.Changeset) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Aggregations

GeoGIG (org.locationtech.geogig.api.GeoGIG)2 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)2 Changeset (org.locationtech.geogig.osm.internal.history.Changeset)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 File (java.io.File)1 Iterator (java.util.Iterator)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ConsoleReader (jline.console.ConsoleReader)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 DiffCount (org.locationtech.geogig.api.plumbing.DiffCount)1 DiffObjectCount (org.locationtech.geogig.api.plumbing.diff.DiffObjectCount)1 AddOp (org.locationtech.geogig.api.porcelain.AddOp)1 Change (org.locationtech.geogig.osm.internal.history.Change)1 HistoryDownloader (org.locationtech.geogig.osm.internal.history.HistoryDownloader)1 WorkingTree (org.locationtech.geogig.repository.WorkingTree)1