Search in sources :

Example 11 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class IndexRouteCreator method indexHighwayRestrictions.

private void indexHighwayRestrictions(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    if (e instanceof Relation && "restriction".equals(e.getTag(OSMTagKey.TYPE))) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String val = e.getTag("restriction");
        if (val != null) {
            if ("no_u_turn".equalsIgnoreCase(val) && Algorithms.objectEquals(e.getTag("from"), e.getTag("to"))) {
                // don't index such roads - can't go through issue https://www.openstreetmap.org/way/338099991#map=17/46.86699/-0.20473
                return;
            }
            byte type = -1;
            if ("no_right_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_RIGHT_TURN;
            } else if ("no_left_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_LEFT_TURN;
            } else if ("no_u_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_U_TURN;
            } else if ("no_straight_on".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_STRAIGHT_ON;
            } else if ("only_right_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_RIGHT_TURN;
            } else if ("only_left_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_LEFT_TURN;
            } else if ("only_straight_on".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_STRAIGHT_ON;
            }
            if (type != -1) {
                ctx.loadEntityRelation((Relation) e);
                // $NON-NLS-1$
                Collection<RelationMember> fromL = ((Relation) e).getMembers("from");
                // $NON-NLS-1$
                Collection<RelationMember> toL = ((Relation) e).getMembers("to");
                if (!fromL.isEmpty() && !toL.isEmpty()) {
                    RelationMember from = fromL.iterator().next();
                    RelationMember to = toL.iterator().next();
                    if (from.getEntityId().getType() == EntityType.WAY) {
                        if (!highwayRestrictions.containsKey(from.getEntityId().getId())) {
                            highwayRestrictions.put(from.getEntityId().getId(), new TLongArrayList());
                        }
                        highwayRestrictions.get(from.getEntityId().getId()).add((to.getEntityId().getId() << 3) | (long) type);
                    }
                }
            }
        }
    }
}
Also used : Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) TLongArrayList(gnu.trove.list.array.TLongArrayList)

Example 12 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class IndexTransportCreator method indexRelations.

public void indexRelations(Relation e, OsmDbAccessorContext ctx) throws SQLException {
    if (e.getTag(OSMTagKey.ROUTE_MASTER) != null) {
        ctx.loadEntityRelation(e);
        for (RelationMember child : ((Relation) e).getMembers()) {
            Entity entity = child.getEntity();
            if (entity != null) {
                masterRoutes.put(entity.getId(), (Relation) e);
            }
        }
    }
    if ("stop_area".equals(e.getTag(OSMTagKey.PUBLIC_TRANSPORT))) {
        // save stop area relation members for future processing
        String name = e.getTag(OSMTagKey.NAME);
        if (name == null)
            return;
        ctx.loadEntityRelation(e);
        for (RelationMember entry : e.getMembers()) {
            String role = entry.getRole();
            if ("platform".equals(role) || "stop".equals(role)) {
                if (entry.getEntity() != null && entry.getEntity().getTag(OSMTagKey.NAME) == null) {
                    stopAreas.put(entry.getEntityId(), e);
                }
            }
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember)

Example 13 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class IndexTransportCreator method processTransportRelationV2.

private boolean processTransportRelationV2(Relation rel, TransportRoute route) {
    // first, verify we can accept this relation as new transport relation
    // accepted roles restricted to: <empty>, stop, platform, ^(stop|platform)_(entry|exit)_only$
    String version = rel.getTag("public_transport:version");
    try {
        if (Algorithms.isEmpty(version) || Integer.parseInt(version) < 2) {
            for (RelationMember entry : rel.getMembers()) {
                // ignore ways (cause with even with new relations there could be a mix of forward/backward ways)
                if (entry.getEntity() instanceof Way) {
                    continue;
                }
                String role = entry.getRole();
                if (role.isEmpty() || "stop".equals(role) || "platform".equals(role)) {
                    // accepted roles
                    continue;
                }
                stopPlatformMatcher.reset(role);
                if (stopPlatformMatcher.matches()) {
                    continue;
                }
                // there is wrong role in the relation, exit
                return false;
            }
        }
    } catch (NumberFormatException e) {
        return false;
    }
    List<Entity> platformsAndStops = new ArrayList<Entity>();
    List<Entity> platforms = new ArrayList<Entity>();
    List<Entity> stops = new ArrayList<Entity>();
    Map<EntityId, Entity> platformNames = new LinkedHashMap<>();
    for (RelationMember entry : rel.getMembers()) {
        String role = entry.getRole();
        if (entry.getEntity() == null || entry.getEntity().getLatLon() == null) {
            continue;
        }
        if (role.startsWith("platform")) {
            platformsAndStops.add(entry.getEntity());
            platforms.add(entry.getEntity());
        } else if (role.startsWith("stop")) {
            platformsAndStops.add(entry.getEntity());
            stops.add(entry.getEntity());
        } else {
            if (entry.getEntity() instanceof Way) {
                route.addWay((Way) entry.getEntity());
            }
        }
    }
    mergePlatformsStops(platformsAndStops, platforms, stops, platformNames);
    if (platformsAndStops.isEmpty()) {
        // nothing to get from this relation - there is no stop
        return true;
    }
    for (Entity s : platformsAndStops) {
        TransportStop stop = EntityParser.parseTransportStop(s);
        Relation stopArea = stopAreas.get(EntityId.valueOf(s));
        // verify name tag, not stop.getName because it may contain unnecessary refs, etc
        Entity genericStopName = null;
        if (stopArea != null && !Algorithms.isEmpty(stopArea.getTag(OSMTagKey.NAME))) {
            genericStopName = stopArea;
        } else if (platformNames.containsKey(EntityId.valueOf(s))) {
            genericStopName = platformNames.get(EntityId.valueOf(s));
        }
        if (genericStopName != null) {
            stop.copyNames(genericStopName.getTag(OSMTagKey.NAME), genericStopName.getTag(OSMTagKey.NAME_EN), genericStopName.getNameTags(), true);
        }
        route.getForwardStops().add(stop);
    }
    return true;
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Entity(net.osmand.osm.edit.Entity) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) ArrayList(java.util.ArrayList) TransportStop(net.osmand.data.TransportStop) Way(net.osmand.osm.edit.Way) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class IndexTransportCreator method processTransportRelationV1.

private boolean processTransportRelationV1(Relation rel, TransportRoute directRoute, TransportRoute backwardRoute) {
    final Map<TransportStop, Integer> forwardStops = new LinkedHashMap<TransportStop, Integer>();
    final Map<TransportStop, Integer> backwardStops = new LinkedHashMap<TransportStop, Integer>();
    int currentStop = 0;
    int forwardStop = 0;
    int backwardStop = 0;
    for (RelationMember e : rel.getMembers()) {
        if (e.getRole().contains("stop") || e.getRole().contains("platform")) {
            // $NON-NLS-1$
            if (e.getEntity() instanceof Node) {
                TransportStop stop = EntityParser.parseTransportStop(e.getEntity());
                Relation stopArea = stopAreas.get(EntityId.valueOf(e.getEntity()));
                if (stopArea != null) {
                    stop.copyNames(stopArea.getTag(OSMTagKey.NAME), stopArea.getTag(OSMTagKey.NAME_EN), stopArea.getNameTags(), true);
                }
                // $NON-NLS-1$
                boolean forward = e.getRole().contains("forward");
                // $NON-NLS-1$
                boolean backward = e.getRole().contains("backward");
                currentStop++;
                if (forward || !backward) {
                    forwardStop++;
                }
                if (backward) {
                    backwardStop++;
                }
                boolean common = !forward && !backward;
                int index = -1;
                int i = e.getRole().length() - 1;
                int accum = 1;
                while (i >= 0 && Character.isDigit(e.getRole().charAt(i))) {
                    if (index < 0) {
                        index = 0;
                    }
                    index = accum * Character.getNumericValue(e.getRole().charAt(i)) + index;
                    accum *= 10;
                    i--;
                }
                if (index < 0) {
                    index = forward ? forwardStop : (backward ? backwardStop : currentStop);
                }
                if (forward || common) {
                    forwardStops.put(stop, index);
                    directRoute.getForwardStops().add(stop);
                }
                if (backward || common) {
                    if (common) {
                        // put with negative index
                        backwardStops.put(stop, -index);
                    } else {
                        backwardStops.put(stop, index);
                    }
                    backwardRoute.getForwardStops().add(stop);
                }
            }
        } else if (e.getEntity() instanceof Way) {
            int dir = e.getRole().equals("backward") ? -1 : (e.getRole().equals("forward") ? 1 : 0);
            if (dir >= 0) {
                directRoute.addWay((Way) e.getEntity());
            }
            if (dir <= 0) {
                backwardRoute.addWay((Way) e.getEntity());
            }
        }
    }
    if (forwardStops.isEmpty() && backwardStops.isEmpty()) {
        return false;
    }
    Collections.sort(directRoute.getForwardStops(), new Comparator<TransportStop>() {

        @Override
        public int compare(TransportStop o1, TransportStop o2) {
            return forwardStops.get(o1) - forwardStops.get(o2);
        }
    });
    // all common stops are with negative index (reeval them)
    for (TransportStop s : new ArrayList<TransportStop>(backwardStops.keySet())) {
        if (backwardStops.get(s) < 0) {
            backwardStops.put(s, backwardStops.size() + backwardStops.get(s) - 1);
        }
    }
    Collections.sort(backwardRoute.getForwardStops(), new Comparator<TransportStop>() {

        @Override
        public int compare(TransportStop o1, TransportStop o2) {
            return backwardStops.get(o1) - backwardStops.get(o2);
        }
    });
    return true;
}
Also used : Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) Way(net.osmand.osm.edit.Way) LinkedHashMap(java.util.LinkedHashMap) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) TransportStop(net.osmand.data.TransportStop)

Example 15 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class OsmStorageWriter method saveStorage.

public void saveStorage(OutputStream output, OsmBaseStorage storage, Collection<EntityId> interestedObjects, boolean includeLinks) throws XMLStreamException, IOException {
    Map<EntityId, Entity> entities = storage.getRegisteredEntities();
    Map<EntityId, EntityInfo> entityInfo = storage.getRegisteredEntityInfo();
    Set<Node> nodes = new LinkedHashSet<Node>();
    Set<Way> ways = new LinkedHashSet<Way>();
    Set<Relation> relations = new LinkedHashSet<Relation>();
    if (interestedObjects == null) {
        interestedObjects = entities.keySet();
    }
    Stack<EntityId> toResolve = new Stack<EntityId>();
    toResolve.addAll(interestedObjects);
    while (!toResolve.isEmpty()) {
        EntityId l = toResolve.pop();
        if (entities.get(l) instanceof Node) {
            nodes.add((Node) entities.get(l));
        } else if (entities.get(l) instanceof Way) {
            ways.add((Way) entities.get(l));
            if (includeLinks) {
                toResolve.addAll(((Way) entities.get(l)).getEntityIds());
            }
        } else if (entities.get(l) instanceof Relation) {
            relations.add((Relation) entities.get(l));
            if (includeLinks) {
                for (RelationMember rm : ((Relation) entities.get(l)).getMembers()) {
                    toResolve.add(rm.getEntityId());
                }
            }
        }
    }
    writeOSM(output, entityInfo, nodes, ways, relations);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entity(net.osmand.osm.edit.Entity) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) Stack(java.util.Stack) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) EntityInfo(net.osmand.osm.edit.EntityInfo)

Aggregations

Relation (net.osmand.osm.edit.Relation)16 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Way (net.osmand.osm.edit.Way)11 Node (net.osmand.osm.edit.Node)9 Entity (net.osmand.osm.edit.Entity)7 EntityId (net.osmand.osm.edit.Entity.EntityId)7 LinkedHashMap (java.util.LinkedHashMap)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 MultipolygonBuilder (net.osmand.data.MultipolygonBuilder)2 TransportStop (net.osmand.data.TransportStop)2 MapRulType (net.osmand.osm.MapRenderingTypes.MapRulType)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1