Search in sources :

Example 1 with Way

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

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {

            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
            List<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, null);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            return container;
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) 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) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with Way

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

the class WayPolygonGeometryBuilder method createRing.

public LinearRing createRing(Way way) {
    List<Point> points = new ArrayList<Point>();
    for (WayNode wayNode : way.getWayNodes()) {
        NodeLocation nodeLocation;
        double longitude;
        double latitude;
        nodeLocation = locationStore.getNodeLocation(wayNode.getNodeId());
        longitude = nodeLocation.getLongitude();
        latitude = nodeLocation.getLatitude();
        if (nodeLocation.isValid()) {
            Point point = new Point(longitude, latitude);
            points.add(point);
        }
    }
    return new LinearRing(points.toArray(new Point[0]));
}
Also used : WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) NodeLocation(org.openstreetmap.osmosis.pgsimple.common.NodeLocation) ArrayList(java.util.ArrayList) Point(org.postgis.Point) LinearRing(org.postgis.LinearRing)

Example 3 with Way

use of org.openstreetmap.osmosis.core.domain.v0_6.Way 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 4 with Way

use of org.openstreetmap.osmosis.core.domain.v0_6.Way 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 5 with Way

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

Way (org.openstreetmap.osmosis.core.domain.v0_6.Way)5 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)4 Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)4 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)4 Optional (com.google.common.base.Optional)3 RevFeature (org.locationtech.geogig.api.RevFeature)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)3 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)3 Entity (org.openstreetmap.osmosis.core.domain.v0_6.Entity)3 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)3 Function (com.google.common.base.Function)2 Point (com.vividsolutions.jts.geom.Point)2 Nullable (javax.annotation.Nullable)2 NodeRef (org.locationtech.geogig.api.NodeRef)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)2 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)2 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)2 EntityContainer (org.openstreetmap.osmosis.core.container.v0_6.EntityContainer)2 NodeContainer (org.openstreetmap.osmosis.core.container.v0_6.NodeContainer)2