Search in sources :

Example 11 with WorkingTree

use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.

the class ImportOpTest method testDeleteException.

@Test
public void testDeleteException() throws Exception {
    WorkingTree workTree = mock(WorkingTree.class);
    Context cmdl = mock(Context.class);
    when(cmdl.workingTree()).thenReturn(workTree);
    doThrow(new RuntimeException("Exception")).when(workTree).delete(any(String.class));
    ImportOp importOp = new ImportOp();
    importOp.setContext(cmdl);
    importOp.setDataStore(TestHelper.createTestFactory().createDataStore(null));
    importOp.setAll(true);
    exception.expect(GeoToolsOpException.class);
    importOp.call();
}
Also used : Context(org.locationtech.geogig.api.Context) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Test(org.junit.Test)

Example 12 with WorkingTree

use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.

the class OSMHistoryImport method importOsmHistory.

private void importOsmHistory(GeogigCLI cli, ConsoleReader console, HistoryDownloader downloader, @Nullable Envelope featureFilter) throws IOException {
    Iterator<Changeset> changesets = downloader.fetchChangesets();
    GeoGIG geogig = cli.getGeogig();
    WorkingTree workingTree = geogig.getContext().workingTree();
    while (changesets.hasNext()) {
        Changeset changeset = changesets.next();
        if (changeset.isOpen()) {
            throw new CommandFailedException("Can't import past changeset " + changeset.getId() + " as it is still open.");
        }
        String desc = String.format("obtaining osm changeset %,d...", changeset.getId());
        console.print(desc);
        console.flush();
        Optional<Iterator<Change>> opchanges = changeset.getChanges().get();
        if (!opchanges.isPresent()) {
            updateBranchChangeset(geogig, changeset.getId());
            console.println(" does not apply.");
            console.flush();
            continue;
        }
        Iterator<Change> changes = opchanges.get();
        console.print("applying...");
        console.flush();
        ObjectId workTreeId = workingTree.getTree().getId();
        long changeCount = insertChanges(cli, changes, featureFilter);
        console.print(String.format("Applied %,d changes, staging...", changeCount));
        console.flush();
        ObjectId afterTreeId = workingTree.getTree().getId();
        DiffObjectCount diffCount = geogig.command(DiffCount.class).setOldVersion(workTreeId.toString()).setNewVersion(afterTreeId.toString()).call();
        geogig.command(AddOp.class).call();
        console.println(String.format("done. %,d changes actually applied.", diffCount.featureCount()));
        console.flush();
        commit(cli, changeset);
    }
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ObjectId(org.locationtech.geogig.api.ObjectId) Change(org.locationtech.geogig.osm.internal.history.Change) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) DiffObjectCount(org.locationtech.geogig.api.plumbing.diff.DiffObjectCount) Iterator(java.util.Iterator) Changeset(org.locationtech.geogig.osm.internal.history.Changeset) DiffCount(org.locationtech.geogig.api.plumbing.DiffCount) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 13 with WorkingTree

use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.

the class OSMUnmapOpTest method testMappingAndUnmappingOfNodesWithAlias.

@Test
public void testMappingAndUnmappingOfNodesWithAlias() throws Exception {
    // Import
    String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Map
    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_alias", 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();
    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());
    // unmap without having made any changes and check that the canonical folders are not
    // modified
    geogig.command(OSMUnmapOp.class).setPath("busstops").call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("way").count();
    assertEquals(0, unstaged);
    unstaged = workTree.countUnstaged("node").count();
    assertEquals(0, unstaged);
    // Modify a node
    GeometryFactory gf = new GeometryFactory();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder((SimpleFeatureType) featureType.get().type());
    fb.set("geom", gf.createPoint(new Coordinate(0, 1)));
    fb.set("name_alias", "newname");
    fb.set("id", 507464799l);
    SimpleFeature newFeature = fb.buildFeature("507464799");
    geogig.getRepository().workingTree().insert("busstops", newFeature);
    // check that it was correctly inserted in the working tree
    Optional<RevFeature> mapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:busstops/507464799").call(RevFeature.class);
    assertTrue(mapped.isPresent());
    values = mapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(2).get().toString());
    assertEquals(507464799l, ((Long) values.get(0).get()).longValue());
    assertEquals("newname", values.get(1).get().toString());
    // unmap
    geogig.command(OSMUnmapOp.class).setPath("busstops").call();
    unstaged = workTree.countUnstaged("node").featureCount();
    assertEquals(1, unstaged);
    // check that the unmapped node has the changes we introduced
    Optional<RevFeature> unmapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464799").call(RevFeature.class);
    assertTrue(unmapped.isPresent());
    values = unmapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(6).get().toString());
    assertEquals("bus:yes|public_transport:platform|highway:bus_stop|VRS:ortsteil:Hoholz|name:newname|VRS:ref:68566|VRS:gemeinde:BONN", values.get(3).get().toString());
    // check that unchanged nodes keep their attributes
    Optional<RevFeature> unchanged = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/1633594723").call(RevFeature.class);
    values = unchanged.get().getValues();
    assertEquals("14220478", values.get(4).get().toString());
    assertEquals("1355097351000", values.get(2).get().toString());
    assertEquals("2", values.get(1).get().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 14 with WorkingTree

use of org.locationtech.geogig.repository.WorkingTree in project GeoGig by boundlessgeo.

the class OSMMapOpTest method testMappingwithNoGeometry.

@Test
public void testMappingwithNoGeometry() throws Exception {
    // Test that an exception is thrown when the mapping does not contain a geometry field
    String filename = OSMImportOp.class.getResource("ways.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 a wrong mapping without geometry
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> filters = Maps.newHashMap();
    filters.put("oneway", Lists.newArrayList("yes"));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("onewaystreets", filters, filterExclude, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    // Try to create a mapping
    try {
        geogig.command(OSMMapOp.class).setMapping(mapping).call();
        fail();
    } catch (NullPointerException e) {
        assertTrue(e.getMessage().startsWith("The mapping rule does not define a geometry field"));
    }
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) WorkingTree(org.locationtech.geogig.repository.WorkingTree) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 15 with WorkingTree

use of org.locationtech.geogig.repository.WorkingTree 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());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ResolveOSMMappingLogFolder(org.locationtech.geogig.osm.internal.log.ResolveOSMMappingLogFolder) 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) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

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