如何在python或Javascript中从UTM转换为LatLng

Sta*_*ale 25 javascript python gis proj4js arcgis-js-api

我有一堆坐标为UTM格式的文​​件.对于每个坐标,我有东,北和区.我需要将其转换为LatLng,以便与Google Map API一起使用,以便在地图中显示信息.

我找到了一些这样做的在线计算器,但没有实际的代码或库.http://trac.osgeo.org/proj4js/是Javascript的投影库,但是看一下它不包括UTM投影的演示.

我对整个GIS领域仍然很新鲜,所以我想要的是ala:

(lat,lng) = transform(easting, northing, zone)
Run Code Online (Sandbox Code Playgroud)

Sta*_*ale 35

我最终找到了解决它的IBM代码:http://www.ibm.com/developerworks/java/library/j-coordconvert/index.html

仅供参考,这是我需要的方法的python实现:

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)
Run Code Online (Sandbox Code Playgroud)

在这里,我认为这是一个简单的easting*x+zone*y东西.

  • 我不怪你认为这很简单.我上周从Lat/Lon转到Easting/Northing进行Miller Projection,我仍然在内心庆祝. (4认同)

ken*_*der 10

我找到的是以下网站:http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html 它有一个javascript转换器,你应该检查那里的算法.从页面:

程序员:本文档中的JavaScript源代码可以不受限制地复制和重用.


小智 8

根据这个页面,proj4js支持UTM.

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

您可能还想看看GDAL.gdal库具有出色的python支持,但如果你只进行投影转换可能有点过分.


san*_*ino 7

Staale 答案的 Javascript 版本

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;
      }
Run Code Online (Sandbox Code Playgroud)


小智 7

有一个 Python 库(utm)“Python 的双向 UTM-WGS84 转换器”,可以管理单个坐标点或系列。

将 (纬度, 经度) 元组转换为 UTM 坐标:

>>> utm.from_latlon(51.2, 7.5)
Run Code Online (Sandbox Code Playgroud)

结果

(395201.3103811303, 5673135.241182375, 32, 'U')
Run Code Online (Sandbox Code Playgroud)

返回的形式为(EASTING, NORTHING, ZONE_NUMBER, ZONE_LETTER).

如果使用系列,结果 EASTING 和 NORTHING 将具有相同的形状。

>>> utm.from_latlon(np.array([51.2, 49.0]), np.array([7.5, 8.4]))
Run Code Online (Sandbox Code Playgroud)

结果

(array([395201.31038113, 456114.59586214]), array([5673135.24118237, 5427629.20426126]), 32, 'U')
Run Code Online (Sandbox Code Playgroud)

相反,将 UTM 坐标转换为(纬度,经度)元组:

>>> utm.to_latlon(340000, 5710000, 32, 'U')
Run Code Online (Sandbox Code Playgroud)
(51.51852098408468, 6.693872395145327)
Run Code Online (Sandbox Code Playgroud)

与语法utm.to_latlon(EASTING, NORTHING, ZONE_NUMBER, ZONE_LETTER). 如果要使用系列,则在这种情况下也是如此。根据文档,传递一系列函数比对函数进行多个单点调用更快。


mon*_*kut 6

我也是新手,并且最近一直在研究这个问题.

这是我使用python gdal pacakge找到的方法(osr包包含在gdal中).gdal包非常强大,但文档可能更好.

这源于此处的讨论:http: //www.mail-archive.com/gdal-dev@lists.osgeo.org/msg12398.html

import osr

def transform_utm_to_wgs84(easting, northing, zone):
    utm_coordinate_system = osr.SpatialReference()
    utm_coordinate_system.SetWellKnownGeogCS("WGS84") # Set geographic coordinate system to handle lat/lon
    is_northern = northing > 0    
    utm_coordinate_system.SetUTM(zone, is_northern)

    wgs84_coordinate_system = utm_coordinate_system.CloneGeogCS() # Clone ONLY the geographic coordinate system 

    # create transform component
    utm_to_wgs84_transform = osr.CoordinateTransformation(utm_coordinate_system, wgs84_coordinate_system) # (<from>, <to>)
    return utm_to_wgs84_transform.TransformPoint(easting, northing, 0) # returns lon, lat, altitude
Run Code Online (Sandbox Code Playgroud)

这里是从wgs84中的lat,lon(大多数gps单位报告)转换为utm的方法:

def transform_wgs84_to_utm(lon, lat):    
    def get_utm_zone(longitude):
        return (int(1+(longitude+180.0)/6.0))

    def is_northern(latitude):
        """
        Determines if given latitude is a northern for UTM
        """
        if (latitude < 0.0):
            return 0
        else:
            return 1

    utm_coordinate_system = osr.SpatialReference()
    utm_coordinate_system.SetWellKnownGeogCS("WGS84") # Set geographic coordinate system to handle lat/lon  
    utm_coordinate_system.SetUTM(get_utm_zone(lon), is_northern(lat))

    wgs84_coordinate_system = utm_coordinate_system.CloneGeogCS() # Clone ONLY the geographic coordinate system 

    # create transform component
    wgs84_to_utm_transform = osr.CoordinateTransformation(wgs84_coordinate_system, utm_coordinate_system) # (<from>, <to>)
    return wgs84_to_utm_transform.TransformPoint(lon, lat, 0) # returns easting, northing, altitude    
Run Code Online (Sandbox Code Playgroud)

我还发现如果你已经安装了django/gdal并且你知道你正在使用的UTM区域的EPSG代码,你可以使用Point() transform()方法.

from django.contrib.gis.geos import Point
utm2epsg = {"54N": 3185, ...}
p = Point(lon, lat, srid=4326) # 4326 = WGS84 epsg code
p.transform(utm2epsg["54N"])
Run Code Online (Sandbox Code Playgroud)