Java low level API routing: Avoid sharp turns

I just started using the Graphhopper Core Java library. I am trying to create a graph using individual route segments from my database and finding the best possible routes between two points. Hereby I am focussing on train transport. I used this documentation for creating and querying my graph:

Now because I am routing for trains, I want to avoid impossible routes such as this example:

I think I can accomplish this using a TurnWeighting, is this a right assumption? If so, how do I initialize such Weighting, do I need to iterate over all junctions and assign a TurnInfo for all edges that start or end in that node?

I realize that there is a possibility that I am making it to hard, or looking in the wrong direction. Any guidance will be appreciated.

This is one possibility yes. You can have a look into GraphHopperStorageWithTurnCostsTest where this is done at the level you need.

Another possibility is to calculate the infinite weight per request (slower but could be easier for you to get started). For that you need to overload the createWeighting method of GraphHopper and return infinity if it is a sharp turn. For that you need two edges, the current you get directly in calcWeight → EdgeIteratorState and the second you get via the edgeId int parameter:

graph.getEdgeIteratorState(edgeId, currentEdgeIteratorState.getBaseNode())

Thank you for your reply. I wil try the first approach because speed will be of en essence. How can I easily find all junctions? At first sight I cannot find a function that does this for me, do I need to iterate all nodes? If so, how exactly, I can only find a function that gets a Node based on his ID. Do I need to save all the ID’s myself for further processing? Surely there is an easier way.

The node IDs are adjacent and start at 0 and go to getGraphHopperStorage().getNodes(). And then

ex = graph.createEdgeExplorer;
edgeIter = ex.setBaseNode(nodeId)
while(edgeIter.hasNext()) {
edgeIter.fetchWayGeometry
}