Search in sources :

Example 1 with Change

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

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

the class OSMHistoryImport method insertChanges.

/**
     * @param cli
     * @param changes
     * @param featureFilter
     * @throws IOException
     */
private long insertChanges(GeogigCLI cli, final Iterator<Change> changes, @Nullable Envelope featureFilter) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    final Repository repository = geogig.getRepository();
    final WorkingTree workTree = repository.workingTree();
    Map<Long, Coordinate> thisChangePointCache = new LinkedHashMap<Long, Coordinate>() {

        /** serialVersionUID */
        private static final long serialVersionUID = 1277795218777240552L;

        @Override
        protected boolean removeEldestEntry(Map.Entry<Long, Coordinate> eldest) {
            return size() == 10000;
        }
    };
    long cnt = 0;
    Set<String> deletes = Sets.newHashSet();
    Multimap<String, SimpleFeature> insertsByParent = HashMultimap.create();
    while (changes.hasNext()) {
        Change change = changes.next();
        final String featurePath = featurePath(change);
        if (featurePath == null) {
            // ignores relations
            continue;
        }
        final String parentPath = NodeRef.parentPath(featurePath);
        if (Change.Type.delete.equals(change.getType())) {
            cnt++;
            deletes.add(featurePath);
        } else {
            final Primitive primitive = change.getNode().isPresent() ? change.getNode().get() : change.getWay().get();
            final Geometry geom = parseGeometry(geogig, primitive, thisChangePointCache);
            if (geom instanceof Point) {
                thisChangePointCache.put(Long.valueOf(primitive.getId()), ((Point) geom).getCoordinate());
            }
            SimpleFeature feature = toFeature(primitive, geom);
            if (featureFilter == null || featureFilter.intersects((Envelope) feature.getBounds())) {
                insertsByParent.put(parentPath, feature);
                cnt++;
            }
        }
    }
    for (String parentPath : insertsByParent.keySet()) {
        Collection<SimpleFeature> features = insertsByParent.get(parentPath);
        if (features.isEmpty()) {
            continue;
        }
        Iterator<? extends Feature> iterator = features.iterator();
        ProgressListener listener = new DefaultProgressListener();
        List<org.locationtech.geogig.api.Node> insertedTarget = null;
        Integer collectionSize = Integer.valueOf(features.size());
        workTree.insert(parentPath, iterator, listener, insertedTarget, collectionSize);
    }
    if (!deletes.isEmpty()) {
        workTree.delete(deletes.iterator());
    }
    return cnt;
}
Also used : Node(org.locationtech.geogig.osm.internal.history.Node) Envelope(com.vividsolutions.jts.geom.Envelope) LinkedHashMap(java.util.LinkedHashMap) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Entry(java.util.Map.Entry) Primitive(org.locationtech.geogig.osm.internal.history.Primitive) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Change(org.locationtech.geogig.osm.internal.history.Change) Point(com.vividsolutions.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) Repository(org.locationtech.geogig.repository.Repository) ProgressListener(org.locationtech.geogig.api.ProgressListener) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Coordinate(com.vividsolutions.jts.geom.Coordinate) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Aggregations

GeoGIG (org.locationtech.geogig.api.GeoGIG)2 Change (org.locationtech.geogig.osm.internal.history.Change)2 WorkingTree (org.locationtech.geogig.repository.WorkingTree)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 Point (com.vividsolutions.jts.geom.Point)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 DefaultProgressListener (org.locationtech.geogig.api.DefaultProgressListener)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 ProgressListener (org.locationtech.geogig.api.ProgressListener)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 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)1 Changeset (org.locationtech.geogig.osm.internal.history.Changeset)1 Node (org.locationtech.geogig.osm.internal.history.Node)1 Primitive (org.locationtech.geogig.osm.internal.history.Primitive)1