How to get the number of intersections on all local cycle routes?

We are working on a full revamping of the signage of our cycle network in Drammen, Norway. To start the project, we would like to get a quick overview of the current network and how many potential intersections we will need to put up new road signs on. So, all roads with relation: network=lcn as described in this wiki page (Norwegian only) . A local road example that should be included: Way: ‪Muusøya‬ (‪42122816‬) | OpenStreetMap

I found this Overpass-Turbo example, that find all intersections but would like to edit it to filter out only the roads and cycleways that has the local cycle route relation. I have searched and tried hacking it together myself, but with no luck. Can anyone here please help?

Also, whith cycleway included in the the script, it still does not return this node: Node: 4511681893 | OpenStreetMap. Why?

[out:json][timeout:25][bbox:{{bbox}}];

// The public street network
way["highway"~"^(trunk|primary|secondary|tertiary|unclassified|residential|cycleway)$"]->.streets;

// Get nodes that connect between three or more street segments
node(way_link.streets:3-)->.connections;

// Get intersections between distinct streets
foreach .connections->.connection(
  // Get adjacent streets
  way(bn.connection);
  // If the names don't all match, add the node to the set of intersections
  if (u(t["name"]) == "< multiple values found >") {
    (.connection; .intersections;)->.intersections;
  }
);

.intersections out geom;
2 Likes

If you know Python, you could use GeoDesk for this task: Download an extract of your region from BBBike (choose format “PBF”) and create a GOL file, which you can then query. I’ll post an example script if you’re interested.

I’d be interested in knowing how you’d do this in GeoDesk.

1 Like

Here’s how you can find route intersections using GeoDesk and display them on a map.

First, you need to do a little setup:

  1. Install Python and a Java (if your system doesn’t have them already)

  2. Install the GeoDesk Python module: pip install geodesk

  3. Download and install the GOL Tool

  4. Download the OSM data for your region (list of extract services)

  5. Turn the .osm.pbf file into a GOL): gol build drammen extract (Where extract is the name of the OSM data file)

Now we can write the actual script:

from geodesk import *

drammen = Features("drammen")

This gives us access to all the features in drammen.gol.

Next, we’ll define two subsets: cycling routes, and streets (any highway, except footpaths and minor ways like alleys and driveways).

routes = drammen("r[route=bicycle]")
streets = drammen("w[highway][highway != path,footway,pedestrian,service]")

We’re going to mark each point where a cycling route intersects with another street. First, we’ll create a Map object:

map = Map("cycle-routes")

We’ll process each route with a simple for-loop, then traverse each of its member ways and mark them in blue.

for route in routes:
    print(f"{route} {route.name} ({route.ref}):")
    count = 0
    for member in route.members.ways:
        map.add(member, color="blue", opacity=0.3, weight=20)

Inside this loop, we’ll traverse the nodes of each route segment and check their parent ways (excluding ways that are part of the route itself). If the node belongs to any street other than one on the route, we’ll mark it in red.

        for node in member.nodes:
            has_intersection = False
            for way in streets.parents_of(node):
                if not route in way.parents:
                    print(f"  - Intersects with {way.highway} {way.name}")
                    has_intersection = True
            if has_intersection:
                map.add(node, color="red")
                count += 1

For each route, we’ll print the total number of intersections:

    print(f"  {count} intersections.")

And finally, we’ll display our map in a browser:

map.show()

The output will look like this:

relation/6723229 Løkkeberget (S15):
  - Intersects with tertiary Engene
  - Intersects with tertiary Engene
  - Intersects with unclassified Rådhusgata
  - Intersects with unclassified Thornegata
  - Intersects with residential Cappelens gate
  ...
  28 intersections.

Here’s the full script:

cycleways.py
from geodesk import *

drammen = Features("c:\\geodesk\\tests\\drammen")
routes = drammen("r[route=bicycle]")
streets = drammen("w[highway][highway != path,footway,pedestrian,service]")

map = Map("cycle-routes")
for route in routes:
    name = route.name if route.name else "Unnamed route"
    print(f"{route} {name} ({route.ref}):")
    count = 0
    for member in route.members.ways:
        map.add(member, color="blue" opacity=0.3, weight=20)
        for node in member.nodes:
            has_intersection = False
            for way in streets.parents_of(node):
                if not route in way.parents:
                    name = way.name if way.name else "without name"
                    print(f"  - Intersects with {way.highway} {name}")
                    has_intersection = True
            if has_intersection:
                map.add(node, color="red")
                count += 1
    print(f"  {count} intersections.")
map.show()

You can run it with

python cycleways.py

(If we wanted to get fancy, we could analyze the intersections further to determine if they are “sign-worthy” – if the route follows a main road, and a dirt track veers off to the right, there’s no need to post signage. Likewise, if the route goes straight through an intersection of two residential roads, it probably does not need a sign, either. When I get a chance, I’ll flesh this out into a full-fledged example.)

4 Likes