Search in sources :

Example 1 with OSMWay

use of org.opentripplanner.openstreetmap.model.OSMWay in project OpenTripPlanner by opentripplanner.

the class OpenStreetMapParser method processDocument.

private void processDocument(Document doc, OpenStreetMapContentHandler map, int phase) {
    Node osm = doc.getFirstChild();
    Node node = osm.getFirstChild();
    while (node != null) {
        if (!(node instanceof Element)) {
            node = node.getNextSibling();
            continue;
        }
        Element element = (Element) node;
        if (phase == 3 && element.getTagName().equals("node")) {
            OSMNode osmNode = new OSMNode();
            osmNode.setId(Long.parseLong(element.getAttribute("id")));
            osmNode.lat = Double.parseDouble(element.getAttribute("lat"));
            osmNode.lon = Double.parseDouble(element.getAttribute("lon"));
            processTags(osmNode, element);
            map.addNode(osmNode);
        } else if (phase == 2 && element.getTagName().equals("way")) {
            OSMWay osmWay = new OSMWay();
            osmWay.setId(Long.parseLong(element.getAttribute("id")));
            processTags(osmWay, element);
            Node node2 = element.getFirstChild();
            while (node2 != null) {
                if (!(node2 instanceof Element)) {
                    node2 = node2.getNextSibling();
                    continue;
                }
                Element element2 = (Element) node2;
                if (element2.getNodeName().equals("nd")) {
                    OSMNodeRef nodeRef = new OSMNodeRef();
                    nodeRef.setRef(Long.parseLong(element2.getAttribute("ref")));
                    osmWay.addNodeRef(nodeRef);
                }
                node2 = node2.getNextSibling();
            }
            map.addWay(osmWay);
        } else if (phase == 1 && element.getTagName().equals("relation")) {
            OSMRelation osmRelation = new OSMRelation();
            osmRelation.setId(Long.parseLong(element.getAttribute("id")));
            processTags(osmRelation, element);
            Node node2 = element.getFirstChild();
            while (node2 != null) {
                if (!(node2 instanceof Element)) {
                    node2 = node2.getNextSibling();
                    continue;
                }
                Element element2 = (Element) node2;
                if (element2.getNodeName().equals("member")) {
                    OSMRelationMember member = new OSMRelationMember();
                    member.setRole(element2.getAttribute("role"));
                    member.setType(element2.getAttribute("type"));
                    member.setRef(Long.parseLong(element2.getAttribute("ref")));
                    osmRelation.addMember(member);
                }
                node2 = node2.getNextSibling();
            }
            map.addRelation(osmRelation);
        }
        node = node.getNextSibling();
    }
}
Also used : OSMWay(org.opentripplanner.openstreetmap.model.OSMWay) OSMRelation(org.opentripplanner.openstreetmap.model.OSMRelation) OSMRelationMember(org.opentripplanner.openstreetmap.model.OSMRelationMember) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode) OSMNodeRef(org.opentripplanner.openstreetmap.model.OSMNodeRef)

Example 2 with OSMWay

use of org.opentripplanner.openstreetmap.model.OSMWay in project OpenTripPlanner by opentripplanner.

the class Area method constructRingsRecursive.

private boolean constructRingsRecursive(ArrayListMultimap<Long, OSMWay> waysByEndpoint, List<Long> ring, List<List<Long>> closedRings, long endpoint) {
    List<OSMWay> ways = new ArrayList<OSMWay>(waysByEndpoint.get(endpoint));
    for (OSMWay way : ways) {
        // remove this way from the map
        List<Long> nodeRefs = way.getNodeRefs();
        long firstEndpoint = nodeRefs.get(0);
        long otherEndpoint = nodeRefs.get(nodeRefs.size() - 1);
        waysByEndpoint.remove(firstEndpoint, way);
        waysByEndpoint.remove(otherEndpoint, way);
        ArrayList<Long> newRing = new ArrayList<Long>(ring.size() + nodeRefs.size());
        long newFirstEndpoint;
        if (firstEndpoint == endpoint) {
            for (int j = nodeRefs.size() - 1; j >= 1; --j) {
                newRing.add(nodeRefs.get(j));
            }
            newRing.addAll(ring);
            newFirstEndpoint = otherEndpoint;
        } else {
            newRing.addAll(nodeRefs.subList(0, nodeRefs.size() - 1));
            newRing.addAll(ring);
            newFirstEndpoint = firstEndpoint;
        }
        if (newRing.get(newRing.size() - 1).equals(newRing.get(0))) {
            // ring closure
            closedRings.add(newRing);
            // if we're out of endpoints, then we have succeeded
            if (waysByEndpoint.size() == 0) {
                // success
                return true;
            }
            // otherwise, we need to start a new partial ring
            newRing = new ArrayList<Long>();
            OSMWay firstWay = null;
            for (Long entry : waysByEndpoint.keySet()) {
                List<OSMWay> list = waysByEndpoint.get(entry);
                firstWay = list.get(0);
                nodeRefs = firstWay.getNodeRefs();
                newRing.addAll(nodeRefs);
                firstEndpoint = nodeRefs.get(0);
                otherEndpoint = nodeRefs.get(nodeRefs.size() - 1);
                break;
            }
            waysByEndpoint.remove(firstEndpoint, firstWay);
            waysByEndpoint.remove(otherEndpoint, firstWay);
            if (constructRingsRecursive(waysByEndpoint, newRing, closedRings, firstEndpoint)) {
                return true;
            }
            waysByEndpoint.remove(firstEndpoint, firstWay);
            waysByEndpoint.remove(otherEndpoint, firstWay);
        } else {
            // continue with this ring
            if (waysByEndpoint.get(newFirstEndpoint) != null) {
                if (constructRingsRecursive(waysByEndpoint, newRing, closedRings, newFirstEndpoint)) {
                    return true;
                }
            }
        }
        if (firstEndpoint == endpoint) {
            waysByEndpoint.put(otherEndpoint, way);
        } else {
            waysByEndpoint.put(firstEndpoint, way);
        }
    }
    return false;
}
Also used : OSMWay(org.opentripplanner.openstreetmap.model.OSMWay) ArrayList(java.util.ArrayList)

Example 3 with OSMWay

use of org.opentripplanner.openstreetmap.model.OSMWay in project OpenTripPlanner by opentripplanner.

the class Area method constructRings.

public List<List<Long>> constructRings(List<OSMWay> ways) {
    if (ways.size() == 0) {
        // no rings is no rings
        return Collections.emptyList();
    }
    List<List<Long>> closedRings = new ArrayList<List<Long>>();
    ArrayListMultimap<Long, OSMWay> waysByEndpoint = ArrayListMultimap.create();
    for (OSMWay way : ways) {
        List<Long> refs = way.getNodeRefs();
        long start = refs.get(0);
        long end = refs.get(refs.size() - 1);
        if (start == end) {
            ArrayList<Long> ring = new ArrayList<Long>(refs);
            closedRings.add(ring);
        } else {
            waysByEndpoint.put(start, way);
            waysByEndpoint.put(end, way);
        }
    }
    // precheck for impossible situations
    List<Long> toRemove = new ArrayList<Long>();
    for (Long endpoint : waysByEndpoint.keySet()) {
        Collection<OSMWay> list = waysByEndpoint.get(endpoint);
        if (list.size() % 2 == 1) {
            return null;
        }
    }
    for (Long key : toRemove) {
        waysByEndpoint.removeAll(key);
    }
    List<Long> partialRing = new ArrayList<Long>();
    if (waysByEndpoint.size() == 0) {
        return closedRings;
    }
    long firstEndpoint = 0, otherEndpoint = 0;
    OSMWay firstWay = null;
    for (Long endpoint : waysByEndpoint.keySet()) {
        List<OSMWay> list = waysByEndpoint.get(endpoint);
        firstWay = list.get(0);
        List<Long> nodeRefs = firstWay.getNodeRefs();
        partialRing.addAll(nodeRefs);
        firstEndpoint = nodeRefs.get(0);
        otherEndpoint = nodeRefs.get(nodeRefs.size() - 1);
        break;
    }
    waysByEndpoint.get(firstEndpoint).remove(firstWay);
    waysByEndpoint.get(otherEndpoint).remove(firstWay);
    if (constructRingsRecursive(waysByEndpoint, partialRing, closedRings, firstEndpoint)) {
        return closedRings;
    } else {
        return null;
    }
}
Also used : OSMWay(org.opentripplanner.openstreetmap.model.OSMWay) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with OSMWay

use of org.opentripplanner.openstreetmap.model.OSMWay in project OpenTripPlanner by opentripplanner.

the class OpenStreetMapParserTest method testParser.

public void testParser(OSMMap map) throws Exception {
    Map<Long, OSMNode> nodes = map.getNodes();
    assertEquals(7197, nodes.size());
    OSMNode nodeA = map.getNodeForId(27308461);
    assertEquals(27308461, nodeA.getId());
    assertEquals(52.3887673, nodeA.lat, 0.0000001);
    assertEquals(16.8506243, nodeA.lon, 0.0000001);
    Map<String, String> tags = nodeA.getTags();
    assertEquals("JOSM", tags.get("created_by"));
    assertEquals("survey", tags.get("source"));
    OSMNode nodeB = map.getNodeForId(27308457);
    assertEquals(27308457, nodeB.getId());
    assertEquals(52.3850672, nodeB.lat, 0.0000001);
    assertEquals(16.8396962, nodeB.lon, 0.0000001);
    tags = nodeB.getTags();
    assertEquals("Wieruszowska", tags.get("name"));
    assertEquals("tram_stop", tags.get("railway"));
    assertEquals("survey", tags.get("source"));
    assertEquals("1", tags.get("layer"));
    OSMNode nodeC = map.getNodeForId(299769943);
    assertTrue(nodeC.hasTag("name"));
    assertNull(nodeC.getTag("not-existing-tag"));
    assertEquals("Apteka Junikowska", nodeC.getTag("name"));
    assertTrue(nodeC.isTagTrue("dispensing"));
    assertFalse(nodeC.isTagFalse("dispensing"));
    assertFalse(nodeC.isTagTrue("not-existing-tag"));
    assertFalse(nodeC.isTagFalse("not-existing-tag"));
    OSMNode nodeD = map.getNodeForId(338912397);
    assertTrue(nodeD.isTagFalse("dispensing"));
    assertFalse(nodeD.isTagTrue("dispensing"));
    Map<Long, OSMWay> ways = map.getWays();
    assertEquals(1511, ways.size());
    OSMWay wayA = map.getWayForId(13490353);
    assertEquals(13490353, wayA.getId());
    List<Long> nodeRefsA = wayA.getNodeRefs();
    assertEquals(2, nodeRefsA.size());
    assertEquals(123978834, nodeRefsA.get(0).longValue());
    assertEquals(123980465, nodeRefsA.get(1).longValue());
    tags = wayA.getTags();
    assertEquals("Potlatch 0.9a", tags.get("created_by"));
    assertEquals("secondary", tags.get("highway"));
}
Also used : OSMWay(org.opentripplanner.openstreetmap.model.OSMWay) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode)

Example 5 with OSMWay

use of org.opentripplanner.openstreetmap.model.OSMWay in project OpenTripPlanner by opentripplanner.

the class TestOpenStreetMapGraphBuilder method testCreativeNaming.

@Test
public void testCreativeNaming() {
    OSMWithTags way = new OSMWay();
    way.addTag("highway", "footway");
    way.addTag("cycleway", "lane");
    way.addTag("access", "no");
    CreativeNamer namer = new CreativeNamer();
    namer.setCreativeNamePattern("Highway with cycleway {cycleway} and access {access} and morx {morx}");
    assertEquals("Highway with cycleway lane and access no and morx ", namer.generateCreativeName(way).toString());
}
Also used : OSMWay(org.opentripplanner.openstreetmap.model.OSMWay) OSMWithTags(org.opentripplanner.openstreetmap.model.OSMWithTags) Test(org.junit.Test)

Aggregations

OSMWay (org.opentripplanner.openstreetmap.model.OSMWay)6 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 OSMNode (org.opentripplanner.openstreetmap.model.OSMNode)2 OSMWithTags (org.opentripplanner.openstreetmap.model.OSMWithTags)2 List (java.util.List)1 OSMNodeRef (org.opentripplanner.openstreetmap.model.OSMNodeRef)1 OSMRelation (org.opentripplanner.openstreetmap.model.OSMRelation)1 OSMRelationMember (org.opentripplanner.openstreetmap.model.OSMRelationMember)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1