use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class PatchSerializer method write.
public static void write(Writer w, Patch patch) throws IOException {
StringBuilder sb = new StringBuilder();
List<RevFeatureType> featureTypes = patch.getFeatureTypes();
for (RevFeatureType featureType : featureTypes) {
ObjectWriter<RevObject> writer = factory.createObjectWriter(TYPE.FEATURETYPE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
writer.write(featureType, output);
sb.append(output.toString());
sb.append('\n');
}
TextSerializationFactory factory = new TextSerializationFactory();
for (FeatureInfo feature : patch.getAddedFeatures()) {
String path = feature.getPath();
sb.append("A\t" + path + "\t" + feature.getFeatureType().getId() + "\n");
ObjectWriter<RevObject> writer = factory.createObjectWriter(TYPE.FEATURE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
RevFeature revFeature = RevFeatureBuilder.build(feature.getFeature());
try {
writer.write(revFeature, output);
} catch (IOException e) {
}
sb.append(output.toString());
sb.append('\n');
}
for (FeatureInfo feature : patch.getRemovedFeatures()) {
String path = feature.getPath();
sb.append("R\t" + path + "\t" + feature.getFeatureType().getId() + "\n");
ObjectWriter<RevObject> writer = factory.createObjectWriter(TYPE.FEATURE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
RevFeature revFeature = RevFeatureBuilder.build(feature.getFeature());
try {
writer.write(revFeature, output);
} catch (IOException e) {
}
sb.append(output.toString());
sb.append('\n');
}
for (FeatureDiff diff : patch.getModifiedFeatures()) {
sb.append("M\t" + diff.getPath() + "\t" + diff.getOldFeatureType().getId().toString() + "\t" + diff.getNewFeatureType().getId().toString() + "\n");
sb.append(diff.asText() + "\n");
}
for (FeatureTypeDiff diff : patch.getAlteredTrees()) {
sb.append(diff.toString() + "\n");
}
w.write(sb.toString());
w.flush();
}
use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class FeatureDiffWeb method run.
/**
* Runs the command and builds the appropriate response
*
* @param context - the context to use for this command
*
* @throws CommandSpecException
*/
@Override
public void run(CommandContext context) {
if (path == null || path.trim().isEmpty()) {
throw new CommandSpecException("No path for feature name specifed");
}
final Context geogig = this.getCommandLocator(context);
ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get();
ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get();
RevFeature newFeature = null;
RevFeatureType newFeatureType = null;
RevFeature oldFeature = null;
RevFeatureType oldFeatureType = null;
final Map<PropertyDescriptor, AttributeDiff> diffs;
Optional<NodeRef> ref = parseID(newId, geogig);
Optional<RevObject> object;
// need these to determine if the feature was added or removed so I can build the diffs
// myself until the FeatureDiff supports null values
boolean removed = false;
boolean added = false;
if (ref.isPresent()) {
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
if (object.isPresent() && object.get() instanceof RevFeatureType) {
newFeatureType = (RevFeatureType) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's featureType");
}
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
if (object.isPresent() && object.get() instanceof RevFeature) {
newFeature = (RevFeature) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's feature");
}
} else {
removed = true;
}
if (!oldId.equals(ObjectId.NULL)) {
ref = parseID(oldId, geogig);
if (ref.isPresent()) {
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
if (object.isPresent() && object.get() instanceof RevFeatureType) {
oldFeatureType = (RevFeatureType) object.get();
} else {
throw new CommandSpecException("Couldn't resolve oldCommit's featureType");
}
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
if (object.isPresent() && object.get() instanceof RevFeature) {
oldFeature = (RevFeature) object.get();
} else {
throw new CommandSpecException("Couldn't resolve oldCommit's feature");
}
} else {
added = true;
}
} else {
added = true;
}
if (removed) {
Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = oldFeature.getValues();
for (int index = 0; index < attributes.size(); index++) {
Optional<Object> value = values.get(index);
if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
Optional<Geometry> temp = Optional.absent();
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp));
}
} else {
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent()));
}
}
}
diffs = tempDiffs;
} else if (added) {
Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = newFeature.getValues();
for (int index = 0; index < attributes.size(); index++) {
Optional<Object> value = values.get(index);
if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
Optional<Geometry> temp = Optional.absent();
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull())));
}
} else {
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value));
}
}
}
diffs = tempDiffs;
} else {
FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all);
diffs = diff.getDiffs();
}
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeFeatureDiffResponse(diffs);
out.finish();
}
});
}
use of org.locationtech.geogig.api.RevObject 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.RevObject 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.RevObject in project GeoGig by boundlessgeo.
the class DeepMove method moveObject.
private void moveObject(final ObjectId objectId, final ObjectDatabase from, final ObjectDatabase to) {
RevObject object = from.get(objectId);
if (object instanceof RevTree) {
Set<ObjectId> metadataIds = new HashSet<ObjectId>();
moveTree(object.getId(), from, to, metadataIds);
for (ObjectId metadataId : metadataIds) {
moveObject(metadataId, from, to);
}
} else {
moveObject(object, from, to);
}
}
Aggregations