use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.
the class OSMUnmapOp method _call.
@Override
protected RevTree _call() {
Optional<OSMMappingLogEntry> entry = command(ReadOSMMappingLogEntry.class).setPath(path).call();
if (entry.isPresent()) {
Optional<Mapping> opt = command(ReadOSMMapping.class).setEntry(entry.get()).call();
if (opt.isPresent()) {
mapping = opt.get();
}
}
Iterator<NodeRef> iter = command(LsTreeOp.class).setReference(path).setStrategy(Strategy.FEATURES_ONLY).call();
FeatureMapFlusher flusher = new FeatureMapFlusher(workingTree());
while (iter.hasNext()) {
NodeRef node = iter.next();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
String id = null;
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
if (descriptor.getName().getLocalPart().equals("id")) {
id = values.get(i).get().toString();
}
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
Preconditions.checkNotNull(id, "No 'id' attribute found");
SimpleFeature feature = featureBuilder.buildFeature(id);
unmapFeature(feature, flusher);
}
flusher.flushAll();
if (entry.isPresent()) {
Iterator<DiffEntry> diffs = command(DiffTree.class).setPathFilter(path).setNewTree(workingTree().getTree().getId()).setOldTree(entry.get().getPostMappingId()).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (diff.changeType().equals(DiffEntry.ChangeType.REMOVED)) {
ObjectId featureId = diff.getOldObject().getNode().getObjectId();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(featureId).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(diff.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
String id = null;
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
if (descriptor.getName().getLocalPart().equals("id")) {
id = values.get(i).get().toString();
}
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
Preconditions.checkNotNull(id, "No 'id' attribute found");
SimpleFeature feature = featureBuilder.buildFeature(id);
Class<?> clazz = feature.getDefaultGeometryProperty().getType().getBinding();
String deletePath = clazz.equals(Point.class) ? OSMUtils.NODE_TYPE_NAME : OSMUtils.WAY_TYPE_NAME;
workingTree().delete(deletePath, id);
}
}
}
return workingTree().getTree();
}
use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.
the class OSMImportOp method _call.
@Override
protected Optional<OSMReport> _call() {
checkNotNull(urlOrFilepath);
ObjectId oldTreeId = workingTree().getTree().getId();
File osmDataFile = null;
final InputStream osmDataStream;
if (urlOrFilepath.startsWith("http")) {
osmDataStream = downloadFile();
} else {
osmDataFile = new File(urlOrFilepath);
Preconditions.checkArgument(osmDataFile.exists(), "File does not exist: " + urlOrFilepath);
try {
osmDataStream = new BufferedInputStream(new FileInputStream(osmDataFile), 1024 * 1024);
} catch (FileNotFoundException e) {
throw Throwables.propagate(e);
}
}
ProgressListener progressListener = getProgressListener();
progressListener.setDescription("Importing into GeoGig repo...");
EntityConverter converter = new EntityConverter();
OSMReport report;
try {
report = parseDataFileAndInsert(osmDataFile, osmDataStream, converter);
} finally {
Closeables.closeQuietly(osmDataStream);
}
if (!progressListener.isCanceled() && report != null) {
ObjectId newTreeId = workingTree().getTree().getId();
if (!noRaw) {
if (mapping != null || filter != null) {
progressListener.setDescription("Staging features...");
command(AddOp.class).setProgressListener(progressListener).call();
progressListener.setDescription("Committing features...");
command(CommitOp.class).setMessage(message).setProgressListener(progressListener).call();
OSMLogEntry entry = new OSMLogEntry(newTreeId, report.getLatestChangeset(), report.getLatestTimestamp());
command(AddOSMLogEntry.class).setEntry(entry).call();
if (filter != null) {
command(WriteOSMFilterFile.class).setEntry(entry).setFilterCode(filter).call();
}
if (mapping != null) {
command(WriteOSMMappingEntries.class).setMapping(mapping).setMappingLogEntry(new OSMMappingLogEntry(oldTreeId, newTreeId)).call();
}
}
}
}
return Optional.fromNullable(report);
}
use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.
the class OSMMapOp method _call.
@Override
protected RevTree _call() {
checkNotNull(mapping);
long staged = index().countStaged(null).count();
long unstaged = workingTree().countUnstaged(null).count();
Preconditions.checkState((staged == 0 && unstaged == 0), "You must have a clean working tree and index to perform a mapping.");
ObjectId oldTreeId = workingTree().getTree().getId();
Iterator<Feature> nodes;
if (mapping.canUseNodes()) {
nodes = getFeatures("WORK_HEAD:node");
} else {
nodes = Iterators.emptyIterator();
}
Iterator<Feature> ways;
if (mapping.canUseWays()) {
ways = getFeatures("WORK_HEAD:way");
} else {
ways = Iterators.emptyIterator();
}
Iterator<Feature> iterator = Iterators.concat(nodes, ways);
if (iterator.hasNext()) {
FeatureMapFlusher insertsByParent = new FeatureMapFlusher(workingTree());
while (iterator.hasNext()) {
Feature feature = iterator.next();
List<MappedFeature> mappedFeatures = mapping.map(feature);
if (!mappedFeatures.isEmpty()) {
for (MappedFeature mapped : mappedFeatures) {
String path = mapped.getPath();
insertsByParent.put(path, mapped);
}
}
}
insertsByParent.flushAll();
ObjectId newTreeId = workingTree().getTree().getId();
// If the mapping generates the same mapped features that already exist, we do nothing
if (!newTreeId.equals(oldTreeId)) {
command(AddOp.class).call();
command(CommitOp.class).setMessage(message).call();
command(WriteOSMMappingEntries.class).setMapping(mapping).setMappingLogEntry(new OSMMappingLogEntry(oldTreeId, newTreeId)).call();
}
}
return workingTree().getTree();
}
Aggregations