【问题标题】:Convert longitude and latitude coordinates to image of a map pixels X and Y coordinates Java将经度和纬度坐标转换为地图像素的图像 X 和 Y 坐标 Java
【发布时间】:2016-12-09 11:37:31
【问题描述】:

我有一张以色列地图。

我需要创建一个获取两个双参数(经度和纬度)的函数,并且该函数应在地图图像的该区域上绘制一个小圆圈。

我有以下关于地图的信息:

  • 地图的宽度(以像素为单位)
  • 地图的高度(以像素为单位)
  • 地图上的纬度/经度位置至少有一个坐标
  • 每个度数之间的像素距离

我需要根据该图像将我得到的坐标转换为像素 X、Y。

有以下地图图片:

例如(不是一个准确的例子,只是一个例子,所以你明白我的意思),左上角坐标是33.5,34,它的X,Y是0, 0 在地图上。

如何将这些坐标转换为 X、Y 坐标?

我尝试了this answer,但它并没有真正起作用,它显示在31.5, 34.5 而不是33, 34

更新:这是另一个问题的虚拟快速代码示例;

public class MapRenderer extends JFrame {

    public static void main(String... args) throws IOException {

        new MapRenderer();

    }

    public MapRenderer() throws IOException {
        setSize(new Dimension(614, 1141));
        add(new TestPane());
        setVisible(true);
    }

}

class TestPane extends JPanel {

    private BufferedImage image;

    public TestPane() throws IOException {
        File file = new File("israel_map.jpg");
        BufferedImage image = ImageIO.read(file);
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics g) {
        double lon = 34;
        double lat = 33;

        int mapW = 614;
        int mapH = 1141;

        double x = (lon + 180) * (mapW / 360);

        double latRad = lat * Math.PI / 180;

        double mercN = Math.log( Math.tan( (Math.PI  / 4) + (latRad / 2)) );

        double y = (mapH / 2) - (mapW * mercN / (2 * Math.PI));

        System.out.println("[lon: " + lon + " lat: " + lat + "]: X: " + x + " Y: " + y);

        g.drawImage(image, 0, 0, null);
        g.setColor(Color.RED);
        g.drawOval((int) x, (int) y, 5, 5);
    }
}

输出:

[lon: 34.0 lat: 33.0]: X: 214.0 Y: 510.3190109117399

截图:

https://gyazo.com/5a19dece37ebace496c6b8d68eb9ec3c

【问题讨论】:

  • 它是平面地图吗?所有经纬线都是平行的(忽略地球曲率)?
  • @Drgabble 是的,它完全平坦
  • 你能发布你的代码吗?如果现有答案有效,则表明您的代码中有错字
  • 谷歌:点坡形式。

标签: java math maps coordinates


【解决方案1】:

除了像素之外,您还需要在经度/纬度中添加地图的偏移量和长度。然后你就可以进行转换了。

static final int mapWidth = 614, mapHeight = 1141;
// offsets
static final double mapLongitudeStart = 33.5, mapLatitudeStart = 33.5;
// length of map in long/lat
static final double mapLongitude = 36.5-mapLongitudeStart, 
        // invert because it decreases as you go down
        mapLatitude = mapLatitudeStart-29.5;

private static Point getPositionOnScreen(double longitude, double latitude){
    // use offsets
    longitude -= mapLongitudeStart;
    // do inverse because the latitude increases as we go up but the y decreases as we go up.
    // if we didn't do the inverse then all the y values would be negative.
    latitude = mapLatitudeStart-latitude;

    // set x & y using conversion
    int x = (int) (mapWidth*(longitude/mapLongitude));
    int y = (int) (mapHeight*(latitude/mapLatitude));

    return new Point(x, y);
}

public static void main(String[] args) {
    System.out.println(getPositionOnScreen(33.5, 33.5).toString());
    System.out.println(getPositionOnScreen(35, 32).toString());
    System.out.println(getPositionOnScreen(36.5, 29.5).toString());
}

这将打印出以下内容:

java.awt.Point[x=0,y=0]
java.awt.Point[x=307,y=427]
java.awt.Point[x=614,y=1141]

【讨论】:

  • 这些值是多少:36.5 和 29.5,你用它们来计算 mapLongitudemapLatitude
【解决方案2】:

您的代码没有考虑到您的地图不是整个世界地图。您需要对其进行调整,使其具有偏移量(因为可以说地图的左上角不是 0,0),因此它不会认为地图的宽度是 360',高度是 180'。

对于初学者,x = (longitude+180)*(mapWidth/360) 应该更像x = (longitude+<distance west of prime meridian of left side of map>)*(mapWidth/<width of map in degrees>)

【讨论】:

  • 嘿,感谢您的回复,是的,我认为这些答案应该有效,这不是因为它不是整个地图。它需要根据这张带有偏移量的地图进行计算。但问题是,我如何添加这些偏移量,以及什么?另外,“grenich 以西”是什么意思?
  • 距离应该是多少?如果距离是5144km?
猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2013-09-04
  • 1970-01-01
  • 2016-02-07
相关资源
最近更新 更多