Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free geo data class library in C++ (Lat / Lng / distance / conversions)

I am looking for a free geo data class library. I need to convert different formats of Latitude / Longitude values (e.g as here), get distances between lat/lng values, coordinates in range, etc.

Google shows me several single methods / functions in C++, this is not the issue. Examples

  1. Calculating the distance between 2 latitudes and longitudes that are saved in a text file?
  2. Lat Long: http://www.codecogs.com/code/maths/geometry/spherical/latlong.php

What I am looking for is a more complete / sophisticated class library with some documentation and examples. Any hints?

-- Edit --

LatLong is such a library in Javascript: http://www.movable-type.co.uk/scripts/latlong.html
Will also check (C++): http://geographiclib.sourceforge.net/html/annotated.html

-- Edit 2 --

The transformation approach below is a good starter, however, if there are other great libs please let me know. Thanks to all who have contributed.

like image 660
Horst Walter Avatar asked Jul 12 '12 19:07

Horst Walter


People also ask

How do you find the distance between two latitude and longitude in C++?

159): dlon = lon2 - lon1 dlat = lat2 - lat1 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a), sqrt(1-a)) d = R * c will give mathematically and computationally exact results. The intermediate result c is the great circle distance in radians.


1 Answers

proj4 is what you're looking for. It's a serious mapping tool, mature and well respected projection api. It's predominantly popular for doing forward and backward conversion between geographical coordinates (lat/lng) and planar (x,y). Coordinate geometry with planar coordinates is much simpler than with geographical.

Documentation is not the best. Because it can also be used as a stand-alone program, I'd start off reading the man page to get an introduction. The command-line switches are the same parameters used to initialise the structure when using the api. Then read the api docs - the function calls listed under Basic API functions may very well be all you need.

Edit with what started off as a comment to OP: Projecting to planar coordinates is attractive for many reasons: Coordinate comparisons are then in linear units rather than angular. Distance becomes a trivial hypotenuse calculation. Axis-aligned rectangles are intuitive to work with. Elementary right-angled trigonometry can be used to calculate new points, e.g. midway, offset, etc. This is all provided the extents of the region is not massive, e.g. of continental scale - If your dataset is within say a degree square (that's still large) you'll be fine with this approach.

like image 70
acraig5075 Avatar answered Sep 20 '22 21:09

acraig5075