Search in sources :

Example 16 with WorkingTree

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());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) Optional(com.google.common.base.Optional) WorkingTree(org.locationtech.geogig.repository.WorkingTree) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) Test(org.junit.Test)

Example 17 with WorkingTree

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());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 18 with WorkingTree

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());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) WorkingTree(org.locationtech.geogig.repository.WorkingTree) NodeRef(org.locationtech.geogig.api.NodeRef) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) StagingArea(org.locationtech.geogig.repository.StagingArea) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 19 with WorkingTree

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());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Node(org.locationtech.geogig.api.Node) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevCommit(org.locationtech.geogig.api.RevCommit) Name(org.opengis.feature.type.Name) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Test(org.junit.Test)

Example 20 with WorkingTree

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();
        }
    }
}
Also used : WorkingTree(org.locationtech.geogig.repository.WorkingTree) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CheckoutOp(org.locationtech.geogig.api.porcelain.CheckoutOp) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Aggregations

WorkingTree (org.locationtech.geogig.repository.WorkingTree)54 Test (org.junit.Test)32 AddOp (org.locationtech.geogig.api.porcelain.AddOp)25 List (java.util.List)18 ImmutableList (com.google.common.collect.ImmutableList)17 File (java.io.File)17 ArrayList (java.util.ArrayList)16 RevFeature (org.locationtech.geogig.api.RevFeature)15 Optional (com.google.common.base.Optional)12 SimpleFeature (org.opengis.feature.simple.SimpleFeature)12 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)11 NodeRef (org.locationtech.geogig.api.NodeRef)10 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)9 Name (org.opengis.feature.type.Name)9 GeoGIG (org.locationtech.geogig.api.GeoGIG)7 Node (org.locationtech.geogig.api.Node)7 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 RevCommit (org.locationtech.geogig.api.RevCommit)6 Coordinate (com.vividsolutions.jts.geom.Coordinate)5 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)5