As per the attached document I'm trying to define a rail network by providing geocodes for each rail stop, how would I define a 'stop' as a junction for a branch in the network without having to define each branch option multiple times. For example, if I need to travel from Brisbane to Longreach in one trip and in the next trip Brisbane - Mackay, I don't want to have to define each stop from Brisbane - Rockhamptom for each of these branches. I want Google to pickup in the definition of my geocodes that Rockhampton is the stop which 'forks' into one or more branches, it will then continue to display the polylines of the branch which is closest to the destination.

In summary, in the example I've defined my destination station may be on one of two lines, I don't want to have to define the geocodes for those lines separately especially when they share more than half of the same geocodes up to a certain point, how is this best achieved?

Thanks in advance.

  • 1
    Would it be possible to post a link to an image/diagram? I'm thinking you want to simplify a line definition from B --> x --> y --> R --> (L|M) by collapsing the intermediate nodes (i.e. branch) to B --> R --> (L|M)?
    – om_henners
    Aug 29, 2012 at 1:03
  • @Kurt- Can you provide the link the to document you said was "attached"? Aug 29, 2012 at 12:45
  • Hi Ryan and Om, here is the document link queenslandrail.com.au/NetworkServices/Documents/…, apologies for the late reply I haven't been in the country for the last two weeks. thanks, Kurt
    – Kurt
    Sep 14, 2012 at 5:51

1 Answer 1


I'm making a couple of assumptions here. First, you're only encoding the points using the Google Earth geocoder, and you can do that already. Second, aside from the PDF you've supplied us you have some sort of data that contains all the names of the stations and their links further down the line. Thirdly I'm guessing that you want to be able to start and end at any station, but you're only interested in the end points and where you would have to change trains.

One way of getting this info would be to use the networkX library in Python, which has some very helpful functions, such as Graph.degree_iter and lots of shortest path algorithms. For example:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()

G.add_edges_from([["Brisbane", "x"],
                  ["x", "y"],
                  ["y", "Rockhampton"],
                  ["Rockhampton", "Longreach"],
                  ["Rockhampton", "Mackay"]])

def get_nodes(start, finish):
    out_n = []
    skipped_n = []
    for n, d in G.degree_iter(p):
        if n in [start, finish] or d != 2:

    pos = nx.spring_layout(G)
    nx.draw_networkx_edges(G, pos)
    nx.draw_networkx_nodes(G, pos, out_n)
    nx.draw_networkx_nodes(G, pos, skipped_n, node_size=150, node_color='b')

    return out_n

print get_nodes("Brisbane", "Mackay")
print get_nodes("x", "Longreach")
  • Hi Om,This is very interesting, your assumptions are pretty correct. I do have my train lines and stations/stops/nodes listed. Does this code make the assumption that the nodes are related or random when determining the shortest path? I need to be able to relate the stops/nodes as they need to follow a specific 'stop' sequence on the line, at a certain point I need to change lines at a specific 'stop' at which point I fork into a different line of related 'stops'.
    – Kurt
    Sep 22, 2012 at 18:00
  • Hi @Kurt, Yeah, this code implicitly implies that the points are related in order - basically add_edges_from links nodes as from --> to based on their ids (any hashable, i.e. "Brisbane"). If you check the docs then you can see you can also add costs to a route, so that a direct link fro "Brisbane" --> "Macaky" could be longer than a linked set of nodes should you so wish. The code currently assumes that any time you have more than one train from a location (ie, degree > 2), but you can change this also.
    – om_henners
    Sep 23, 2012 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.