use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMMapOpTest method testMappingOnlyClosedPolygons.
@Test
public void testMappingOnlyClosedPolygons() throws Exception {
// import and check that we have both ways and nodes
String filename = OSMImportOp.class.getResource("ways_restriction.xml").getFile();
File file = new File(filename);
geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
WorkingTree workTree = geogig.getRepository().workingTree();
long unstaged = workTree.countUnstaged("way").count();
assertTrue(unstaged > 0);
unstaged = workTree.countUnstaged("node").count();
assertTrue(unstaged > 0);
geogig.command(AddOp.class).call();
geogig.command(CommitOp.class).setMessage("msg").call();
// Define mapping
Map<String, AttributeDefinition> fields = Maps.newHashMap();
Map<String, List<String>> filter = Maps.newHashMap();
filter.put("geom", Lists.newArrayList("closed"));
fields.put("geom", new AttributeDefinition("geom", FieldType.POLYGON));
fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
Map<String, List<String>> filterExclude = Maps.newHashMap();
MappingRule mappingRule = new MappingRule("polygons", filter, filterExclude, fields, null);
List<MappingRule> mappingRules = Lists.newArrayList();
mappingRules.add(mappingRule);
Mapping mapping = new Mapping(mappingRules);
geogig.command(OSMMapOp.class).setMapping(mapping).call();
// Check that mapping was correctly performed
Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:polygons/31045880").call(RevFeature.class);
assertTrue(revFeature.isPresent());
ImmutableList<Optional<Object>> values = revFeature.get().getValues();
assertEquals(4, values.size());
String wkt = "POLYGON ((7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781, 7.1923367 50.7395887))";
assertEquals(wkt, values.get(2).get().toString());
assertEquals("yes", values.get(1).get());
revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:polygons/24777894").call(RevFeature.class);
assertFalse(revFeature.isPresent());
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMAplyDiffOpTest method testApplyChangeset.
@Test
public void testApplyChangeset() throws Exception {
String filename = getClass().getResource("nodes_for_changeset2.xml").getFile();
File file = new File(filename);
geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
long unstaged = geogig.getRepository().workingTree().countUnstaged("node").count();
assertTrue(unstaged > 0);
Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/2059114068").call(RevFeature.class);
assertTrue(revFeature.isPresent());
revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464865").call(RevFeature.class);
assertFalse(revFeature.isPresent());
String changesetFilename = getClass().getResource("changeset.xml").getFile();
geogig.command(OSMApplyDiffOp.class).setDiffFile(new File(changesetFilename)).call();
revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/2059114068").call(RevFeature.class);
assertFalse(revFeature.isPresent());
revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464865").call(RevFeature.class);
assertTrue(revFeature.isPresent());
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class ApplyPatchOp method applyPatch.
private void applyPatch(Patch patch) {
final WorkingTree workTree = workingTree();
final StagingDatabase indexDb = stagingDatabase();
if (reverse) {
patch = patch.reversed();
}
List<FeatureInfo> removed = patch.getRemovedFeatures();
for (FeatureInfo feature : removed) {
workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
}
List<FeatureInfo> added = patch.getAddedFeatures();
for (FeatureInfo feature : added) {
workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
}
List<FeatureDiff> diffs = patch.getModifiedFeatures();
for (FeatureDiff diff : diffs) {
String path = diff.getPath();
DepthSearch depthSearch = new DepthSearch(indexDb);
Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
String refSpec = Ref.WORK_HEAD + ":" + path;
RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
ImmutableList<Optional<Object>> values = feature.getValues();
ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
Map<Name, Optional<?>> attrs = Maps.newHashMap();
for (int i = 0; i < oldDescriptors.size(); i++) {
PropertyDescriptor descriptor = oldDescriptors.get(i);
if (newDescriptors.contains(descriptor)) {
Optional<Object> value = values.get(i);
attrs.put(descriptor.getName(), value);
}
}
Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
Optional<?> oldValue = attrs.get(entry.getKey().getName());
attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
}
}
Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
Entry<Name, Optional<?>> entry = iterator.next();
featureBuilder.set(entry.getKey(), entry.getValue().orNull());
}
SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
workTree.insert(NodeRef.parentPath(path), featureToInsert);
}
ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
for (FeatureTypeDiff diff : alteredTrees) {
Optional<RevFeatureType> featureType;
if (diff.getOldFeatureType().isNull()) {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.createTypeTree(diff.getPath(), featureType.get().type());
} else if (diff.getNewFeatureType().isNull()) {
workTree.delete(diff.getPath());
} else {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.updateTypeTree(diff.getPath(), featureType.get().type());
}
}
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class BlameOp method _call.
@Override
protected BlameReport _call() {
String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
if (!id.isPresent()) {
throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
}
TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
if (!type.equals(TYPE.FEATURE)) {
throw new BlameException(StatusCode.PATH_NOT_FEATURE);
}
Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
BlameReport report = new BlameReport(featureType.get());
Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
RevCommit commit = log.next();
RevObjectParse revObjectParse = command(RevObjectParse.class);
DiffOp diffOp = command(DiffOp.class);
DiffFeature diffFeature = command(DiffFeature.class);
while (!report.isComplete()) {
if (!log.hasNext()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
RevCommit commitB = log.next();
Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (path.equals(diff.newPath())) {
if (diff.isAdd()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
while (iter.hasNext()) {
PropertyDescriptor key = iter.next();
Optional<?> value = attribDiffs.get(key).getNewValue();
String attribute = key.getName().toString();
report.addDiff(attribute, value, commit);
}
}
}
commit = commitB;
}
return report;
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class FeatureNodeRefFromRefspec method getFeatureFromRefSpec.
private Optional<RevFeature> getFeatureFromRefSpec() {
Optional<RevObject> revObject = command(RevObjectParse.class).setRefSpec(ref).call(RevObject.class);
if (!revObject.isPresent()) {
// let's try to see if it is a feature in the working tree
NodeRef.checkValidPath(ref);
Optional<NodeRef> elementRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(ref).setIndex(true).call();
Preconditions.checkArgument(elementRef.isPresent(), "Invalid reference: %s", ref);
ObjectId id = elementRef.get().objectId();
revObject = command(RevObjectParse.class).setObjectId(id).call(RevObject.class);
}
if (revObject.isPresent()) {
Preconditions.checkArgument(TYPE.FEATURE.equals(revObject.get().getType()), "%s does not resolve to a feature", ref);
return Optional.of(RevFeature.class.cast(revObject.get()));
} else {
return Optional.absent();
}
}
Aggregations