use of com.vividsolutions.jts.geom.Geometry in project GeoGig by boundlessgeo.
the class MergeFeaturesOp method merge.
@SuppressWarnings("unchecked")
private Feature merge(RevFeature featureA, RevFeature featureB, RevFeature ancestor, RevFeatureType featureType) {
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) featureType.type());
ImmutableList<Optional<Object>> valuesA = featureA.getValues();
ImmutableList<Optional<Object>> valuesB = featureB.getValues();
ImmutableList<Optional<Object>> valuesAncestor = ancestor.getValues();
ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
boolean isGeom = Geometry.class.isAssignableFrom(descriptor.getType().getBinding());
Name name = descriptor.getName();
Optional<Object> valueAncestor = valuesAncestor.get(i);
Optional<Object> valueA = valuesA.get(i);
Optional<Object> valueB = valuesB.get(i);
if (!valueA.equals(valueAncestor)) {
Optional<Object> merged = valueA;
if (isGeom && !valueB.equals(valueAncestor)) {
// true merge is only done with
// geometries
GeometryAttributeDiff diffB = new GeometryAttributeDiff(Optional.fromNullable((Geometry) valueAncestor.orNull()), Optional.fromNullable((Geometry) valueB.orNull()));
merged = (Optional<Object>) diffB.applyOn(valueA);
}
featureBuilder.set(name, merged.orNull());
} else {
featureBuilder.set(name, valueB.orNull());
}
}
return featureBuilder.buildFeature(nodeRefA.name());
}
use of com.vividsolutions.jts.geom.Geometry in project GeoGig by boundlessgeo.
the class LCSGeometryDiffImpl method geomToStringOfCoordinates.
private String geomToStringOfCoordinates(Optional<Geometry> opt) {
if (!opt.isPresent()) {
return "";
}
Function<Coordinate, String> printCoords = new Function<Coordinate, String>() {
@Override
@Nullable
public String apply(@Nullable Coordinate coord) {
return Double.toString(coord.x) + "," + Double.toString(coord.y);
}
};
StringBuilder sb = new StringBuilder();
Geometry geom = opt.get();
sb.append(geom.getGeometryType() + " ");
int n = geom.getNumGeometries();
for (int i = 0; i < n; i++) {
Geometry subgeom = geom.getGeometryN(i);
if (subgeom instanceof Polygon) {
Polygon polyg = (Polygon) subgeom;
Coordinate[] coords = polyg.getExteriorRing().getCoordinates();
Iterator<String> iter = Iterators.transform(Iterators.forArray(coords), printCoords);
sb.append(Joiner.on(' ').join(iter));
for (int j = 0; j < polyg.getNumInteriorRing(); j++) {
coords = polyg.getInteriorRingN(j).getCoordinates();
iter = Iterators.transform(Iterators.forArray(coords), printCoords);
sb.append(" " + INNER_RING_SEPARATOR + " ");
sb.append(Joiner.on(' ').join(iter));
}
if (i < n - 1) {
sb.append(" " + SUBGEOM_SEPARATOR + " ");
}
} else {
Coordinate[] coords = subgeom.getCoordinates();
Iterator<String> iter = Iterators.transform(Iterators.forArray(coords), printCoords);
sb.append(Joiner.on(' ').join(iter));
sb.append(" " + SUBGEOM_SEPARATOR + " ");
}
}
String s = sb.toString().trim();
return s;
}
use of com.vividsolutions.jts.geom.Geometry 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 com.vividsolutions.jts.geom.Geometry in project GeoGig by boundlessgeo.
the class ResolveConflict 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 (this.getTransactionId() == null) {
throw new CommandSpecException("No transaction was specified, add requires a transaction to preserve the stability of the repository.");
}
final Context geogig = this.getCommandLocator(context);
RevTree revTree = geogig.workingTree().getTree();
Optional<NodeRef> nodeRef = geogig.command(FindTreeChild.class).setParent(revTree).setChildPath(NodeRef.parentPath(path)).setIndex(true).call();
Preconditions.checkArgument(nodeRef.isPresent(), "Invalid reference: %s", NodeRef.parentPath(path));
RevFeatureType revFeatureType = geogig.command(RevObjectParse.class).setObjectId(nodeRef.get().getMetadataId()).call(RevFeatureType.class).get();
RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(objectId).call(RevFeature.class).get();
CoordinateReferenceSystem crs = revFeatureType.type().getCoordinateReferenceSystem();
Envelope bounds = ReferencedEnvelope.create(crs);
Optional<Object> o;
for (int i = 0; i < revFeature.getValues().size(); i++) {
o = revFeature.getValues().get(i);
if (o.isPresent() && o.get() instanceof Geometry) {
Geometry g = (Geometry) o.get();
if (bounds.isNull()) {
bounds.init(JTS.bounds(g, crs));
} else {
bounds.expandToInclude(JTS.bounds(g, crs));
}
}
}
NodeRef node = new NodeRef(Node.create(NodeRef.nodeFromPath(path), objectId, ObjectId.NULL, TYPE.FEATURE, bounds), NodeRef.parentPath(path), ObjectId.NULL);
Optional<NodeRef> parentNode = geogig.command(FindTreeChild.class).setParent(geogig.workingTree().getTree()).setChildPath(node.getParentPath()).setIndex(true).call();
RevTreeBuilder treeBuilder = null;
ObjectId metadataId = ObjectId.NULL;
if (parentNode.isPresent()) {
metadataId = parentNode.get().getMetadataId();
Optional<RevTree> parsed = geogig.command(RevObjectParse.class).setObjectId(parentNode.get().getNode().getObjectId()).call(RevTree.class);
checkState(parsed.isPresent(), "Parent tree couldn't be found in the repository.");
treeBuilder = new RevTreeBuilder(geogig.objectDatabase(), parsed.get());
treeBuilder.remove(node.getNode().getName());
} else {
treeBuilder = new RevTreeBuilder(geogig.stagingDatabase());
}
treeBuilder.put(node.getNode());
ObjectId newTreeId = geogig.command(WriteBack.class).setAncestor(geogig.workingTree().getTree().builder(geogig.stagingDatabase())).setChildPath(node.getParentPath()).setToIndex(true).setTree(treeBuilder.build()).setMetadataId(metadataId).call();
geogig.workingTree().updateWorkHead(newTreeId);
AddOp command = geogig.command(AddOp.class);
command.addPattern(path);
command.call();
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeElement("Add", "Success");
out.finish();
}
});
}
use of com.vividsolutions.jts.geom.Geometry 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");
}
}
Aggregations