use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ResponseWriter method writeGeometryChanges.
/**
* Writes the response for a set of diffs while also supplying the geometry.
*
* @param geogig - a CommandLocator to call commands from
* @param diff - a DiffEntry iterator to build the response from
* @throws XMLStreamException
*/
public void writeGeometryChanges(final Context geogig, Iterator<DiffEntry> diff, int page, int elementsPerPage) throws XMLStreamException {
Iterators.advance(diff, page * elementsPerPage);
int counter = 0;
Iterator<GeometryChange> changeIterator = Iterators.transform(diff, new Function<DiffEntry, GeometryChange>() {
@Override
public GeometryChange apply(DiffEntry input) {
Optional<RevObject> feature = Optional.absent();
Optional<RevObject> type = Optional.absent();
String path = null;
String crsCode = null;
GeometryChange change = null;
if (input.changeType() == ChangeType.ADDED || input.changeType() == ChangeType.MODIFIED) {
feature = geogig.command(RevObjectParse.class).setObjectId(input.newObjectId()).call();
type = geogig.command(RevObjectParse.class).setObjectId(input.getNewObject().getMetadataId()).call();
path = input.getNewObject().path();
} else if (input.changeType() == ChangeType.REMOVED) {
feature = geogig.command(RevObjectParse.class).setObjectId(input.oldObjectId()).call();
type = geogig.command(RevObjectParse.class).setObjectId(input.getOldObject().getMetadataId()).call();
path = input.getOldObject().path();
}
if (feature.isPresent() && feature.get() instanceof RevFeature && type.isPresent() && type.get() instanceof RevFeatureType) {
RevFeatureType featureType = (RevFeatureType) type.get();
Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
for (PropertyDescriptor attrib : attribs) {
PropertyType attrType = attrib.getType();
if (attrType instanceof GeometryType) {
GeometryType gt = (GeometryType) attrType;
CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
if (crs != null) {
try {
crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
} catch (FactoryException e) {
crsCode = null;
}
if (crsCode != null) {
crsCode = "EPSG:" + crsCode;
}
}
break;
}
}
RevFeature revFeature = (RevFeature) feature.get();
FeatureBuilder builder = new FeatureBuilder(featureType);
GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
change = new GeometryChange(simpleFeature, input.changeType(), path, crsCode);
}
return change;
}
});
while (changeIterator.hasNext() && (elementsPerPage == 0 || counter < elementsPerPage)) {
GeometryChange next = changeIterator.next();
if (next != null) {
GeogigSimpleFeature feature = next.getFeature();
ChangeType change = next.getChangeType();
out.writeStartElement("Feature");
writeElement("change", change.toString());
writeElement("id", next.getPath());
List<Object> attributes = feature.getAttributes();
for (Object attribute : attributes) {
if (attribute instanceof Geometry) {
writeElement("geometry", ((Geometry) attribute).toText());
break;
}
}
if (next.getCRS() != null) {
writeElement("crs", next.getCRS());
}
out.writeEndElement();
counter++;
}
}
if (changeIterator.hasNext()) {
writeElement("nextPage", "true");
}
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ResponseWriter method writeConflicts.
/**
* Writes the response for a set of conflicts while also supplying the geometry.
*
* @param geogig - a CommandLocator to call commands from
* @param conflicts - a Conflict iterator to build the response from
* @throws XMLStreamException
*/
public void writeConflicts(final Context geogig, Iterator<Conflict> conflicts, final ObjectId ours, final ObjectId theirs) throws XMLStreamException {
Iterator<GeometryConflict> conflictIterator = Iterators.transform(conflicts, new Function<Conflict, GeometryConflict>() {
@Override
public GeometryConflict apply(Conflict input) {
ObjectId commitId = ours;
if (input.getOurs().equals(ObjectId.NULL)) {
commitId = theirs;
}
Optional<RevObject> object = geogig.command(RevObjectParse.class).setObjectId(commitId).call();
RevCommit commit = null;
if (object.isPresent() && object.get() instanceof RevCommit) {
commit = (RevCommit) object.get();
} else {
throw new CommandSpecException("Couldn't resolve id: " + commitId.toString() + " to a commit");
}
object = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call();
Optional<NodeRef> node = Optional.absent();
if (object.isPresent()) {
RevTree tree = (RevTree) object.get();
node = geogig.command(FindTreeChild.class).setParent(tree).setChildPath(input.getPath()).call();
} else {
throw new CommandSpecException("Couldn't resolve commit's treeId");
}
RevFeatureType type = null;
RevFeature feature = null;
if (node.isPresent()) {
object = geogig.command(RevObjectParse.class).setObjectId(node.get().getMetadataId()).call();
if (object.isPresent() && object.get() instanceof RevFeatureType) {
type = (RevFeatureType) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's featureType");
}
object = geogig.command(RevObjectParse.class).setObjectId(node.get().objectId()).call();
if (object.isPresent() && object.get() instanceof RevFeature) {
feature = (RevFeature) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's feature");
}
}
GeometryConflict conflict = null;
if (feature != null && type != null) {
String crsCode = null;
Collection<PropertyDescriptor> attribs = type.type().getDescriptors();
for (PropertyDescriptor attrib : attribs) {
PropertyType attrType = attrib.getType();
if (attrType instanceof GeometryType) {
GeometryType gt = (GeometryType) attrType;
CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
if (crs != null) {
try {
crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
} catch (FactoryException e) {
crsCode = null;
}
if (crsCode != null) {
crsCode = "EPSG:" + crsCode;
}
}
break;
}
}
FeatureBuilder builder = new FeatureBuilder(type);
GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(feature.getId().toString(), feature);
Geometry geom = null;
List<Object> attributes = simpleFeature.getAttributes();
for (Object attribute : attributes) {
if (attribute instanceof Geometry) {
geom = (Geometry) attribute;
break;
}
}
conflict = new GeometryConflict(input, geom, crsCode);
}
return conflict;
}
});
while (conflictIterator.hasNext()) {
GeometryConflict next = conflictIterator.next();
if (next != null) {
out.writeStartElement("Feature");
writeElement("change", "CONFLICT");
writeElement("id", next.getConflict().getPath());
writeElement("ourvalue", next.getConflict().getOurs().toString());
writeElement("theirvalue", next.getConflict().getTheirs().toString());
writeElement("geometry", next.getGeometry().toText());
if (next.getCRS() != null) {
writeElement("crs", next.getCRS());
}
out.writeEndElement();
}
}
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class GeoGigAPI method getFeaturesToCommit.
/**
* Returns an array with the features that are staged and ready to be commited. If noDeletions
* is true, it doesn't include features to be removed, only those ones to be added or modified,
* so it can be used to check the new data that will get commited into the repository
*
* @return
*/
public Feature[] getFeaturesToCommit(String path, boolean noDeletions) {
DiffOp diffOp = repository.command(DiffOp.class);
diffOp.setCompareIndex(true);
diffOp.setFilter(path);
Iterator<DiffEntry> diffs = diffOp.call();
List<Feature> list = Lists.newArrayList();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (!diff.changeType().equals(ChangeType.REMOVED) || !noDeletions) {
RevFeature revFeature = repository.command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = repository.command(RevObjectParse.class).setObjectId(diff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
FeatureBuilder builder = new FeatureBuilder(revFeatureType);
list.add(builder.build(diff.getNewObject().name(), revFeature));
}
}
return list.toArray(new Feature[0]);
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class GeoGigAPI method getFeatureFromHead.
/**
* Returns a feature from the Head of the repository, given its full path
*
* Returns null if the given path doesn't resolve to a feature
*
* @param path the path to the feature to return
*/
public Feature getFeatureFromHead(String path) {
String name = NodeRef.nodeFromPath(path);
String refSpec = "HEAD:" + path;
Optional<RevFeature> revFeature = repository.command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class);
if (revFeature.isPresent()) {
RevFeatureType revFeatureType = repository.command(ResolveFeatureType.class).setRefSpec(refSpec).call().get();
FeatureBuilder builder = new FeatureBuilder(revFeatureType);
return builder.build(name, revFeature.get());
} else {
return null;
}
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class OSMHistoryImport method parseGeometry.
/**
* @param primitive
* @param thisChangePointCache
* @return
*/
private Geometry parseGeometry(GeoGIG geogig, Primitive primitive, Map<Long, Coordinate> thisChangePointCache) {
if (primitive instanceof Relation) {
return null;
}
if (primitive instanceof Node) {
Optional<Point> location = ((Node) primitive).getLocation();
return location.orNull();
}
final Way way = (Way) primitive;
final ImmutableList<Long> nodes = way.getNodes();
StagingArea index = geogig.getRepository().index();
FeatureBuilder featureBuilder = new FeatureBuilder(NODE_REV_TYPE);
List<Coordinate> coordinates = Lists.newArrayList(nodes.size());
FindTreeChild findTreeChild = geogig.command(FindTreeChild.class);
findTreeChild.setIndex(true);
ObjectId rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(Ref.HEAD).call().get();
if (!rootTreeId.isNull()) {
RevTree headTree = geogig.command(RevObjectParse.class).setObjectId(rootTreeId).call(RevTree.class).get();
findTreeChild.setParent(headTree);
}
for (Long nodeId : nodes) {
Coordinate coord = thisChangePointCache.get(nodeId);
if (coord == null) {
String fid = String.valueOf(nodeId);
String path = NodeRef.appendChild(NODE_TYPE_NAME, fid);
Optional<org.locationtech.geogig.api.Node> ref = index.findStaged(path);
if (!ref.isPresent()) {
Optional<NodeRef> nodeRef = findTreeChild.setChildPath(path).call();
if (nodeRef.isPresent()) {
ref = Optional.of(nodeRef.get().getNode());
} else {
ref = Optional.absent();
}
}
if (ref.isPresent()) {
org.locationtech.geogig.api.Node nodeRef = ref.get();
RevFeature revFeature = index.getDatabase().getFeature(nodeRef.getObjectId());
String id = NodeRef.nodeFromPath(nodeRef.getName());
Feature feature = featureBuilder.build(id, revFeature);
Point p = (Point) ((SimpleFeature) feature).getAttribute("location");
if (p != null) {
coord = p.getCoordinate();
thisChangePointCache.put(Long.valueOf(nodeId), coord);
}
}
}
if (coord != null) {
coordinates.add(coord);
}
}
if (coordinates.size() < 2) {
return null;
}
return GEOMF.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
}
Aggregations