How to convert from UTM to LatLng in python or Javascript

前端 未结 10 677
余生分开走
余生分开走 2020-11-30 23:00

I have a bunch of files with coordinates in UTM form. For each coordinate I have easting, northing and zone. I need to convert this to LatLng for use with Google Map API to

相关标签:
10条回答
  • 2020-11-30 23:29

    I ended up finding java code from IBM that solved it: http://www.ibm.com/developerworks/java/library/j-coordconvert/index.html

    Just for reference, here is my python implementation of the method I needed:

    import math
    
    def utmToLatLng(zone, easting, northing, northernHemisphere=True):
        if not northernHemisphere:
            northing = 10000000 - northing
    
        a = 6378137
        e = 0.081819191
        e1sq = 0.006739497
        k0 = 0.9996
    
        arc = northing / k0
        mu = arc / (a * (1 - math.pow(e, 2) / 4.0 - 3 * math.pow(e, 4) / 64.0 - 5 * math.pow(e, 6) / 256.0))
    
        ei = (1 - math.pow((1 - e * e), (1 / 2.0))) / (1 + math.pow((1 - e * e), (1 / 2.0)))
    
        ca = 3 * ei / 2 - 27 * math.pow(ei, 3) / 32.0
    
        cb = 21 * math.pow(ei, 2) / 16 - 55 * math.pow(ei, 4) / 32
        cc = 151 * math.pow(ei, 3) / 96
        cd = 1097 * math.pow(ei, 4) / 512
        phi1 = mu + ca * math.sin(2 * mu) + cb * math.sin(4 * mu) + cc * math.sin(6 * mu) + cd * math.sin(8 * mu)
    
        n0 = a / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (1 / 2.0))
    
        r0 = a * (1 - e * e) / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (3 / 2.0))
        fact1 = n0 * math.tan(phi1) / r0
    
        _a1 = 500000 - easting
        dd0 = _a1 / (n0 * k0)
        fact2 = dd0 * dd0 / 2
    
        t0 = math.pow(math.tan(phi1), 2)
        Q0 = e1sq * math.pow(math.cos(phi1), 2)
        fact3 = (5 + 3 * t0 + 10 * Q0 - 4 * Q0 * Q0 - 9 * e1sq) * math.pow(dd0, 4) / 24
    
        fact4 = (61 + 90 * t0 + 298 * Q0 + 45 * t0 * t0 - 252 * e1sq - 3 * Q0 * Q0) * math.pow(dd0, 6) / 720
    
        lof1 = _a1 / (n0 * k0)
        lof2 = (1 + 2 * t0 + Q0) * math.pow(dd0, 3) / 6.0
        lof3 = (5 - 2 * Q0 + 28 * t0 - 3 * math.pow(Q0, 2) + 8 * e1sq + 24 * math.pow(t0, 2)) * math.pow(dd0, 5) / 120
        _a2 = (lof1 - lof2 + lof3) / math.cos(phi1)
        _a3 = _a2 * 180 / math.pi
    
        latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / math.pi
    
        if not northernHemisphere:
            latitude = -latitude
    
        longitude = ((zone > 0) and (6 * zone - 183.0) or 3.0) - _a3
    
        return (latitude, longitude)
    

    And here I thought it was something simple like easting*x+zone*y or something.

    0 讨论(0)
  • 2020-11-30 23:29

    A Javascript version of Staale answer

    function utmToLatLng(zone, easting, northing, northernHemisphere){
            if (!northernHemisphere){
                northing = 10000000 - northing;
            }
    
            var a = 6378137;
            var e = 0.081819191;
            var e1sq = 0.006739497;
            var k0 = 0.9996;
    
            var arc = northing / k0;
            var mu = arc / (a * (1 - Math.pow(e, 2) / 4.0 - 3 * Math.pow(e, 4) / 64.0 - 5 * Math.pow(e, 6) / 256.0));
    
            var ei = (1 - Math.pow((1 - e * e), (1 / 2.0))) / (1 + Math.pow((1 - e * e), (1 / 2.0)));
    
            var ca = 3 * ei / 2 - 27 * Math.pow(ei, 3) / 32.0;
    
            var cb = 21 * Math.pow(ei, 2) / 16 - 55 * Math.pow(ei, 4) / 32;
            var cc = 151 * Math.pow(ei, 3) / 96;
            var cd = 1097 * Math.pow(ei, 4) / 512;
            var phi1 = mu + ca * Math.sin(2 * mu) + cb * Math.sin(4 * mu) + cc * Math.sin(6 * mu) + cd * Math.sin(8 * mu);
    
            var n0 = a / Math.pow((1 - Math.pow((e * Math.sin(phi1)), 2)), (1 / 2.0));
    
            var r0 = a * (1 - e * e) / Math.pow((1 - Math.pow((e * Math.sin(phi1)), 2)), (3 / 2.0));
            var fact1 = n0 * Math.tan(phi1) / r0;
    
            var _a1 = 500000 - easting;
            var dd0 = _a1 / (n0 * k0);
            var fact2 = dd0 * dd0 / 2;
    
            var t0 = Math.pow(Math.tan(phi1), 2);
            var Q0 = e1sq * Math.pow(Math.cos(phi1), 2);
            var fact3 = (5 + 3 * t0 + 10 * Q0 - 4 * Q0 * Q0 - 9 * e1sq) * Math.pow(dd0, 4) / 24;
    
            var fact4 = (61 + 90 * t0 + 298 * Q0 + 45 * t0 * t0 - 252 * e1sq - 3 * Q0 * Q0) * Math.pow(dd0, 6) / 720;
    
            var lof1 = _a1 / (n0 * k0);
            var lof2 = (1 + 2 * t0 + Q0) * Math.pow(dd0, 3) / 6.0;
            var lof3 = (5 - 2 * Q0 + 28 * t0 - 3 * Math.pow(Q0, 2) + 8 * e1sq + 24 * Math.pow(t0, 2)) * Math.pow(dd0, 5) / 120;
            var _a2 = (lof1 - lof2 + lof3) / Math.cos(phi1);
            var _a3 = _a2 * 180 / Math.PI;
    
            var latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / Math.PI;
    
            if (!northernHemisphere){
              latitude = -latitude;
            }
    
            var longitude = ((zone > 0) && (6 * zone - 183.0) || 3.0) - _a3;
    
            var obj = {
                  latitude : latitude,
                  longitude: longitude
            };
    
    
            return obj;
          }
    
    0 讨论(0)
  • 2020-11-30 23:32

    You could use Proj4js, as follows.

    Download Proj4JS from GitHub, using this link.

    The following code will convert from UTM to longitude latitude

    <html>
    <head>
      <script src="proj4.js"></script>
    
      <script>
        var utm = "+proj=utm +zone=32";
        var wgs84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
        console.log(proj4(utm,wgs84,[539884, 4942158]));
      </script>
    </head>
    <body>
    
    </body>
    </html>
    

    In this code, the UTM zone is 32, as should be obvious. The Easting is 539884, and the Northing is 4942158. The result is:

    [9.502832656648073, 44.631671014204365] 
    

    Which is to say 44.631671014204365N, 9.502832656648073E. Which I have verified is correct.

    If you need other projections, you can find their strings here.

    0 讨论(0)
  • 2020-11-30 23:34

    There is a perl module via CPAN called Geography::NationalGrid which can convert easting/northing to lat/longs. That may help.

    Alternatively there are lots of scripts on the movable-type site that let you convert lat/long and easting/northings.

    0 讨论(0)
  • 2020-11-30 23:39

    One problem I had with using proj4js was that it needed the exact zone as @Richard points out. I found a great resource here which can convert WGS to UTM and wrote a cleaner wrapper in JavaScript:

    https://github.com/urbanetic/utm-converter

    0 讨论(0)
  • 2020-11-30 23:41

    According to this page, UTM is supported by proj4js.

    http://trac.osgeo.org/proj4js/wiki/UserGuide#Supportedprojectionclasses

    You may also want to take a look at GDAL. The gdal library has excellent python support, though it may be a bit overkill if you're only doing projection conversion.

    0 讨论(0)
提交回复
热议问题