Search in sources :

Example 86 with SimpleFeature

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());
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Test(org.junit.Test)

Example 87 with SimpleFeature

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());
}
Also used : Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 88 with SimpleFeature

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());
}
Also used : ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) FeatureSource(org.geotools.data.FeatureSource) QueryCapabilities(org.geotools.data.QueryCapabilities) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 89 with SimpleFeature

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);
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) NodeRef(org.locationtech.geogig.api.NodeRef) StringRepresentation(org.restlet.resource.StringRepresentation) RestletException(org.locationtech.geogig.rest.RestletException) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) JsonParser(com.google.gson.JsonParser) InputStreamReader(java.io.InputStreamReader) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevObject(org.locationtech.geogig.api.RevObject) InputStream(java.io.InputStream) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RestletException(org.locationtech.geogig.rest.RestletException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) JsonElement(com.google.gson.JsonElement) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) JsonObject(com.google.gson.JsonObject) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 90 with SimpleFeature

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);
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) ChangeAction(org.openstreetmap.osmosis.core.task.common.ChangeAction) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Function(com.google.common.base.Function) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) ChangeContainer(org.openstreetmap.osmosis.core.container.v0_6.ChangeContainer) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) SimpleFeature(org.opengis.feature.simple.SimpleFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

SimpleFeature (org.opengis.feature.simple.SimpleFeature)147 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)73 Test (org.junit.Test)45 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)35 IOException (java.io.IOException)34 ArrayList (java.util.ArrayList)26 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)25 Geometry (com.vividsolutions.jts.geom.Geometry)23 Coordinate (com.vividsolutions.jts.geom.Coordinate)22 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 File (java.io.File)19 HashMap (java.util.HashMap)18 Query (org.geotools.data.Query)18 RevFeature (org.locationtech.geogig.api.RevFeature)17 DefaultTransaction (org.geotools.data.DefaultTransaction)16 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)16 Optional (com.google.common.base.Optional)15 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)15 Filter (org.opengis.filter.Filter)15 Transaction (org.geotools.data.Transaction)14