Search in sources :

Example 21 with Relation

use of net.osmand.osm.edit.Relation 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 22 with Relation

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

the class IndexTransportCreator method mergePlatformsStops.

private void mergePlatformsStops(List<Entity> platformsAndStopsToProcess, List<Entity> platforms, List<Entity> stops, Map<EntityId, Entity> nameReplacement) {
    // walk through platforms  and verify names from the second:
    for (Entity platform : platforms) {
        Entity replaceStop = null;
        LatLon loc = platform.getLatLon();
        if (loc == null) {
            platformsAndStopsToProcess.remove(platform);
            continue;
        }
        double dist = 300;
        Relation rr = stopAreas.get(EntityId.valueOf(platform));
        for (Entity stop : stops) {
            if (stop.getLatLon() == null) {
                continue;
            }
            if (rr != null && stopAreas.get(EntityId.valueOf(stop)) == rr) {
                replaceStop = stop;
            }
            if (MapUtils.getDistance(stop.getLatLon(), loc) < dist) {
                replaceStop = stop;
                dist = MapUtils.getDistance(stop.getLatLon(), loc);
            }
        }
        if (replaceStop != null) {
            platformsAndStopsToProcess.remove(platform);
            if (!Algorithms.isEmpty(platform.getTag(OSMTagKey.NAME))) {
                nameReplacement.put(EntityId.valueOf(replaceStop), platform);
            }
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) LatLon(net.osmand.data.LatLon) Relation(net.osmand.osm.edit.Relation)

Example 23 with Relation

use of net.osmand.osm.edit.Relation 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 24 with Relation

use of net.osmand.osm.edit.Relation 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 25 with Relation

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

the class FixBasemapRoads method process.

private void process(File read, File write, List<File> relationFiles) throws IOException, XMLStreamException, XmlPullParserException, SQLException {
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder("basemap");
    OsmandRegions or = prepareRegions();
    TagsTransformer transformer = new TagsTransformer();
    for (File relFile : relationFiles) {
        LOG.info("Parse relations file " + relFile.getName());
        OsmBaseStorage storage = parseOsmFile(relFile);
        int total = 0;
        for (EntityId e : storage.getRegisteredEntities().keySet()) {
            if (e.getType() == EntityType.RELATION) {
                total++;
                if (total % 1000 == 0) {
                    LOG.info("Processed " + total + " relations");
                }
                Relation es = (Relation) storage.getRegisteredEntities().get(e);
                transformer.handleRelationPropogatedTags(es, renderingTypes, null, EntityConvertApplyType.MAP);
            }
        }
    }
    LOG.info("Parse main file " + read.getName());
    OsmBaseStorage storage = parseOsmFile(read);
    Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
    int total = 0;
    for (EntityId e : entities.keySet()) {
        if (e.getType() == EntityType.WAY) {
            Way es = (Way) storage.getRegisteredEntities().get(e);
            total++;
            if (total % 1000 == 0) {
                LOG.info("Processed " + total + " ways");
            }
            addRegionTag(or, es);
            transformer.addPropogatedTags(es);
            Map<String, String> ntags = renderingTypes.transformTags(es.getModifiableTags(), EntityType.WAY, EntityConvertApplyType.MAP);
            if (es.getModifiableTags() != ntags) {
                es.getModifiableTags().putAll(ntags);
            }
            processWay(es);
        }
    }
    List<EntityId> toWrite = new ArrayList<EntityId>();
    processRegion(toWrite);
    OsmStorageWriter writer = new OsmStorageWriter();
    LOG.info("Writing file... ");
    writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
    LOG.info("DONE");
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TagsTransformer(net.osmand.data.preparation.TagsTransformer) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) OsmandRegions(net.osmand.map.OsmandRegions) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Relation (net.osmand.osm.edit.Relation)30 Way (net.osmand.osm.edit.Way)17 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)14 EntityId (net.osmand.osm.edit.Entity.EntityId)11 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)7 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 LatLon (net.osmand.data.LatLon)3 EntityType (net.osmand.osm.edit.Entity.EntityType)3 FileOutputStream (java.io.FileOutputStream)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2