use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class MergeOpTest method testOctopusMergeSameFeatureChanges.
@Test
public void testOctopusMergeSameFeatureChanges() throws Exception {
insertAndAdd(points1);
geogig.command(CommitOp.class).call();
geogig.command(BranchCreateOp.class).setName("branch1").call();
geogig.command(BranchCreateOp.class).setName("branch2").call();
geogig.command(BranchCreateOp.class).setName("branch3").call();
insertAndAdd(points1_modified);
geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("branch1").call();
insertAndAdd(points2);
RevCommit branch1 = geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("branch2").call();
insertAndAdd(points3);
RevCommit branch2 = geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("branch3").call();
insertAndAdd(points1_modified);
RevCommit branch3 = geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("master").call();
geogig.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch1.getId())).addCommit(Suppliers.ofInstance(branch2.getId())).addCommit(Suppliers.ofInstance(branch3.getId())).call();
String path = NodeRef.appendChild(pointsName, idP1);
Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call(RevFeature.class);
assertTrue(revFeature.isPresent());
assertEquals(RevFeatureBuilder.build(points1_modified), revFeature.get());
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMMapOp method getFeatures.
private Iterator<Feature> getFeatures(String ref) {
Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
if (!id.isPresent()) {
return Iterators.emptyIterator();
}
LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
Iterator<NodeRef> iterator = op.call();
Function<NodeRef, Feature> nodeRefToFeature = new Function<NodeRef, Feature>() {
private final //
Map<String, FeatureBuilder> builders = //
ImmutableMap.<//
String, //
FeatureBuilder>of(//
OSMUtils.NODE_TYPE_NAME, //
new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.nodeType())), //
OSMUtils.WAY_TYPE_NAME, new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.wayType())));
private final RevObjectParse parseCommand = command(RevObjectParse.class);
@Override
@Nullable
public Feature apply(@Nullable NodeRef ref) {
RevFeature revFeature = parseCommand.setObjectId(ref.objectId()).call(RevFeature.class).get();
final String parentPath = ref.getParentPath();
FeatureBuilder featureBuilder = builders.get(parentPath);
String fid = ref.name();
Feature feature = featureBuilder.build(fid, revFeature);
return feature;
}
};
return Iterators.transform(iterator, nodeRefToFeature);
}
use of org.locationtech.geogig.api.RevFeature 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.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMUnmapOp method unmapNode.
private void unmapNode(SimpleFeature feature, FeatureMapFlusher mapFlusher) {
boolean modified = false;
String id = feature.getID();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + id).call(RevFeature.class);
Map<String, String> tagsMap = Maps.newHashMap();
long timestamp = System.currentTimeMillis();
int version = 1;
long changeset = -1;
String user = UNKNOWN_USER;
Collection<Tag> tags = Lists.newArrayList();
if (rawFeature.isPresent()) {
ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
tags = OSMUtils.buildTagsCollectionFromString(values.get(NODE_TAGS_FIELD_INDEX).get().toString());
for (Tag tag : tags) {
tagsMap.put(tag.getKey(), tag.getValue());
}
Optional<Object> timestampOpt = values.get(NODE_TIMESTAMP_FIELD_INDEX);
if (timestampOpt.isPresent()) {
timestamp = ((Long) timestampOpt.get()).longValue();
}
Optional<Object> versionOpt = values.get(NODE_VERSION_FIELD_INDEX);
if (versionOpt.isPresent()) {
version = ((Integer) versionOpt.get()).intValue();
}
Optional<Object> changesetOpt = values.get(NODE_CHANGESET_FIELD_INDEX);
if (changesetOpt.isPresent()) {
changeset = ((Long) changesetOpt.get()).longValue();
}
Optional<Object> userOpt = values.get(NODE_USER_FIELD_INDEX);
if (userOpt.isPresent()) {
user = (String) userOpt.get();
}
}
Map<String, String> unaliased = Maps.newHashMap();
Collection<Property> properties = feature.getProperties();
for (Property property : properties) {
String name = property.getName().getLocalPart();
if (name.equals("id") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
continue;
}
Object value = property.getValue();
if (value != null) {
String tagName = name;
if (mapping != null) {
if (unaliased.containsKey(name)) {
tagName = unaliased.get(name);
} else {
tagName = mapping.getTagNameFromAlias(path, tagName);
unaliased.put(name, tagName);
}
}
if (!DefaultField.isDefaultField(tagName)) {
if (tagsMap.containsKey(tagName)) {
if (!modified) {
String oldValue = tagsMap.get(tagName);
modified = !value.equals(oldValue);
}
} else {
modified = true;
}
tagsMap.put(tagName, value.toString());
}
}
}
if (!modified && rawFeature.isPresent()) {
// no changes after unmapping tags, so there's nothing else to do
return;
}
Collection<Tag> newTags = Lists.newArrayList();
Set<Entry<String, String>> entries = tagsMap.entrySet();
for (Entry<String, String> entry : entries) {
newTags.add(new Tag(entry.getKey(), entry.getValue()));
}
featureBuilder.set("tags", OSMUtils.buildTagsString(newTags));
featureBuilder.set("location", feature.getDefaultGeometry());
featureBuilder.set("changeset", changeset);
featureBuilder.set("timestamp", timestamp);
featureBuilder.set("version", version);
featureBuilder.set("user", user);
featureBuilder.set("visible", true);
if (rawFeature.isPresent()) {
// the feature has changed, so we cannot reuse some attributes.
// We reconstruct the feature and insert it
featureBuilder.set("timestamp", System.currentTimeMillis());
featureBuilder.set("changeset", null);
featureBuilder.set("version", null);
featureBuilder.set("visible", true);
mapFlusher.put("node", featureBuilder.buildFeature(id));
} else {
// The feature didn't exist, so we have to add it
mapFlusher.put("node", featureBuilder.buildFeature(id));
}
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class RevFeatureSerializationTest method testFeatureReadWrite.
protected void testFeatureReadWrite(Feature feature) throws Exception {
RevFeature newFeature = RevFeatureBuilder.build(feature);
ObjectWriter<RevFeature> writer = factory.<RevFeature>createObjectWriter(TYPE.FEATURE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
writer.write(newFeature, output);
byte[] data = output.toByteArray();
assertTrue(data.length > 0);
ObjectReader<RevFeature> reader = factory.<RevFeature>createObjectReader(TYPE.FEATURE);
ByteArrayInputStream input = new ByteArrayInputStream(data);
RevFeature feat = reader.read(newFeature.getId(), input);
assertNotNull(feat);
assertEquals(newFeature.getValues().size(), feat.getValues().size());
for (int i = 0; i < newFeature.getValues().size(); i++) {
assertEquals(newFeature.getValues().get(i).orNull(), feat.getValues().get(i).orNull());
}
}
Aggregations