Search in sources :

Example 61 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMMapTest method testMapping.

@Test
public void testMapping() throws Exception {
    // import and check
    String filename = OSMImportOp.class.getResource("ways.xml").getFile();
    File file = new File(filename);
    cli.execute("osm", "import", file.getAbsolutePath());
    cli.execute("add");
    cli.execute("commit", "-m", "message");
    GeoGIG geogig = cli.newGeoGIG();
    Optional<RevTree> tree = geogig.command(RevObjectParse.class).setRefSpec("HEAD:node").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    tree = geogig.command(RevObjectParse.class).setRefSpec("HEAD:way").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    // map
    String mappingFilename = OSMMap.class.getResource("mapping.json").getFile();
    File mappingFile = new File(mappingFilename);
    cli.execute("osm", "map", mappingFile.getAbsolutePath());
    // check that a feature was correctly mapped
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets/31045880").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals("345117525;345117526;1300224327;345117527", values.get(3).get());
    assertEquals("yes", values.get(1).get());
    // check that a feature was correctly ignored
    revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets/31347480").call(RevFeature.class);
    assertFalse(revFeature.isPresent());
    geogig.close();
}
Also used : Optional(com.google.common.base.Optional) RevFeature(org.locationtech.geogig.api.RevFeature) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 62 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMApplyDiffTest method testApplyDiff.

@Test
public void testApplyDiff() throws Exception {
    // import and check
    GeoGIG geogig = cli.newGeoGIG();
    String filename = OSMImportOp.class.getResource("nodes_for_changeset2.xml").getFile();
    File file = new File(filename);
    cli.execute("osm", "import", file.getAbsolutePath());
    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 = OSMImportOp.class.getResource("changeset.xml").getFile();
    File changesetFile = new File(changesetFilename);
    cli.execute("osm", "apply-diff", changesetFile.getAbsolutePath());
    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());
    geogig.close();
}
Also used : RevFeature(org.locationtech.geogig.api.RevFeature) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) GeoGIG(org.locationtech.geogig.api.GeoGIG) Test(org.junit.Test)

Example 63 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMImportOpTest method testImportWithMappingAndNoRaw.

@Test
public void testImportWithMappingAndNoRaw() throws Exception {
    String filename = getClass().getResource("ways.xml").getFile();
    File file = new File(filename);
    // Define a mapping
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> mappings = Maps.newHashMap();
    mappings.put("oneway", Lists.newArrayList("yes"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("onewaystreets", mappings, filterExclude, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    // import with mapping and check import went ok and canonical folders were not created
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).setMapping(mapping).setNoRaw(true).call();
    long unstaged = geogig.getRepository().workingTree().countUnstaged("node").featureCount();
    assertEquals(0, unstaged);
    unstaged = geogig.getRepository().workingTree().countUnstaged("way").featureCount();
    assertEquals(0, unstaged);
    unstaged = geogig.getRepository().workingTree().countUnstaged("onewaystreets").featureCount();
    assertEquals(2, unstaged);
    Optional<Node> feature = geogig.getRepository().workingTree().findUnstaged("onewaystreets/31045880");
    assertTrue(feature.isPresent());
    // check that the mapping was correctly performed
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setObjectId(feature.get().getObjectId()).call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals("31045880", values.get(0).get().toString());
    assertEquals("yes", values.get(1).get());
    // check it has not created mapping log files
    File osmMapFolder = geogig.command(ResolveOSMMappingLogFolder.class).call();
    file = new File(osmMapFolder, "onewaystreets");
    assertFalse(file.exists());
    file = new File(osmMapFolder, geogig.getRepository().workingTree().getTree().getId().toString());
    assertFalse(file.exists());
}
Also used : ResolveOSMMappingLogFolder(org.locationtech.geogig.osm.internal.log.ResolveOSMMappingLogFolder) Optional(com.google.common.base.Optional) Node(org.locationtech.geogig.api.Node) RevFeature(org.locationtech.geogig.api.RevFeature) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File) Test(org.junit.Test)

Example 64 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class WorkingTree method updateTypeTree.

/**
     * Updates the definition of a Feature type associated as default feature type to a given path.
     * It also modifies the metadataId associated to features under the passed path, which used the
     * previous default feature type.
     * 
     * @param path the path
     * @param featureType the new feature type definition to set as default for the passed path
     */
public NodeRef updateTypeTree(final String treePath, final FeatureType featureType) {
    // TODO: This is not the optimal way of doing this. A better solution should be found.
    final RevTree workHead = getTree();
    Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(workHead).setChildPath(treePath).call();
    Preconditions.checkArgument(typeTreeRef.isPresent(), "Tree does not exist: %s", treePath);
    Iterator<NodeRef> iter = context.command(LsTreeOp.class).setReference(treePath).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
    final RevFeatureType revType = RevFeatureTypeImpl.build(featureType);
    indexDatabase.put(revType);
    final ObjectId metadataId = revType.getId();
    RevTreeBuilder treeBuilder = new RevTreeBuilder(indexDatabase);
    final RevTree newTree = treeBuilder.build();
    ObjectId newWorkHeadId = context.command(WriteBack.class).setToIndex(true).setAncestor(workHead.builder(indexDatabase)).setChildPath(treePath).setTree(newTree).setMetadataId(metadataId).call();
    updateWorkHead(newWorkHeadId);
    Map<ObjectId, FeatureBuilder> featureBuilders = Maps.newHashMap();
    while (iter.hasNext()) {
        NodeRef noderef = iter.next();
        RevFeature feature = context.command(RevObjectParse.class).setObjectId(noderef.objectId()).call(RevFeature.class).get();
        if (!featureBuilders.containsKey(noderef.getMetadataId())) {
            RevFeatureType ft = context.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
            featureBuilders.put(noderef.getMetadataId(), new FeatureBuilder(ft));
        }
        FeatureBuilder fb = featureBuilders.get(noderef.getMetadataId());
        String parentPath = NodeRef.parentPath(NodeRef.appendChild(treePath, noderef.path()));
        insert(parentPath, fb.build(noderef.getNode().getName(), feature));
    }
    return context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(treePath).call().get();
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) NodeRef(org.locationtech.geogig.api.NodeRef) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree)

Example 65 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class WorkingTree method insert.

/**
     * Inserts the given {@code features} into the working tree, using the {@code treePathResolver}
     * function to determine to which tree each feature is added.
     * 
     * @param treePathResolver a function that determines the path of the tree where each feature
     *        node is stored
     * @param features the features to insert, possibly of different schema and targetted to
     *        different tree paths
     * @param listener a progress listener
     * @param insertedTarget if provided, all nodes created will be added to this list. Beware of
     *        possible memory implications when inserting a lot of features.
     * @param collectionSize if given, used to determine progress and notify the {@code listener}
     * @return the total number of inserted features
     */
public void insert(final Function<Feature, String> treePathResolver, Iterator<? extends Feature> features, final ProgressListener listener, @Nullable final List<Node> insertedTarget, @Nullable final Integer collectionSize) {
    checkArgument(collectionSize == null || collectionSize.intValue() > -1);
    final int nTreeThreads = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
    final ExecutorService treeBuildingService = Executors.newFixedThreadPool(nTreeThreads, new ThreadFactoryBuilder().setNameFormat("WorkingTree-tree-builder-%d").build());
    final WorkingTreeInsertHelper insertHelper;
    insertHelper = new WorkingTreeInsertHelper(indexDatabase, context, getTree(), treePathResolver, treeBuildingService);
    UnmodifiableIterator<? extends Feature> filtered = Iterators.filter(features, new Predicate<Feature>() {

        @Override
        public boolean apply(Feature feature) {
            if (listener.isCanceled()) {
                return false;
            }
            if (feature instanceof FeatureToDelete) {
                insertHelper.remove((FeatureToDelete) feature);
                return false;
            } else {
                return true;
            }
        }
    });
    Iterator<RevObject> objects = Iterators.transform(filtered, new Function<Feature, RevObject>() {

        private int count;

        @Override
        public RevFeature apply(Feature feature) {
            final RevFeature revFeature = RevFeatureBuilder.build(feature);
            ObjectId id = revFeature.getId();
            final Node node = insertHelper.put(id, feature);
            if (insertedTarget != null) {
                insertedTarget.add(node);
            }
            count++;
            if (collectionSize == null) {
                listener.setProgress(count);
            } else {
                listener.setProgress((float) (count * 100) / collectionSize.intValue());
            }
            return revFeature;
        }
    });
    try {
        listener.started();
        indexDatabase.putAll(objects);
        if (listener.isCanceled()) {
            return;
        }
        listener.setDescription("Building trees for " + new TreeSet<String>(insertHelper.getTreeNames()));
        Stopwatch sw = Stopwatch.createStarted();
        Map<NodeRef, RevTree> trees = insertHelper.buildTrees();
        listener.setDescription(String.format("Trees built in %s", sw.stop()));
        for (Map.Entry<NodeRef, RevTree> treeEntry : trees.entrySet()) {
            if (!listener.isCanceled()) {
                NodeRef treeRef = treeEntry.getKey();
                RevTree newFeatureTree = treeEntry.getValue();
                String treePath = treeRef.path();
                ObjectId newRootTree = context.command(WriteBack.class).setAncestor(getTreeSupplier()).setChildPath(treePath).setMetadataId(treeRef.getMetadataId()).setToIndex(true).setTree(newFeatureTree).call();
                updateWorkHead(newRootTree);
            }
        }
        listener.complete();
    } finally {
        treeBuildingService.shutdownNow();
    }
}
Also used : RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Stopwatch(com.google.common.base.Stopwatch) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) NodeRef(org.locationtech.geogig.api.NodeRef) TreeSet(java.util.TreeSet) ExecutorService(java.util.concurrent.ExecutorService) RevFeature(org.locationtech.geogig.api.RevFeature) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Map(java.util.Map) RevTree(org.locationtech.geogig.api.RevTree)

Aggregations

RevFeature (org.locationtech.geogig.api.RevFeature)89 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)46 Test (org.junit.Test)45 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)43 Optional (com.google.common.base.Optional)40 NodeRef (org.locationtech.geogig.api.NodeRef)32 File (java.io.File)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)24 ObjectId (org.locationtech.geogig.api.ObjectId)21 AddOp (org.locationtech.geogig.api.porcelain.AddOp)21 ImmutableList (com.google.common.collect.ImmutableList)20 Feature (org.opengis.feature.Feature)20 List (java.util.List)19 RevObject (org.locationtech.geogig.api.RevObject)18 RevTree (org.locationtech.geogig.api.RevTree)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 ArrayList (java.util.ArrayList)15 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)15 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15