将纬度/经度点转换为墨卡托投影上的像素(x,y)

将纬度/经度点转换为墨卡托投影上的像素(x,y)

我正在尝试将纬度/长点转换为2d点,以便我可以将它显示在世界的图像上 - 这是一个墨卡托投影。


我已经看到了各种方法来做这个以及关于堆栈溢出的一些问题 - 我尝试了不同的代码片段,虽然我得到了像素的正确经度,但纬度总是偏离 - 似乎变得更加合理。


我需要公式来考虑图像大小,宽度等。


我试过这段代码:


double minLat = -85.05112878;

double minLong = -180;

double maxLat = 85.05112878;

double maxLong = 180;


// Map image size (in points)

double mapHeight = 768.0;

double mapWidth = 991.0;


// Determine the map scale (points per degree)

double xScale = mapWidth/ (maxLong - minLong);

double yScale = mapHeight / (maxLat - minLat);


// position of map image for point

double x = (lon - minLong) * xScale;

double y = - (lat + minLat) * yScale;


System.out.println("final coords: " + x + " " + y);

在我尝试的例子中,纬度似乎偏差约30px。任何帮助或建议?


更新


基于这个问题:Lat / lon到xy


我试图使用提供的代码,但我仍然有一些纬度转换的问题,经度很好。


int mapWidth = 991;

int mapHeight = 768;


double mapLonLeft = -180;

double mapLonRight = 180;

double mapLonDelta = mapLonRight - mapLonLeft;


double mapLatBottom = -85.05112878;

double mapLatBottomDegree = mapLatBottom * Math.PI / 180;

double worldMapWidth = ((mapWidth / mapLonDelta) * 360) / (2 * Math.PI);

double mapOffsetY = (worldMapWidth / 2 * Math.log((1 + Math.sin(mapLatBottomDegree)) / (1 - Math.sin(mapLatBottomDegree))));


double x = (lon - mapLonLeft) * (mapWidth / mapLonDelta);

double y = 0.1;

if (lat < 0) {

    lat = lat * Math.PI / 180;

    y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);

} else if (lat > 0) {

    lat = lat * Math.PI / 180;

    lat = lat * -1;

    y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);

    System.out.println("y before minus: " + y);

    y = mapHeight - y;

} else {

    y = mapHeight / 2;

}

System.out.println(x);

System.out.println(y);

当使用原始代码时,如果纬度值为正,则返回负点,因此我稍微修改了它并用极端纬度测试 - 应该是点0和点766,它工作正常。然而,当我尝试不同的纬度值ex:58.07(在英国北部)时,它显示为西班牙北部。


摇曳的蔷薇
浏览 1874回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java