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