Search in sources :

Example 1 with Property

use of org.opengis.feature.Property in project GeoGig by boundlessgeo.

the class OSMUnmapOp method unmapNode.

private void unmapNode(SimpleFeature feature, FeatureMapFlusher mapFlusher) {
    boolean modified = false;
    String id = feature.getID();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
    Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + id).call(RevFeature.class);
    Map<String, String> tagsMap = Maps.newHashMap();
    long timestamp = System.currentTimeMillis();
    int version = 1;
    long changeset = -1;
    String user = UNKNOWN_USER;
    Collection<Tag> tags = Lists.newArrayList();
    if (rawFeature.isPresent()) {
        ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
        tags = OSMUtils.buildTagsCollectionFromString(values.get(NODE_TAGS_FIELD_INDEX).get().toString());
        for (Tag tag : tags) {
            tagsMap.put(tag.getKey(), tag.getValue());
        }
        Optional<Object> timestampOpt = values.get(NODE_TIMESTAMP_FIELD_INDEX);
        if (timestampOpt.isPresent()) {
            timestamp = ((Long) timestampOpt.get()).longValue();
        }
        Optional<Object> versionOpt = values.get(NODE_VERSION_FIELD_INDEX);
        if (versionOpt.isPresent()) {
            version = ((Integer) versionOpt.get()).intValue();
        }
        Optional<Object> changesetOpt = values.get(NODE_CHANGESET_FIELD_INDEX);
        if (changesetOpt.isPresent()) {
            changeset = ((Long) changesetOpt.get()).longValue();
        }
        Optional<Object> userOpt = values.get(NODE_USER_FIELD_INDEX);
        if (userOpt.isPresent()) {
            user = (String) userOpt.get();
        }
    }
    Map<String, String> unaliased = Maps.newHashMap();
    Collection<Property> properties = feature.getProperties();
    for (Property property : properties) {
        String name = property.getName().getLocalPart();
        if (name.equals("id") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
            continue;
        }
        Object value = property.getValue();
        if (value != null) {
            String tagName = name;
            if (mapping != null) {
                if (unaliased.containsKey(name)) {
                    tagName = unaliased.get(name);
                } else {
                    tagName = mapping.getTagNameFromAlias(path, tagName);
                    unaliased.put(name, tagName);
                }
            }
            if (!DefaultField.isDefaultField(tagName)) {
                if (tagsMap.containsKey(tagName)) {
                    if (!modified) {
                        String oldValue = tagsMap.get(tagName);
                        modified = !value.equals(oldValue);
                    }
                } else {
                    modified = true;
                }
                tagsMap.put(tagName, value.toString());
            }
        }
    }
    if (!modified && rawFeature.isPresent()) {
        // no changes after unmapping tags, so there's nothing else to do
        return;
    }
    Collection<Tag> newTags = Lists.newArrayList();
    Set<Entry<String, String>> entries = tagsMap.entrySet();
    for (Entry<String, String> entry : entries) {
        newTags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    featureBuilder.set("tags", OSMUtils.buildTagsString(newTags));
    featureBuilder.set("location", feature.getDefaultGeometry());
    featureBuilder.set("changeset", changeset);
    featureBuilder.set("timestamp", timestamp);
    featureBuilder.set("version", version);
    featureBuilder.set("user", user);
    featureBuilder.set("visible", true);
    if (rawFeature.isPresent()) {
        // the feature has changed, so we cannot reuse some attributes.
        // We reconstruct the feature and insert it
        featureBuilder.set("timestamp", System.currentTimeMillis());
        featureBuilder.set("changeset", null);
        featureBuilder.set("version", null);
        featureBuilder.set("visible", true);
        mapFlusher.put("node", featureBuilder.buildFeature(id));
    } else {
        // The feature didn't exist, so we have to add it
        mapFlusher.put("node", featureBuilder.buildFeature(id));
    }
}
Also used : Optional(com.google.common.base.Optional) LineString(com.vividsolutions.jts.geom.LineString) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) ReadOSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) RevFeature(org.locationtech.geogig.api.RevFeature) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Property(org.opengis.feature.Property) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with Property

use of org.opengis.feature.Property in project GeoGig by boundlessgeo.

the class GeogigSimpleFeature method setValue.

@Override
public void setValue(Collection<Property> values) {
    int index = 0;
    for (Property p : values) {
        mutableValues().set(index, Optional.fromNullable(p.getValue()));
        index++;
    }
}
Also used : Property(org.opengis.feature.Property) Point(com.vividsolutions.jts.geom.Point)

Example 3 with Property

use of org.opengis.feature.Property in project GeoGig by boundlessgeo.

the class RevFeatureBuilderTest method testBuildFull.

@Test
public void testBuildFull() throws Exception {
    RevFeature feature = RevFeatureBuilder.build(points1);
    ImmutableList<Optional<Object>> values = feature.getValues();
    assertEquals(values.size(), points1.getProperties().size());
    for (Property prop : points1.getProperties()) {
        assertTrue(values.contains(Optional.fromNullable(prop.getValue())));
    }
    RevFeature feature2 = RevFeatureBuilder.build(lines1);
    values = feature2.getValues();
    assertEquals(values.size(), lines1.getProperties().size());
    for (Property prop : lines1.getProperties()) {
        assertTrue(values.contains(Optional.fromNullable(prop.getValue())));
    }
}
Also used : Optional(com.google.common.base.Optional) RevFeature(org.locationtech.geogig.api.RevFeature) Property(org.opengis.feature.Property) Test(org.junit.Test)

Example 4 with Property

use of org.opengis.feature.Property in project GeoGig by boundlessgeo.

the class RevFeatureBuilder method build.

/**
     * Constructs a new {@link RevFeature} from the provided {@link Feature}.
     * 
     * @param feature the feature to build from
     * @return the newly constructed RevFeature
     */
public static RevFeature build(Feature feature) {
    if (feature == null) {
        throw new IllegalStateException("No feature set");
    }
    Collection<Property> props = feature.getProperties();
    ImmutableList.Builder<Optional<Object>> valuesBuilder = new ImmutableList.Builder<Optional<Object>>();
    for (Property prop : props) {
        valuesBuilder.add(Optional.fromNullable(prop.getValue()));
    }
    return RevFeatureImpl.build(valuesBuilder.build());
}
Also used : Optional(com.google.common.base.Optional) ImmutableList(com.google.common.collect.ImmutableList) Property(org.opengis.feature.Property)

Example 5 with Property

use of org.opengis.feature.Property in project GeoGig by boundlessgeo.

the class OSMUnmapOp method unmapWay.

private void unmapWay(SimpleFeature feature, FeatureMapFlusher flusher) {
    boolean modified = false;
    String id = feature.getID();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.wayType());
    Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.WAY_TYPE_NAME + "/" + id).call(RevFeature.class);
    Map<String, String> tagsMap = Maps.newHashMap();
    long timestamp = System.currentTimeMillis();
    int version = 1;
    long changeset = -1;
    String user = UNKNOWN_USER;
    Collection<Tag> tags = Lists.newArrayList();
    if (rawFeature.isPresent()) {
        ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
        tags = OSMUtils.buildTagsCollectionFromString(values.get(WAY_TAGS_FIELD_INDEX).get().toString());
        for (Tag tag : tags) {
            tagsMap.put(tag.getKey(), tag.getValue());
        }
        Optional<Object> timestampOpt = values.get(WAY_TIMESTAMP_FIELD_INDEX);
        if (timestampOpt.isPresent()) {
            timestamp = ((Long) timestampOpt.get()).longValue();
        }
        Optional<Object> versionOpt = values.get(WAY_VERSION_FIELD_INDEX);
        if (versionOpt.isPresent()) {
            version = ((Integer) versionOpt.get()).intValue();
        }
        Optional<Object> changesetOpt = values.get(WAY_CHANGESET_FIELD_INDEX);
        if (changesetOpt.isPresent()) {
            changeset = ((Long) changesetOpt.get()).longValue();
        }
        Optional<Object> userOpt = values.get(WAY_USER_FIELD_INDEX);
        if (userOpt.isPresent()) {
            user = (String) userOpt.get();
        }
    }
    Map<String, String> unaliased = Maps.newHashMap();
    Collection<Property> properties = feature.getProperties();
    for (Property property : properties) {
        String name = property.getName().getLocalPart();
        if (name.equals("id") || name.equals("nodes") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
            continue;
        }
        Object value = property.getValue();
        if (value != null) {
            String tagName = name;
            if (mapping != null) {
                if (unaliased.containsKey(name)) {
                    tagName = unaliased.get(name);
                } else {
                    tagName = mapping.getTagNameFromAlias(path, tagName);
                    unaliased.put(name, tagName);
                }
            }
            if (!DefaultField.isDefaultField(tagName)) {
                if (tagsMap.containsKey(tagName)) {
                    if (!modified) {
                        String oldValue = tagsMap.get(tagName);
                        modified = !value.equals(oldValue);
                    }
                } else {
                    modified = true;
                }
                tagsMap.put(tagName, value.toString());
            }
        }
    }
    if (!modified && rawFeature.isPresent()) {
        // no changes after unmapping tags, so there's nothing else to do
        return;
    }
    tags.clear();
    Set<Entry<String, String>> entries = tagsMap.entrySet();
    for (Entry<String, String> entry : entries) {
        tags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    Geometry geom = (Geometry) feature.getDefaultGeometry();
    LineString line;
    if (geom instanceof LineString) {
        line = (LineString) geom;
    } else {
        line = gf.createLineString(geom.getCoordinates());
    }
    featureBuilder.set("visible", true);
    featureBuilder.set("tags", OSMUtils.buildTagsString(tags));
    featureBuilder.set("way", line);
    featureBuilder.set("changeset", changeset);
    featureBuilder.set("timestamp", timestamp);
    featureBuilder.set("version", version);
    featureBuilder.set("user", user);
    featureBuilder.set("nodes", getNodeStringFromWay(feature, flusher));
    if (rawFeature.isPresent()) {
        // the feature has changed, so we cannot reuse some attributes
        featureBuilder.set("timestamp", System.currentTimeMillis());
        // temporary negative changeset ID
        featureBuilder.set("changeset", -changeset);
        // featureBuilder.set("version", version);
        flusher.put("way", featureBuilder.buildFeature(id));
    } else {
        flusher.put("way", featureBuilder.buildFeature(id));
    }
}
Also used : Optional(com.google.common.base.Optional) LineString(com.vividsolutions.jts.geom.LineString) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) ReadOSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) LineString(com.vividsolutions.jts.geom.LineString) RevFeature(org.locationtech.geogig.api.RevFeature) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Property(org.opengis.feature.Property) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

Property (org.opengis.feature.Property)7 Optional (com.google.common.base.Optional)6 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)4 Point (com.vividsolutions.jts.geom.Point)3 RevFeature (org.locationtech.geogig.api.RevFeature)3 Function (com.google.common.base.Function)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 LineString (com.vividsolutions.jts.geom.LineString)2 Entry (java.util.Map.Entry)2 Nullable (javax.annotation.Nullable)2 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)2 OSMMappingLogEntry (org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry)2 ReadOSMMappingLogEntry (org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry)2 Feature (org.opengis.feature.Feature)2 GeometryAttribute (org.opengis.feature.GeometryAttribute)2 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)2 ImmutableList (com.google.common.collect.ImmutableList)1 Test (org.junit.Test)1