将经纬度坐标转换为地图像素 X 和 Y 坐标的图像 Java

Convert longitude and latitude coordinates to image of a map pixels X and Y coordinates Java

我有一张以色列地图。

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

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

我需要根据该图像将得到的坐标转换为像素 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

您的代码没有考虑到您的地图不是整个世界地图。您需要对其进行调整,使其具有偏移量(因为可以说地图的左上角不是 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>)

除了像素之外,您还需要在 longitude/latitude 中添加地图的偏移量和长度。然后你可以做一个转换。

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]