将图像X,Y坐标转换为经度和纬度? [英] Converting image X,Y coordinates to longitude and latitude?

查看:73
本文介绍了将图像X,Y坐标转换为经度和纬度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置了特定静态地图图像的最小经度和纬度值.该地图图像是某些国家/地区的片段.

I have set values of minimum longitude and latitude of a specific static map image. That map image is a cut of some country.

/**
 * 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;

我有一个名为 mapImage BufferedImage .

我有一个与朋友一起编写的方法,该方法可以接收经度纬度,并为您提供 X Y大约在地图上的位置,以便您可以在地图上绘制一些东西.

I have a method that I wrote with a friend that receives longitude and latitude and gives you an X and a Y position approximately on the map so you can draw something on the map.

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

Now if I want to move my mouse around the map, I want it to show longitude/latitude of my mouse position, that means I need to create a method which converts X and Y of the mouse position to longitude and latitude, which should do the opposite of my other method.

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

This is my method to convert globe coordinates to image X and Y:

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 而没有经度纬度,因此这不能简单地通过反转来完成.我是坐标系统的新手,这让我有些困惑,但是我开始追赶它.

Now I tried thinking, the first thought that came into my head is just doing the same in reverse... so I started doing it and I ran into problems like, I can't get the value of adjLon or adjLat without having the longitude or latitude, so this can't be simply done by reversing it. I am all new to coordinates systems so it's all a bit confusing for me but I am starting to catch it up.

对我有什么提示吗?

根据此答案,您无法真正获得真实的结果,因为地球不是平坦的,它可以真正地将其转换为具有经度和纬度的平面地图,而无需实现真正的数学算法以使其适应变化.

According to this answer, you can't really get real results because the earth is not flat, it can't really be converted to a flat map with longitude and latitude without implementing a real mathematical algorithm to make it work with the changes.

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

There are few reasons in my code why the answer can not be exact:

  1. 由于上述原因
  2. 因为我的X,Y值是整数而不是浮点数.

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

So my question now, if it is really impossible with my method?

推荐答案

遗憾的是,没有一个简单的答案.虽然您可以自己编写投影例程,但是最简单的操作可能是获取GIS库,但是由于我最终使用C#而不是Java来完成此操作,所以我不知道有什么可用.

Sadly, there's not an easy answer to this. While you can write the projection routines yourself, the easiest thing to do is probably to get a GIS library, but since I ended up doing this in C# and not Java, I don't know what's available.

您需要的最大信息就是地图图像所使用的投影.墨卡托投影非常受欢迎,但并不是唯一的.您还需要确保所选的投影适用于所需的纬度和经度范围.如果您开始超过 +-70 N,墨卡托投影就会中断,因此,如果您在极点处做很多位置,这些位置可能不适合您.

The biggest piece of information you need is exactly which projection your map image uses. The Mercator Projection is quite popular, but it's not the only one. You also need to make sure that your chosen projection works for the range of latitudes and longitudes you want. The Mercator projection kind of breaks if you start going above +-70 N, so if you're doing a lot of positions at the poles that might not be the projection for you.

这篇关于将图像X,Y坐标转换为经度和纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆