Search in sources :

Example 11 with Tag

use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project voltdb by VoltDB.

the class VoltDBOsmSink method process.

public void process(WayContainer wayContainer) {
    Way way;
    List<Long> nodeIds;
    way = wayContainer.getEntity();
    nodeIds = new ArrayList<Long>(way.getWayNodes().size());
    for (WayNode wayNode : way.getWayNodes()) {
        nodeIds.add(wayNode.getNodeId());
    }
    // Keep invalid ways out of the database if desired by the user
    if (way.getWayNodes().size() > 1 || keepInvalidWays) {
        for (Tag tag : way.getTags()) {
            try {
                client.callProcedure(new InsertCallback(), INS_WAY_TAGS_PROC, way.getId(), tag.getKey(), tag.getValue());
            } catch (NoConnectionsException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Add these to the ways_nodes_table;
        int sequence = 0;
        for (Long nodeId : nodeIds) {
            try {
                client.callProcedure(new InsertCallback(), INS_WAYS_NODES_PROC, way.getId(), nodeId, sequence);
            } catch (NoConnectionsException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sequence++;
        }
        StringBuffer sb = new StringBuffer();
        // if the first node id == the last nodeId, we know that this is a
        // closed loop.
        long n0 = nodeIds.get(0);
        long nn = nodeIds.get(nodeIds.size() - 1);
        if (n0 == nn) {
            if (enableBboxBuilder) {
                Polygon pg = wayGeometryBuilder.createPolygon(way);
                pg.outerWKT(sb);
            }
        } else {
            // it's a lineString, but we don't support it yet.
            if (enableLinestringBuilder) {
                LineString lineString = wayGeometryBuilder.createWayLinestring(way);
                lineString.outerWKT(sb);
            } else {
                return;
            }
        }
        String bbox = sb.toString();
        try {
            client.callProcedure(new InsertCallback(), INS_WAYS_PROC, way.getId(), way.getVersion(), way.getUser().getId(), way.getTimestamp(), way.getChangesetId(), bbox);
        } catch (NoConnectionsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) IOException(java.io.IOException) LineString(org.postgis.LineString) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) NoConnectionsException(org.voltdb.client.NoConnectionsException) LineString(org.postgis.LineString) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Polygon(org.postgis.Polygon)

Example 12 with Tag

use of org.openstreetmap.osmosis.core.domain.v0_6.Tag 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)

Example 13 with Tag

use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.

the class MappingRule method apply.

/**
     * Returns the feature resulting from transforming a given feature using this rule. This method
     * takes a collection of tags, so there is no need to compute them from the 'tags' attribute.
     * This is meant as a faster alternative to the apply(Feature) method, in case the mapping
     * object calling this has already computed the tags, to avoid recomputing them
     * 
     * @param feature
     * @param tags
     * @return
     */
public Optional<Feature> apply(Feature feature, Collection<Tag> tags) {
    if (!canBeApplied(feature, tags)) {
        return Optional.absent();
    }
    for (AttributeDescriptor attribute : getFeatureType().getAttributeDescriptors()) {
        String attrName = attribute.getName().toString();
        Class<?> clazz = attribute.getType().getBinding();
        if (Geometry.class.isAssignableFrom(clazz)) {
            Geometry geom = prepareGeometry((Geometry) feature.getDefaultGeometryProperty().getValue());
            if (geom == null) {
                return Optional.absent();
            }
            featureBuilder.set(attrName, geom);
        } else {
            Object value = null;
            for (Tag tag : tags) {
                if (fields.containsKey(tag.getKey())) {
                    if (fields.get(tag.getKey()).getName().equals(attrName)) {
                        FieldType type = FieldType.forBinding(clazz);
                        value = getAttributeValue(tag.getValue(), type);
                        break;
                    }
                }
            }
            featureBuilder.set(attribute.getName(), value);
        }
    }
    String id = feature.getIdentifier().getID();
    featureBuilder.set("id", id);
    if (defaultFields != null) {
        for (DefaultField df : defaultFields) {
            featureBuilder.set(df.name(), feature.getProperty(df.name()).getValue());
        }
    }
    if (!featureType.getGeometryDescriptor().getType().getBinding().equals(Point.class)) {
        featureBuilder.set("nodes", feature.getProperty("nodes").getValue());
    }
    return Optional.of((Feature) featureBuilder.buildFeature(id));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Point(com.vividsolutions.jts.geom.Point) FieldType(org.locationtech.geogig.storage.FieldType)

Example 14 with Tag

use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.

the class EntityConverter method toEntity.

/**
     * Converts a Feature to a OSM Entity
     * 
     * @param feature the feature to convert
     * @param replaceId. The changesetId to use in case the feature has a negative one indicating a
     *        temporary value
     * @return
     */
public Entity toEntity(SimpleFeature feature, Long changesetId) {
    Entity entity;
    SimpleFeatureType type = feature.getFeatureType();
    long id = Long.parseLong(feature.getID());
    int version = ((Integer) feature.getAttribute("version")).intValue();
    Long changeset = (Long) feature.getAttribute("changeset");
    if (changesetId != null && changeset < 0) {
        changeset = changesetId;
    }
    Long milis = (Long) feature.getAttribute("timestamp");
    Date timestamp = new Date(milis);
    String user = (String) feature.getAttribute("user");
    String[] userTokens = user.split(":");
    OsmUser osmuser;
    try {
        osmuser = new OsmUser(Integer.parseInt(userTokens[1]), userTokens[0]);
    } catch (Exception e) {
        osmuser = OsmUser.NONE;
    }
    String tagsString = (String) feature.getAttribute("tags");
    Collection<Tag> tags = OSMUtils.buildTagsCollectionFromString(tagsString);
    CommonEntityData entityData = new CommonEntityData(id, version, timestamp, osmuser, changeset, tags);
    if (type.equals(OSMUtils.nodeType())) {
        Point pt = (Point) feature.getDefaultGeometryProperty().getValue();
        entity = new Node(entityData, pt.getY(), pt.getX());
    } else {
        List<WayNode> nodes = Lists.newArrayList();
        String nodesString = (String) feature.getAttribute("nodes");
        for (String s : nodesString.split(";")) {
            nodes.add(new WayNode(Long.parseLong(s)));
        }
        entity = new Way(entityData, nodes);
    }
    return entity;
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) CommonEntityData(org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) Date(java.util.Date) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Example 15 with Tag

use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.

the class OSMUtils method buildTagsString.

/**
     * @param collection
     * @return
     */
@Nullable
public static String buildTagsString(Collection<Tag> collection) {
    if (collection.isEmpty()) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (Iterator<Tag> it = collection.iterator(); it.hasNext(); ) {
        Tag e = it.next();
        String key = e.getKey();
        if (key == null || key.isEmpty()) {
            continue;
        }
        String value = e.getValue();
        sb.append(key).append(':').append(value);
        if (it.hasNext()) {
            sb.append('|');
        }
    }
    return sb.toString();
}
Also used : Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Nullable(javax.annotation.Nullable)

Aggregations

Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)10 IOException (java.io.IOException)5 Point (com.vividsolutions.jts.geom.Point)4 Event (io.requery.test.model3.Event)4 Tag (io.requery.test.model3.Tag)4 Test (org.junit.Test)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 UUID (java.util.UUID)3 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)3 NoConnectionsException (org.voltdb.client.NoConnectionsException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Optional (com.google.common.base.Optional)2 LineString (com.vividsolutions.jts.geom.LineString)2 EntityMapper (io.requery.jackson.EntityMapper)2 StringWriter (java.io.StringWriter)2 Entry (java.util.Map.Entry)2 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)2 OSMMappingLogEntry (org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry)2