How to convert from UTM to LatLng in python or Javascript

前端 未结 10 762
余生分开走
余生分开走 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:47

    I'm new to this as well and have been studying up on the subject recently.

    Here's a method I found using the python gdal pacakge (the osr package is included in gdal). The gdal package is pretty powerful, but the documentation could be better.

    This is derived from a discussion here: 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) # (, )
        return utm_to_wgs84_transform.TransformPoint(easting, northing, 0) # returns lon, lat, altitude
    

    And here's the method for converting from a lat, lon in wgs84 (what most gps units report) to 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) # (, )
        return wgs84_to_utm_transform.TransformPoint(lon, lat, 0) # returns easting, northing, altitude    
    

    I also found that if you've already got django/gdal installed and you know the EPSG code for the UTM zone you're working on, you can just use the Point() transform() method.

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

提交回复
热议问题