Reputation: 619
I have an application that uses the DotSpatial framework to connect to a usb GPS on my laptop. I essentially use it when driving out on site to know where I am.
The application also includes a ShapeFile map as background so that I know where I am.
What I have is:
What I need to do is the following:
If there are any other suggestions please let me know.
P.S the application needs to be portable so dont want to mess around with SQL Server or PostgrSQL with posGIS etc.
Upvotes: 1
Views: 1567
Reputation: 17964
You need the element in your table where the distance (pythagoras) to your point is closest.
select top 1 * from mytable
order by ( (table.lon - lon) * (table.lon - lon) + (table.lat - lat) * (table.lat - lat) )
Lon&Lat: position you're at. Skipping the sqrt will speed things up quite a bit too.
Upvotes: 0
Reputation: 7982
Since you want to get the nearest distance in metres from your current lat/long, the first thing you need to do is perform a coordinate transform.
Most GPS receivers work in WGS84, and assuming this, you can use a library such as Proj4 (.NET wrapper) to convert both your current position and the road position to metres (projected coordinates; either into WGS84 projected, or a local coordinate system based on your geographic location. UTM zones can work quite well, or OSGB if you're based in the UK).
You can find a projection best suited for your needs here (however, as I write this, it's currently down... hence my lack of helpful links at this time. I will edit when it's accessible again).
If you need more help with the GIS side of this, consider paying a visit to GIS.SE
Once you have your locations in metres, get the distance between them:
double distanceX = currentXmetres - roadXmetres;
double distanceY = currentYmetres - roadYmetres;
double Distance = Math.Abs( Math.Sqrt( (distanceX * distanceX) + (distanceY * distanceY) ) );
Now you can query your database with the value of Distance and find the closest location:
SELECT * FROM table
ORDER BY ABS(Distance - Col 2)
LIMIT 1
Note: I don't know the actual name of your table, or columns, so replace "table" and "Col 2" accordingly.
Upvotes: 3