use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class DiffOpTest method testFilterMatchesSingleBlobChange.
@Test
public void testFilterMatchesSingleBlobChange() throws Exception {
final ObjectId initialOid = insertAndAdd(points1);
final RevCommit commit1 = geogig.command(CommitOp.class).setAll(true).call();
insertAndAdd(lines1);
final RevCommit commit2 = geogig.command(CommitOp.class).setAll(true).call();
((SimpleFeature) points1).setAttribute("sp", "modified");
final ObjectId modifiedOid = insertAndAdd(points1);
final RevCommit commit3 = geogig.command(CommitOp.class).setAll(true).call();
diffOp.setOldVersion(commit1.getId()).setNewVersion(commit3.getId());
diffOp.setFilter(NodeRef.appendChild(pointsName, points1.getIdentifier().getID()));
List<DiffEntry> diffs;
DiffEntry diff;
diffs = toList(diffOp.call());
assertEquals(1, diffs.size());
diff = diffs.get(0);
assertEquals(ChangeType.MODIFIED, diff.changeType());
assertEquals(initialOid, diff.oldObjectId());
assertEquals(modifiedOid, diff.newObjectId());
assertTrue(deleteAndAdd(points1));
final RevCommit commit4 = geogig.command(CommitOp.class).setAll(true).call();
diffOp.setOldVersion(commit2.getId()).setNewVersion(commit4.getId());
diffOp.setFilter(NodeRef.appendChild(pointsName, points1.getIdentifier().getID()));
diffs = toList(diffOp.call());
assertEquals(1, diffs.size());
diff = diffs.get(0);
assertEquals(ChangeType.REMOVED, diff.changeType());
assertEquals(initialOid, diff.oldObjectId());
assertEquals(ObjectId.NULL, diff.newObjectId());
// invert the order of old and new commit
diffOp.setOldVersion(commit4.getId()).setNewVersion(commit1.getId());
diffOp.setFilter(NodeRef.appendChild(pointsName, points1.getIdentifier().getID()));
diffs = toList(diffOp.call());
assertEquals(1, diffs.size());
diff = diffs.get(0);
assertEquals(ChangeType.ADDED, diff.changeType());
assertEquals(ObjectId.NULL, diff.oldObjectId());
assertEquals(initialOid, diff.newObjectId());
// different commit range
diffOp.setOldVersion(commit4.getId()).setNewVersion(commit3.getId());
diffOp.setFilter(NodeRef.appendChild(pointsName, points1.getIdentifier().getID()));
diffs = toList(diffOp.call());
assertEquals(1, diffs.size());
diff = diffs.get(0);
assertEquals(ChangeType.ADDED, diff.changeType());
assertEquals(ObjectId.NULL, diff.oldObjectId());
assertEquals(modifiedOid, diff.newObjectId());
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class FeatureBuilderTest method testFeatureBuilder.
@Test
public void testFeatureBuilder() {
FeatureBuilder builder = new FeatureBuilder(pointsType);
RevFeature point1 = RevFeatureBuilder.build(points1);
Feature test = builder.build(idP1, point1);
// assertEquals(points1.getValue(), test.getValue());
assertEquals(points1.getName(), test.getName());
assertEquals(points1.getIdentifier(), test.getIdentifier());
assertEquals(points1.getType(), test.getType());
assertEquals(points1.getUserData(), test.getUserData());
RevFeature feature = RevFeatureBuilder.build(test);
Feature test2 = builder.build(idP1, feature);
assertEquals(((SimpleFeature) test).getAttributes(), ((SimpleFeature) test2).getAttributes());
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class WorkingTreeTest method testInsertNonPagingFeatureSource.
@Test
public void testInsertNonPagingFeatureSource() throws Exception {
assertEquals(2, super.getGeogig().getPlatform().availableProcessors());
final List<SimpleFeature> features = ImmutableList.of((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3);
MemoryDataStore store = new MemoryDataStore();
store.addFeatures(features);
final QueryCapabilities caps = mock(QueryCapabilities.class);
when(caps.isOffsetSupported()).thenReturn(true);
@SuppressWarnings("rawtypes") FeatureSource source = store.getFeatureSource(pointsName);
assertFalse(source.getQueryCapabilities().isOffsetSupported());
String treePath = "target_typename";
workTree.insert(treePath, source, Query.ALL, LISTENER);
assertEquals(3, workTree.countUnstaged(treePath).featureCount());
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class MergeFeatureResource method post.
public void post(Representation entity) {
InputStream input = null;
try {
input = getRequest().getEntity().getStream();
final GeoGIG ggit = getGeogig(getRequest()).get();
final Reader body = new InputStreamReader(input);
final JsonParser parser = new JsonParser();
final JsonElement conflictJson = parser.parse(body);
if (conflictJson.isJsonObject()) {
final JsonObject conflict = conflictJson.getAsJsonObject();
String featureId = null;
RevFeature ourFeature = null;
RevFeatureType ourFeatureType = null;
RevFeature theirFeature = null;
RevFeatureType theirFeatureType = null;
JsonObject merges = null;
if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) {
featureId = conflict.get("path").getAsJsonPrimitive().getAsString();
}
Preconditions.checkState(featureId != null);
if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) {
String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString();
Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit);
if (ourNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
ourFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
ourFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) {
String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString();
Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit);
if (theirNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
theirFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
theirFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("merges") && conflict.get("merges").isJsonObject()) {
merges = conflict.get("merges").getAsJsonObject();
}
Preconditions.checkState(merges != null);
Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type()));
ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).sortedDescriptors();
for (Entry<String, JsonElement> entry : merges.entrySet()) {
int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
JsonObject attributeObject = entry.getValue().getAsJsonObject();
if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) {
JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive();
if (primitive.isString()) {
try {
Object object = valueFromString(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString());
featureBuilder.set(descriptor.getName(), object);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isNumber()) {
try {
Object value = valueFromNumber(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isBoolean()) {
try {
Object value = valueFromBoolean(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isJsonNull()) {
featureBuilder.set(descriptor.getName(), null);
} else {
throw new Exception("Unsupported JSON type for attribute value (" + entry.getKey() + ")");
}
}
}
}
SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId));
RevFeature revFeature = RevFeatureBuilder.build(feature);
ggit.getRepository().stagingDatabase().put(revFeature);
getResponse().setEntity(new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN));
}
} catch (Exception e) {
throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
} finally {
if (input != null)
Closeables.closeQuietly(input);
}
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class CreateOSMChangesetOp method _call.
/**
* Executes the diff operation.
*
* @return an iterator to a set of differences between the two trees
* @see DiffEntry
*/
@Override
protected Iterator<ChangeContainer> _call() {
Iterator<DiffEntry> nodeIterator = command(DiffOp.class).setFilter(OSMUtils.NODE_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> wayIterator = command(DiffOp.class).setFilter(OSMUtils.WAY_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> iterator = Iterators.concat(nodeIterator, wayIterator);
final EntityConverter converter = new EntityConverter();
Function<DiffEntry, ChangeContainer> function = new Function<DiffEntry, ChangeContainer>() {
@Override
@Nullable
public ChangeContainer apply(@Nullable DiffEntry diff) {
NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(ref.getMetadataId()).call(RevFeatureType.class).get();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
ImmutableList<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
SimpleFeature feature = featureBuilder.buildFeature(ref.name());
Entity entity = converter.toEntity(feature, id);
EntityContainer container;
if (entity instanceof Node) {
container = new NodeContainer((Node) entity);
} else {
container = new WayContainer((Way) entity);
}
ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete;
return new ChangeContainer(container, action);
}
};
return Iterators.transform(iterator, function);
}
Aggregations