从纬度,经度转换为x,y

问题描述 投票:0回答:6

我想将 GPS 位置(纬度、经度)转换为 x、y 坐标。 我找到了很多关于这个主题的链接并应用了它,但它没有给我正确的答案!

我正在按照以下步骤来测试答案: (1) 首先,我取两个位置并使用地图计算它们之间的距离。 (2) 然后将这两个位置转换成x,y坐标。 (3) 然后再次计算x,y坐标中两点之间的距离 看看它是否在第(1)点给我相同的结果。

我找到了以下解决方案之一,但它没有给我正确的答案!

latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

// adjust position by radians
latitude -= 1.570795765134; // subtract 90 degrees (in radians)

// and switch z and y 
xPos = (app.radius) * Math.sin(latitude) * Math.cos(longitude);
zPos = (app.radius) * Math.sin(latitude) * Math.sin(longitude);
yPos = (app.radius) * Math.cos(latitude);

我也试过这个link但仍然不能很好地与我一起工作!

任何帮助如何从(纬度,经度)转换为(x,y)?

谢谢,

gps mapping geometry
6个回答
74
投票

不存在精确解

没有从球体到平面的等轴测图。当您将球体的纬度/经度坐标转换为平面中的 x/y 坐标时,您不能希望此操作会保留所有长度。你必须接受某种变形。确实存在许多不同的地图投影,它们可以在长度、角度和面积的保留之间实现不同的折衷。对于地球表面的较小部分,横向墨卡托 是很常见的。您可能听说过UTM。但是还有更多.

您引用的公式计算 x/y/z,即 3D 空间中的一个点。但即使在那里,您也不会自动获得正确的距离。球体表面两点之间的最短距离将穿过该球体,而地球上的距离主要是沿着该表面的测地线长度。所以它们会更长。

小区域的近似值

如果你想画的地球表面的部分比较小,那么你可以使用一个非常简单的近似。您可以简单地使用水平轴 x 表示经度 λ,垂直轴 y 表示纬度 φ。不过,它们之间的比例不应为 1:1。相反,您应该使用 cos(φ0) 作为纵横比,其中 φ0 表示靠近地图中心的纬度。此外,要将角度(以弧度为单位)转换为长度,您需要乘以地球的半径(在此模型中假定为球体)。

  • x = r λ cos(φ0)
  • y = r φ

这很简单等角投影。在大多数情况下,您只能计算一次 cos(φ0),这使得后续大量点的计算非常便宜。


17
投票

我想和你分享一下我是如何解决这个问题的。正如@MvG 所说,我使用了等距柱状投影,但是这种方法为您提供了与地球(或整个地图)相关的 X 和 Y 位置,这意味着您获得了全球位置。在我的例子中,我想在一个小区域(大约 500 平方米)转换坐标,所以我将投影点与另外 2 个点相关联,获取全局位置并与局部(屏幕上)位置相关,就像这样:

首先,我在要投影的区域周围选择2个点(左上角和右下角),如下图:

一旦我有了 lat 和 lng 的全球参考区域,我就会对屏幕位置做同样的事情。包含此数据的对象如下所示。

//top-left reference point
var p0 = {
    scrX: 23.69,        // Minimum X position on screen
    scrY: -0.5,         // Minimum Y position on screen
    lat: -22.814895,    // Latitude
    lng: -47.072892     // Longitude
}

//bottom-right reference point
var p1 = {
    scrX: 276,          // Maximum X position on screen
    scrY: 178.9,        // Maximum Y position on screen
    lat: -22.816419,    // Latitude
    lng: -47.070563     // Longitude
}

var radius = 6371;      //Earth Radius in Km

//## Now I can calculate the global X and Y for each reference point ##\\

// This function converts lat and lng coordinates to GLOBAL X and Y positions
function latlngToGlobalXY(lat, lng){
    //Calculates x based on cos of average of the latitudes
    let x = radius*lng*Math.cos((p0.lat + p1.lat)/2);
    //Calculates y based on latitude
    let y = radius*lat;
    return {x: x, y: y}
}

// Calculate global X and Y for top-left reference point
p0.pos = latlngToGlobalXY(p0.lat, p0.lng);
// Calculate global X and Y for bottom-right reference point
p1.pos = latlngToGlobalXY(p1.lat, p1.lng);

/*
* This gives me the X and Y in relation to map for the 2 reference points.
* Now we have the global AND screen areas and then we can relate both for the projection point.
*/

// This function converts lat and lng coordinates to SCREEN X and Y positions
function latlngToScreenXY(lat, lng){
    //Calculate global X and Y for projection point
    let pos = latlngToGlobalXY(lat, lng);
    //Calculate the percentage of Global X position in relation to total global width
    pos.perX = ((pos.x-p0.pos.x)/(p1.pos.x - p0.pos.x));
    //Calculate the percentage of Global Y position in relation to total global height
    pos.perY = ((pos.y-p0.pos.y)/(p1.pos.y - p0.pos.y));

    //Returns the screen position based on reference points
    return {
        x: p0.scrX + (p1.scrX - p0.scrX)*pos.perX,
        y: p0.scrY + (p1.scrY - p0.scrY)*pos.perY
    }
}

//# The usage is like this #\\

var pos = latlngToScreenXY(-22.815319, -47.071718);
$point = $("#point-to-project");
$point.css("left", pos.x+"em");
$point.css("top", pos.y+"em");

如你所见,我是用 javascript 做的,但计算可以翻译成任何语言。

附言我将转换后的位置应用于 id 为“point-to-project”的 HTML 元素。要在您的项目中使用这段代码,您应创建此元素(样式为绝对位置)或更改“用法”块。


4
投票

由于当我搜索同样的问题时这个页面显示在谷歌之上,我想提供一个更实用的答案。 MVG 的回答是正确的,但相当理论化。

我用 javascript 为 fitbit ionic 制作了一个轨迹绘图应用程序。下面的代码是我如何解决这个问题的。

//LOCATION PROVIDER
index.js
var gpsFix = false;
var circumferenceAtLat = 0;
function locationSuccess(pos){
  if(!gpsFix){
    gpsFix = true;
    circumferenceAtLat = Math.cos(pos.coords.latitude*0.01745329251)*111305;
  }
  pos.x:Math.round(pos.coords.longitude*circumferenceAtLat),
  pos.y:Math.round(pos.coords.latitude*110919), 
  plotTrack(pos);
}

绘图.js

plotTrack(position){

let x = Math.round((this.segments[i].start.x - this.bounds.minX)*this.scale);
let y = Math.round(this.bounds.maxY - this.segments[i].start.y)*this.scale; //heights needs to be inverted

//redraw?
let redraw = false;

//x or y bounds?
 if(position.x>this.bounds.maxX){
   this.bounds.maxX = (position.x-this.bounds.minX)*1.1+this.bounds.minX; //increase by 10%
   redraw = true;
 }
 if(position.x<this.bounds.minX){
   this.bounds.minX = this.bounds.maxX-(this.bounds.maxX-position.x)*1.1;
    redraw = true;
 };
 if(position.y>this.bounds.maxY){
   this.bounds.maxY = (position.y-this.bounds.minY)*1.1+this.bounds.minY; //increase by 10%
    redraw = true;
 }
 if(position.y<this.bounds.minY){
   this.bounds.minY = this.bounds.maxY-(this.bounds.maxY-position.y)*1.1;
    redraw = true;
 }
 if(redraw){
   reDraw();
 }
}


function reDraw(){

let xScale = device.screen.width / (this.bounds.maxX-this.bounds.minX);
let yScale = device.screen.height / (this.bounds.maxY-this.bounds.minY); 
if(xScale<yScale) this.scale = xScale; 
else this.scale = yScale;

//Loop trough your object to redraw all of them
}

3
投票

为了完整起见,我喜欢添加我对 @allexrm 代码的 python 改编,效果非常好。再次感谢!

radius = 6371    #Earth Radius in KM

class referencePoint:
    def __init__(self, scrX, scrY, lat, lng):
        self.scrX = scrX
        self.scrY = scrY
        self.lat = lat
        self.lng = lng


# Calculate global X and Y for top-left reference point        
p0 = referencePoint(0, 0, 52.526470, 13.403215)
# Calculate global X and Y for bottom-right reference point
p1 = referencePoint(2244, 2060, 52.525035, 13.405809) 


# This function converts lat and lng coordinates to GLOBAL X and Y positions
def latlngToGlobalXY(lat, lng):
    # Calculates x based on cos of average of the latitudes
    x = radius*lng*math.cos((p0.lat + p1.lat)/2)
    # Calculates y based on latitude
    y = radius*lat
    return {'x': x, 'y': y}


# This function converts lat and lng coordinates to SCREEN X and Y positions
def latlngToScreenXY(lat, lng):
    # Calculate global X and Y for projection point
    pos = latlngToGlobalXY(lat, lng)
    # Calculate the percentage of Global X position in relation to total global width
    perX = ((pos['x']-p0.pos['x'])/(p1.pos['x'] - p0.pos['x']))
    # Calculate the percentage of Global Y position in relation to total global height
    perY = ((pos['y']-p0.pos['y'])/(p1.pos['y'] - p0.pos['y']))

    # Returns the screen position based on reference points
    return {
        'x': p0.scrX + (p1.scrX - p0.scrX)*perX,
        'y': p0.scrY + (p1.scrY - p0.scrY)*perY
    }


pos = latlngToScreenXY(52.525607, 13.404572);

pos['x] 和 pos['y] 包含转换后的经纬度 x 和 y 坐标 (52.525607, 13.404572)

我希望这对像我一样的人有帮助,可以正确解决将 lat lng 转换为本地参考坐标系的问题。

最佳


2
投票

最好转换为utm坐标,并将其视为x和y。

import utm
u = utm.from_latlon(12.917091, 77.573586)

结果将是 (779260.623156606, 1429369.8665238516, 43, 'P') 前两个可视为x,y坐标,43P为UTM Zone,小区域(宽度可达668km)可以忽略。


0
投票

最简单的方法是使用墨卡托投影。小心,两极周围可能会有一些拉伸。

以及参考代码(Kotlin):

fun latLngToXY(point: LatLng): Pair<Double, Double> {
    // Converts to radians.
    val latRad = Math.toRadians(point.latitude)
    val longRad = Math.toRadians(point.longitude)
    
    // Keep the x axis.
    val x = longRad

    // Mercator
    val y = ln(tan(latRad) - (1 / cos(latRad)))

    return Pair(x, y)
}

请注意,它在

[0;pi/2]
中返回值,因此您可能想要缩放它。

© www.soinside.com 2019 - 2024. All rights reserved.