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());
}
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.");
}
}
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);
}
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();
}
}
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;
}
Aggregations