gpt4 book ai didi

java - 将图像 X、Y 坐标转换为经度和纬度?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:08 25 4
gpt4 key购买 nike

我已经设置了特定静态 map 图像的最小经度和纬度值。该 map 图像是某个国家/地区的剪切图。

/**
* Maximum longitude value of the map
*/
private float mapLongitudeMax;

/**
* Minimum longitude value of the map
*/
private float mapLongitudeMin;

/**
* Maximum latitude value of the map
*/
private float mapLatitudeMax;

/**
* Minimum latitude value of the map
*/
private float mapLatitudeMin;

我有一个名为 mapImageBufferedImage

我有一个方法是我和一个 friend 写的,它接收 longitudelatitude 并给你一个 X 和一个 Y 在 map 上大致定位,以便您可以在 map 上绘制一些东西。

现在,如果我想在 map 上移动鼠标,我希望它显示鼠标位置的 longitude/latitude,这意味着我需要创建一个方法来转换 map 的 X 和 Y鼠标位置到 longitudelatitude,这应该与我的其他方法相反。

这是我将地球坐标转换为图像 XY 的方法:

protected Location getCoordinatesByGlobe(float latitude, float longitude) {

/**
* Work out minimum and maximums, clamp inside map bounds
*/
latitude = Math.max(mapLatitudeMin, Math.min(mapLatitudeMax, latitude));
longitude = Math.max(mapLongitudeMin, Math.min(mapLongitudeMax, longitude));

/**
* We need the distance from 0 or minimum long/lat
*/
float adjLon = longitude - mapLongitudeMin;
float adjLat = latitude - mapLatitudeMin;

float mapLongWidth = mapLongitudeMax - mapLongitudeMin;
float mapLatHeight = mapLatitudeMax - mapLatitudeMin;

float mapWidth = mapImage.getWidth();
float mapHeight = mapImage.getHeight();

float longPixelRatio = mapWidth / mapLongWidth;
float latPixelRatio = mapHeight / mapLatHeight;

int x = Math.round(adjLon * longPixelRatio) - 3;// these are offsets for the target icon that shows.. eedit laterrr @oz
int y = Math.round(adjLat * latPixelRatio) + 3; //

// turn it up
y = (int) (mapHeight - y);

return new Location(x, y);
}

现在我试着思考,进入我脑海的第一个想法就是反过来做同样的事情......所以我开始这样做,但遇到了一些问题,比如,我无法获得 adjLon 的值adjLat 而没有 longitudelatitude,所以这不能简单地通过反转它来完成。我是坐标系统的新手,所以这对我来说有点困惑,但我开始 catch 它了。

有什么建议吗?

编辑(不可能?)

根据 this answer ,你无法真正获得真实的结果,因为地球不是平坦的,如果不实现真正的数学算法使其适应变化,就无法真正将其转换为具有经度和纬度的平面 map 。

我的代码中有几个原因导致答案不准确:

  1. 由于上述原因
  2. 因为我的 X、Y 值是整数而不是 float 。

那么我现在的问题是,我的方法是否真的不可能?

最佳答案

遗憾的是,这个问题没有简单的答案。虽然您可以自己编写投影例程,但最简单的方法可能是获得一个 GIS 库,但由于我最终是在 C# 而不是 Java 中执行此操作,所以我不知道有什么可用的。

您需要的最大信息就是您的 map 图像使用的投影。 The Mercator Projection很受欢迎,但它不是唯一的。您还需要确保您选择的投影适用于您想要的纬度和经度范围。如果你开始超过 +-70 N,墨卡托投影就会中断,所以如果你在极点做很多位置,这可能不是你的投影。

关于java - 将图像 X、Y 坐标转换为经度和纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38818152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com