Geospatial Routing

折月煮酒 提交于 2019-12-03 12:16:22
RyanDalton

I will have to make at least 4000 calculations an hour so using a mapping solution is probably not acceptable due to volume.

In fact, this is a PERFECT example where a mapping solution would be beneficial. Not your traditional "look at a map and determine distance", but rather "let the database determine what is the closest route to your GPS point.

Since you say you are not opposed to using a different database, you could consider:

  1. SQL Server 2008, which has Spatial Database Engine functions, or
  2. PostgreSQL with the open-source PostGIS (spatial) extension, which has significantly more spatial analysis functions that MS SQL 2008.

Take a look at the PostGIS ST_Distance function or MS SQL Server 2008 STDistance function. This is a good blog entry that describes the benefits of SQL2005 vs SQL2008.

You might also consider reading (or asking more detailed mapping) posts over at gis.stackexchange. That whole group is dedicated to spatial analysis. Some good discussions for you to take a look at would be

Google for "Along-track distance" and you should find the formulas commonly used in aviation. Alternatively, the cross-track distance could also be what you want.

How about the following...

Iterate through all line segments

lineSegSlope = Calculate slope for each line segment

draw a pretend line from the point in question that intersects the current line segment. this is done by inverting the lineSegSlope and multiplying by -1 to get the new slope, then substitute your target point X, Y, and new slope into y-y1 = b * (x-x1). Your X goes into x1, your Y goes into Y1, and your newSlope goes into B.

make an equation for the line segment.

if you draw the two lines on top of each other, they should make an X where each corner is 90 degrees.

calculate the intersection of the two lines

calculate the distance between the intersection point and your new point. If it's greater than some tolerable value, the new point is too far.

this looks like a mess, but hopefully it should work.

Can you take your existing route as a sequence of line segments in 2-D, then take your query point and find the closest point and the closest line segment? The distance to that closest point / line segment would be the distance you care about.

If you're sticking to relatively low latitudes (below 60 degrees) you might be able to treat latitude and longitude as if they were simply flat, as on a Mercator projection.

If not, then you could transform the coordinate system to be relative to a great circle running through some of the route points.

Unless you're dealing with millions of route points, you shouldn't have a problem with CPU time.

A naive approach would be to insert the new GPS point at various places along the route. Start by inserting it before your first point P0, then between your first and second points P0, and P1, and so on until you try inserting it as the last point. Each time you try inserting it at a location, compute the total distance for the circuit and save save the shortest total distance. This can be sped up by precomputing leg distances and storing that sum. Check the distance by subtracting the leg distance for the leg you are inserting the new point, and adding the new distance from the point P(n) to your GPS point to P(n+1). If that shortest total distance is within your tolerance level, the you can accept it.

This is probably not the "best" algorithm (depending on your definition of best) but it's O(n) on the number of points in your route for both time and space complexity. So unless you have an enormous number of points in your route, each check should be quite a bit less than a second, so this should be within your time requirements.

Also, be sure you use haversine distance equations when computing the distance between consecutive points on your route.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!