# How to resolve case when routing result does not contain start and end point?

I have routing case shown on picture, SP=start point, EP=end point.

A-----SP-----B------------C----------D--------E-----EP------F

Routing result is BC, CD and DE. My points are not on route. I think that line direction is the key of problem. If line AB has direction AB than my SP will be on route and source will be in point A, but if line AB has direction BA than my start point is not on route and source is in point B.

How to determine line direction to draw by myself segment SP-B?

-

## 1 Answer

I'm going throught similiar efforts. Here's my idea:

1. User wants to create a route.

2. User clicks on start point and then user clicks on end point.

3. Check to see if start point is a node. If it is, use it for routing. If it's not, see if it's a edge. If it is, use linear referencing functions to create a virtual node and use the nearest node (from your virtual node) as input for the routing function.

4. If it's not a node and not an edge, select the nearest edge and from that nearest edge select the nearest node. Use that node for routing. Keep that other results to be inserted and the beggining of the routing function.

5. Check to see if end point is a node. If it is, use it for routing. If it's not a node, see if intersects and edge. If the clicked point intersects an edge, use linear referencing functions to create a virtual node and use the nearest node (from end virtual node) as input for routing function.

6. If it's not a node and not an edge, select the nearest edge and from that nearest edge select the nearest node. Use that node for routing. Keep that other results to be inserted and the end of the routing function.

If you created your virtual nodes in memory, you can tweak the result for routing. If the user clicked outside the network, you will need to add a routing leg in the start or end.

Once you find the nearest edge and the node you will start routing, it's easy. Trace a line from the clicked point to the nearest edge, and trace a line from that intersection point (new virtual edge) to the start routing point.

Do the other way around for end nodes.

-
Well, I have some demands about routing ... User clicks the start point on the map, I am searching the nearest network segment and then I find the closest point on the network segment. I return lon/lat of that point and put pushpin on the map. From the network segment I take source. The same story for the end point, only I take target from the network segment. But I don't know direction of the segment and that makes problem. I have network table with fields source and target, and I use them for routing function. – Route Maker Mar 29 '12 at 18:01
I did it with line interpolation, I locate SP with ST_Line_Locate_Point and then using ST_Line_Substring I am taking only SP-B part of the segment. – Route Maker Apr 2 '12 at 11:14
Exactly. Substring the segment that the user "touched" between the nearest point from clicked point and nearest node. You don't need direction. – George Apr 2 '12 at 18:05