use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ExportOp method getFeatures.
private static Iterator<SimpleFeature> getFeatures(final RevTree typeTree, final ObjectDatabase database, final ObjectId defaultMetadataId, final ProgressListener progressListener) {
Iterator<NodeRef> nodes = new DepthTreeIterator("", defaultMetadataId, typeTree, database, Strategy.FEATURES_ONLY);
// progress reporting
nodes = Iterators.transform(nodes, new Function<NodeRef, NodeRef>() {
private AtomicInteger count = new AtomicInteger();
@Override
public NodeRef apply(NodeRef input) {
progressListener.setProgress((count.incrementAndGet() * 100.f) / typeTree.size());
return input;
}
});
Function<NodeRef, SimpleFeature> asFeature = new Function<NodeRef, SimpleFeature>() {
private Map<ObjectId, FeatureBuilder> ftCache = Maps.newHashMap();
@Override
@Nullable
public SimpleFeature apply(final NodeRef input) {
final ObjectId metadataId = input.getMetadataId();
final RevFeature revFeature = database.getFeature(input.objectId());
FeatureBuilder featureBuilder = getBuilderFor(metadataId);
Feature feature = featureBuilder.build(input.name(), revFeature);
feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
feature.getUserData().put(RevFeature.class, revFeature);
feature.getUserData().put(RevFeatureType.class, featureBuilder.getType());
if (feature instanceof SimpleFeature) {
return (SimpleFeature) feature;
}
return null;
}
private FeatureBuilder getBuilderFor(final ObjectId metadataId) {
FeatureBuilder featureBuilder = ftCache.get(metadataId);
if (featureBuilder == null) {
RevFeatureType revFtype = database.getFeatureType(metadataId);
featureBuilder = new FeatureBuilder(revFtype);
ftCache.put(metadataId, featureBuilder);
}
return featureBuilder;
}
};
Iterator<SimpleFeature> asFeatures = Iterators.transform(nodes, asFeature);
UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
return filterNulls;
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class CreatePatchOp method _call.
@Override
protected Patch _call() {
Patch patch = new Patch();
Map<ObjectId, RevFeatureType> featureTypes = Maps.newHashMap();
while (diffs.hasNext()) {
DiffEntry diffEntry = diffs.next();
final NodeRef newObject = diffEntry.getNewObject();
final NodeRef oldObject = diffEntry.getOldObject();
if (diffEntry.changeType() == ChangeType.MODIFIED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
if (revObject instanceof RevFeature) {
FeatureDiff diff = command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
patch.addModifiedFeature(diff);
} else if (revObject instanceof RevTree) {
RevFeatureType oldFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
RevFeatureType newFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
patch.addFeatureType(oldFeatureType);
patch.addFeatureType(newFeatureType);
patch.addAlteredTree(diffEntry);
}
} else if (diffEntry.changeType() == ChangeType.ADDED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
if (revObject instanceof RevFeature) {
RevFeatureType featureType;
if (featureTypes.containsKey(newObject.getMetadataId())) {
featureType = featureTypes.get(newObject.getMetadataId());
} else {
featureType = command(RevObjectParse.class).setObjectId(newObject.getMetadataId()).call(RevFeatureType.class).get();
featureTypes.put(newObject.getMetadataId(), featureType);
}
FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
Feature feature = featureBuilder.build(diffEntry.newObjectId().toString(), (RevFeature) revObject);
String name = diffEntry.newPath();
patch.addAddedFeature(name, feature, featureType);
} else if (revObject instanceof RevTree) {
ObjectId metadataId = diffEntry.getNewObject().getMetadataId();
if (!metadataId.isNull()) {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
patch.addAlteredTree(diffEntry);
patch.addFeatureType(featureType);
}
}
} else if (diffEntry.changeType() == ChangeType.REMOVED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.oldObjectId()).call().get();
if (revObject instanceof RevFeature) {
RevFeatureType featureType;
if (featureTypes.containsKey(oldObject.getMetadataId())) {
featureType = featureTypes.get(oldObject.getMetadataId());
} else {
featureType = command(RevObjectParse.class).setObjectId(oldObject.getMetadataId()).call(RevFeatureType.class).get();
featureTypes.put(oldObject.getMetadataId(), featureType);
}
FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
Feature feature = featureBuilder.build(diffEntry.oldObjectId().toString(), (RevFeature) revObject);
String name = diffEntry.oldPath();
patch.addRemovedFeature(name, feature, featureType);
} else if (revObject instanceof RevTree) {
ObjectId metadataId = diffEntry.getOldObject().getMetadataId();
if (!metadataId.isNull()) {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
patch.addAlteredTree(diffEntry);
patch.addFeatureType(featureType);
}
}
}
}
return patch;
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ImportOp method alter.
/**
* Translates a feature pointed by a node from its original feature type to a given one, using
* values from those attributes that exist in both original and destination feature type. New
* attributes are populated with null values
*
* @param node The node that points to the feature. No checking is performed to ensure the node
* points to a feature instead of other type
* @param featureType the destination feature type
* @return a feature with the passed feature type and data taken from the input feature
*/
private Feature alter(NodeRef node, RevFeatureType featureType) {
RevFeature oldFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
RevFeatureType oldFeatureType;
oldFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newAttributes = featureType.sortedDescriptors();
ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
List<Optional<Object>> newValues = Lists.newArrayList();
for (int i = 0; i < newAttributes.size(); i++) {
int idx = oldAttributes.indexOf(newAttributes.get(i));
if (idx != -1) {
Optional<Object> oldValue = oldValues.get(idx);
newValues.add(oldValue);
} else {
newValues.add(Optional.absent());
}
}
RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
Feature feature = featureBuilder.build(node.name(), newFeature);
return feature;
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ExportOp method alter.
private Iterator<SimpleFeature> alter(Iterator<SimpleFeature> plainFeatures, final ObjectId targetFeatureTypeId) {
final RevFeatureType targetType = stagingDatabase().getFeatureType(targetFeatureTypeId);
Function<SimpleFeature, SimpleFeature> alterFunction = new Function<SimpleFeature, SimpleFeature>() {
@Override
public SimpleFeature apply(SimpleFeature input) {
final RevFeatureType oldFeatureType;
oldFeatureType = (RevFeatureType) input.getUserData().get(RevFeatureType.class);
final ObjectId metadataId = oldFeatureType.getId();
if (targetType.getId().equals(metadataId)) {
return input;
}
final RevFeature oldFeature;
oldFeature = (RevFeature) input.getUserData().get(RevFeature.class);
ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newAttributes = targetType.sortedDescriptors();
ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
List<Optional<Object>> newValues = Lists.newArrayList();
for (int i = 0; i < newAttributes.size(); i++) {
int idx = oldAttributes.indexOf(newAttributes.get(i));
if (idx != -1) {
Optional<Object> oldValue = oldValues.get(idx);
newValues.add(oldValue);
} else {
newValues.add(Optional.absent());
}
}
RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
FeatureBuilder featureBuilder = new FeatureBuilder(targetType);
SimpleFeature feature = (SimpleFeature) featureBuilder.build(input.getID(), newFeature);
return feature;
}
};
return Iterators.transform(plainFeatures, alterFunction);
}
use of org.locationtech.geogig.api.FeatureBuilder 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();
}
Aggregations