use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class OSMMapTest method testMappingWithPolygons.
@Test
public void testMappingWithPolygons() throws Exception {
// test a mapping with a a mapping rule that uses the polygon geometry type
String filename = OSMImportOp.class.getResource("closed_ways.xml").getFile();
File file = new File(filename);
cli.execute("osm", "import", file.getAbsolutePath());
cli.execute("add");
cli.execute("commit", "-m", "message");
Optional<RevTree> tree = cli.getGeogig().command(RevObjectParse.class).setRefSpec("HEAD:node").call(RevTree.class);
assertTrue(tree.isPresent());
assertTrue(tree.get().size() > 0);
tree = cli.getGeogig().command(RevObjectParse.class).setRefSpec("HEAD:way").call(RevTree.class);
assertTrue(tree.isPresent());
assertTrue(tree.get().size() > 0);
String mappingFilename = OSMMap.class.getResource("polygons_mapping.json").getFile();
File mappingFile = new File(mappingFilename);
cli.execute("osm", "map", mappingFile.getAbsolutePath());
Iterator<NodeRef> iter = cli.getGeogig().command(LsTreeOp.class).setReference("HEAD:areas").call();
assertTrue(iter.hasNext());
Optional<RevFeatureType> ft = cli.getGeogig().command(ResolveFeatureType.class).setRefSpec("HEAD:" + iter.next().path()).call();
assertTrue(ft.isPresent());
assertEquals(Polygon.class, ft.get().sortedDescriptors().get(1).getType().getBinding());
}
use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class OSMMapOpTest method testMappingNodes.
@Test
public void testMappingNodes() throws Exception {
// import and check that we have nodes
String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
File file = new File(filename);
geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
WorkingTree workTree = geogig.getRepository().workingTree();
long 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>> mappings = Maps.newHashMap();
mappings.put("highway", Lists.newArrayList("bus_stop"));
fields.put("geom", new AttributeDefinition("geom", FieldType.POINT));
fields.put("name", new AttributeDefinition("name", FieldType.STRING));
Map<String, List<String>> filterExclude = Maps.newHashMap();
MappingRule mappingRule = new MappingRule("busstops", mappings, 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:busstops/507464799").call(RevFeature.class);
assertTrue(revFeature.isPresent());
Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class).setRefSpec("HEAD:busstops/507464799").call();
assertTrue(featureType.isPresent());
ImmutableList<Optional<Object>> values = revFeature.get().getValues();
assertEquals(3, values.size());
String wkt = "POINT (7.1959361 50.739397)";
assertEquals(wkt, values.get(2).get().toString());
assertEquals(507464799l, values.get(0).get());
// Check that the corresponding log files have been added
File osmMapFolder = geogig.command(ResolveOSMMappingLogFolder.class).call();
file = new File(osmMapFolder, "busstops");
assertTrue(file.exists());
file = new File(osmMapFolder, geogig.getRepository().workingTree().getTree().getId().toString());
assertTrue(file.exists());
}
use of org.locationtech.geogig.api.RevFeatureType 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.RevFeatureType 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.RevFeatureType in project GeoGig by boundlessgeo.
the class Patch method featureTypeDiffAsString.
private String featureTypeDiffAsString(FeatureTypeDiff diff) {
StringBuilder sb = new StringBuilder();
sb.append(diff.toString() + "\n");
if (!diff.getNewFeatureType().equals(ObjectId.NULL) && !diff.getOldFeatureType().equals(ObjectId.NULL)) {
RevFeatureType oldFeatureType = getFeatureTypeFromId(diff.getOldFeatureType()).get();
RevFeatureType newFeatureType = getFeatureTypeFromId(diff.getNewFeatureType()).get();
ImmutableList<PropertyDescriptor> oldDescriptors = oldFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newDescriptors = newFeatureType.sortedDescriptors();
BitSet updatedDescriptors = new BitSet(newDescriptors.size());
for (int i = 0; i < oldDescriptors.size(); i++) {
PropertyDescriptor oldDescriptor = oldDescriptors.get(i);
int idx = newDescriptors.indexOf(oldDescriptor);
if (idx != -1) {
updatedDescriptors.set(idx);
} else {
Class<?> oldType = oldDescriptor.getType().getBinding();
sb.append("R\t" + oldDescriptors.get(i).getName().getLocalPart() + "[" + oldType.getName() + "]");
}
}
updatedDescriptors.flip(0, updatedDescriptors.length());
for (int i = updatedDescriptors.nextSetBit(0); i >= 0; i = updatedDescriptors.nextSetBit(i + 1)) {
PropertyDescriptor newDescriptor = newDescriptors.get(i);
Class<?> oldType = newDescriptor.getType().getBinding();
sb.append("A\t" + newDescriptors.get(i).getName().getLocalPart() + "[" + oldType.getName() + "]");
}
}
return sb.toString();
}
Aggregations