Search in sources :

Example 86 with Node

use of org.locationtech.geogig.api.Node 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 87 with Node

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

the class FormatCommonV1 method readTree.

public static RevTree readTree(ObjectId id, DataInput in) throws IOException {
    final long size = in.readLong();
    final int treeCount = in.readInt();
    final ImmutableList.Builder<Node> featuresBuilder = new ImmutableList.Builder<Node>();
    final ImmutableList.Builder<Node> treesBuilder = new ImmutableList.Builder<Node>();
    final SortedMap<Integer, Bucket> buckets = new TreeMap<Integer, Bucket>();
    final int nFeatures = in.readInt();
    for (int i = 0; i < nFeatures; i++) {
        Node n = readNode(in);
        if (n.getType() != RevObject.TYPE.FEATURE) {
            throw new IllegalStateException("Non-feature node in tree's feature list.");
        }
        featuresBuilder.add(n);
    }
    final int nTrees = in.readInt();
    for (int i = 0; i < nTrees; i++) {
        Node n = readNode(in);
        if (n.getType() != RevObject.TYPE.TREE) {
            throw new IllegalStateException("Non-tree node in tree's subtree list.");
        }
        treesBuilder.add(n);
    }
    final int nBuckets = in.readInt();
    for (int i = 0; i < nBuckets; i++) {
        int key = in.readInt();
        Bucket bucket = readBucket(in);
        buckets.put(key, bucket);
    }
    ImmutableList<Node> trees = treesBuilder.build();
    ImmutableList<Node> features = featuresBuilder.build();
    if (nTrees == 0 && nFeatures == 0 && nBuckets == 0) {
        return RevTree.EMPTY;
    } else if (trees.isEmpty() && features.isEmpty()) {
        return RevTreeImpl.createNodeTree(id, size, treeCount, buckets);
    } else if (buckets.isEmpty()) {
        return RevTreeImpl.createLeafTree(id, size, features, trees);
    } else {
        throw new IllegalArgumentException("Tree has mixed buckets and nodes; this is not supported.");
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) Builder(com.google.common.collect.ImmutableList.Builder) Node(org.locationtech.geogig.api.Node) TreeMap(java.util.TreeMap) Bucket(org.locationtech.geogig.api.Bucket)

Example 88 with Node

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

the class WorkingTree method delete.

/**
     * Deletes a collection of features of the same type from the working tree and updates the
     * WORK_HEAD ref.
     * 
     * @param typeName feature type
     * @param filter - currently unused
     * @param affectedFeatures features to remove
     * @throws Exception
     */
public void delete(final Name typeName, final Filter filter, final Iterator<Feature> affectedFeatures) throws Exception {
    Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(typeName.getLocalPart()).call();
    ObjectId parentMetadataId = null;
    if (typeTreeRef.isPresent()) {
        parentMetadataId = typeTreeRef.get().getMetadataId();
    }
    RevTreeBuilder parentTree = context.command(FindOrCreateSubtree.class).setParent(Suppliers.ofInstance(Optional.of(getTree()))).setIndex(true).setChildPath(typeName.getLocalPart()).call().builder(indexDatabase);
    String fid;
    String featurePath;
    while (affectedFeatures.hasNext()) {
        fid = affectedFeatures.next().getIdentifier().getID();
        featurePath = NodeRef.appendChild(typeName.getLocalPart(), fid);
        Optional<Node> ref = findUnstaged(featurePath);
        if (ref.isPresent()) {
            parentTree.remove(ref.get().getName());
        }
    }
    ObjectId newTree = context.command(WriteBack.class).setAncestor(getTree().builder(indexDatabase)).setMetadataId(parentMetadataId).setChildPath(typeName.getLocalPart()).setToIndex(true).setTree(parentTree.build()).call();
    updateWorkHead(newTree);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectId(org.locationtech.geogig.api.ObjectId) FindOrCreateSubtree(org.locationtech.geogig.api.plumbing.FindOrCreateSubtree) Node(org.locationtech.geogig.api.Node) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder)

Example 89 with Node

use of org.locationtech.geogig.api.Node 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)

Example 90 with Node

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

the class FormatCommonV1 method readNode.

public static Node readNode(DataInput in) throws IOException {
    final String name = in.readUTF();
    final byte[] objectId = new byte[20];
    in.readFully(objectId);
    final byte[] metadataId = new byte[20];
    in.readFully(metadataId);
    final RevObject.TYPE contentType = RevObject.TYPE.valueOf(in.readByte());
    final Envelope bbox = readBBox(in);
    final Node node;
    node = Node.create(name, ObjectId.createNoClone(objectId), ObjectId.createNoClone(metadataId), contentType, bbox);
    return node;
}
Also used : RevObject(org.locationtech.geogig.api.RevObject) Node(org.locationtech.geogig.api.Node) Envelope(com.vividsolutions.jts.geom.Envelope)

Aggregations

Node (org.locationtech.geogig.api.Node)117 RevTree (org.locationtech.geogig.api.RevTree)56 Test (org.junit.Test)50 ObjectId (org.locationtech.geogig.api.ObjectId)44 NodeRef (org.locationtech.geogig.api.NodeRef)24 Bucket (org.locationtech.geogig.api.Bucket)20 TreeTestSupport.featureNode (org.locationtech.geogig.api.plumbing.diff.TreeTestSupport.featureNode)18 Envelope (com.vividsolutions.jts.geom.Envelope)16 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)14 RevCommit (org.locationtech.geogig.api.RevCommit)10 Map (java.util.Map)9 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)9 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)9 File (java.io.File)7 RevFeature (org.locationtech.geogig.api.RevFeature)7 WorkingTree (org.locationtech.geogig.repository.WorkingTree)7 Feature (org.opengis.feature.Feature)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 Stopwatch (com.google.common.base.Stopwatch)6 SortedMap (java.util.SortedMap)5