Currently, the formula I'm using to convert MGRS to Lat/Long and vice versa is giving me slightly different results than most other conversion utilities. It's usually between 10 to 30 feet off. This isn't a very big difference, but it matters for the particular application of data. I'm wondering if anyone knows of a simple library that does conversions between MGRS and Lat/Long or UTM.
[GIS] PHP MGRS Conversion Formula/Library
convertcoordinate systemPHPweb-mapping
Related Solutions
Personally I'd use the Python bindings for the Proj4 library - http://pypi.python.org/pypi/pyproj/1.8.6
Further details at http://pyproj.googlecode.com/svn/trunk/README.html
You can pass your projection parameters directly to the projection object as follows:
PROJ_32756 = """
+proj=utm +zone=56 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs
"""
p1 = pyproj.Proj(PROJ_32756)
A full list of these strings can be found at http://spatialreference.org/ For zone 56 see http://spatialreference.org/ref/epsg/32756/proj4/
Many of these projections are already built into the library so you can just pass in the code. Below is an example of reprojecting some geojson from one projection to another. You should be able to adapt to your needs.
import pyproj
import simplejson
p1 = pyproj.Proj(init='epsg:3857')
p2 =pyproj.Proj(init='epsg:29902')
json='{"type":"Polygon","coordinates":[[[-972279.15280781,6907135.0149664],[-995516.00940234,6867999.2564914],[-946596.31130859,6844762.3998969],[-964941.19809375,6892459.1055383],[-972279.15280781,6907135.0149664]]]}'
pydata = simplejson.loads(json)
print str(pydata)
new_coords = []
for p in pydata['coordinates'][0]:
x2, y2 = pyproj.transform(p1,p2,p[0],p[1])
new_coords.append([x2, y2])
new_json = simplejson.dumps(dict(type="Polygon", coordinates=[new_coords], srid="29902"))
print str(new_json)
Yes, you will get these kinds of errors with a global Mercator projection: it is accurate at the equator and the distortion increases exponentially with latitude away from the equator. The distance distortion is exactly 2 (100%) at 60 degrees latitude. At your test latitudes (64.14 degrees) I compute a distortion of 2.294, exactly agreeing with the ratio 904/394 = 2.294. (Earlier I computed 2.301 but that was based on a sphere, not the WGS84 ellipsoid. The difference (of 0.3%) gives us a sense of the accuracy you might gain from using an ellipsoid-based projection versus the sphere-based Haversine formula.)
There is no such thing as a global projection that yields highly accurate distances everywhere. That's one reason the UTM zone system is used!
One solution is to use spherical geometry for all your calculations, but you have rejected that (which is reasonable if you're going to be doing complex operations, but the decision might be worth revisiting).
Another solution is to adapt the projection to the points being compared. For example, you could safely use a transverse Mercator (as in the UTM system) with a meridian lying near the center of the region of interest. Moving the meridian is a simple thing to do: just subtract the meridian's longitude from all the longitudes and use a single TM projection centered at the Prime Meridian (with a scale factor of 1, rather than the 0.9996 of the UTM system). For your work this will tend to be more accurate than using UTM itself. It will give correct angles (TM is conformal) and will be remarkably accurate for points separated by only a few tens of kilometers: expect better than six-digit accuracy. In fact, I would be inclined to attribute any small differences between these adapted-TM distances and the Haversine distances to the difference between the ellipsoid (used for the TM projection) and the sphere (used by Haversine), rather than to distortion in the projection.
Best Answer
http://freegeographytools.com/2007/mgrsutmlatitude-longitude-converter