use of org.locationtech.geogig.repository.WorkingTree 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.repository.WorkingTree 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.repository.WorkingTree in project GeoGig by boundlessgeo.
the class CommitOpTest method testCommitEmptyTreeOnNonEmptyRepo.
@Test
public void testCommitEmptyTreeOnNonEmptyRepo() throws Exception {
insertAndAdd(points1, points2);
geogig.command(CommitOp.class).call();
// insertAndAdd(lines1, lines2);
WorkingTree workingTree = geogig.getRepository().workingTree();
final String emptyTreeName = "emptyTree";
workingTree.createTypeTree(emptyTreeName, pointsType);
{
List<DiffEntry> unstaged = toList(workingTree.getUnstaged(null));
assertEquals(unstaged.toString(), 1, unstaged.size());
// assertEquals(NodeRef.ROOT, unstaged.get(0).newName());
assertEquals(emptyTreeName, unstaged.get(0).newName());
}
geogig.command(AddOp.class).call();
{
StagingArea index = geogig.getRepository().index();
List<DiffEntry> staged = toList(index.getStaged(null));
assertEquals(staged.toString(), 1, staged.size());
// assertEquals(NodeRef.ROOT, staged.get(0).newName());
assertEquals(emptyTreeName, staged.get(0).newName());
}
CommitOp commitCommand = geogig.command(CommitOp.class);
RevCommit commit = commitCommand.call();
assertNotNull(commit);
RevTree head = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ref = geogig.command(FindTreeChild.class).setChildPath(emptyTreeName).setParent(head).call();
assertTrue(ref.isPresent());
}
use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.
the class DiffOpTest method testReportRename.
@Test
public void testReportRename() throws Exception {
insertAndAdd(lines1);
final RevCommit commit1 = geogig.command(CommitOp.class).setAll(true).call();
Feature lines1B = feature(linesType, idL2, "StringProp2_1", new Integer(1000), "LINESTRING (1 1, 2 2)");
delete(lines1);
// insert(lines2);
WorkingTree workTree = repo.workingTree();
Name name = lines1.getType().getName();
String parentPath = name.getLocalPart();
@SuppressWarnings("unused") Node ref = workTree.insert(parentPath, lines1B);
geogig.command(AddOp.class).call();
RevCommit commit2 = geogig.command(CommitOp.class).setAll(true).call();
List<DiffEntry> diffs;
diffOp.setOldVersion(commit1.getId());
diffOp.setNewVersion(commit2.getId());
diffs = toList(diffOp.call());
// this is reported as an addition and a removal, with both
assertEquals(2, diffs.size());
// nodes pointing to same ObjectId
assertEquals(diffs.get(0).newObjectId(), diffs.get(1).oldObjectId());
assertEquals(diffs.get(1).newObjectId(), diffs.get(0).oldObjectId());
}
use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.
the class GeoGigDataStore method createSchema.
/**
* Creates a new feature type tree on the {@link #getOrFigureOutBranch() current branch}.
* <p>
* Implementation detail: the operation is the homologous to starting a transaction, checking
* out the current/configured branch, creating the type tree inside the transaction, issueing a
* geogig commit, and committing the transaction for the created tree to be merged onto the
* configured branch.
*/
@Override
public void createSchema(SimpleFeatureType featureType) throws IOException {
if (!allowTransactions) {
throw new IllegalStateException("Configured head " + refspec + " is not a branch; transactions are not supported.");
}
GeogigTransaction tx = getCommandLocator(null).command(TransactionBegin.class).call();
boolean abort = false;
try {
String treePath = featureType.getName().getLocalPart();
// check out the datastore branch on the transaction space
final String branch = getOrFigureOutBranch();
tx.command(CheckoutOp.class).setForce(true).setSource(branch).call();
// now we can use the transaction working tree with the correct branch checked out
WorkingTree workingTree = tx.workingTree();
workingTree.createTypeTree(treePath, featureType);
tx.command(AddOp.class).addPattern(treePath).call();
tx.command(CommitOp.class).setMessage("Created feature type tree " + treePath).call();
tx.commit();
} catch (IllegalArgumentException alreadyExists) {
abort = true;
throw new IOException(alreadyExists.getMessage(), alreadyExists);
} catch (Exception e) {
abort = true;
throw Throwables.propagate(e);
} finally {
if (abort) {
tx.abort();
}
}
}
Aggregations