当前位置: 首页 > 知识库问答 >
问题:

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

南门建章
2023-03-14

我有一张以色列地图。

我需要创建一个获得两个双参数(经度和纬度)的函数,该函数应该在地图图像中的那个区域上画一个小圆圈。

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

    null

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

共有1个答案

仲孙疏珂
2023-03-14

除了像素之外,还需要添加地图的经度/纬度偏移和长度。然后你就可以做一个转换。

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]
 类似资料:
  • 问题内容: 我正在尝试将经纬度对转换为像素坐标。我发现了这种墨卡托投影,但我不理解代码。x_adj,y_adj变量是什么因素?当我在没有这些常量的情况下运行代码时,我的经/纬对就不在地图上,并且x和y像素坐标也不是我想要的。 问题答案: 这些变量从何而来 选择这些变量以使计算出的坐标与地图的背景图像匹配。如果知道地图的投影参数,则可以计算它们。但是我相信,它们很可能是通过反复试验而获得的。 如何计

  • 问题内容: 我具有纽约市纽约市的纬度/经度值;40.7560540,-73.9869510和地球的平面图像,即1000px×446px。 我希望能够使用Javascript将纬度/经度转换为X,Y坐标,该点将反映该位置。 因此,图像左上角的X,Y坐标将是;289、111 注意事项: 不用担心要使用哪种投影的问题,可以自己做假设,也可以按照自己知道的可行的方法进行操作 X,Y可以形成图像的任意一角

  • 我正在使用PDFBox的在Java中显示PDF页面。我正试图基于页面中的(即AcroForm字段)在PDF页面的图像上创建可点击的区域。问题是PDF似乎使用了一个完全不同的坐标系: 收益率 如果我要估计图像上字段矩形的实际尺寸,它将是40,40,50,10(x,y,宽度,高度)。这两者之间没有明显的相关性,我似乎找不到任何关于这一点的信息与谷歌。 如何确定PDPage的cosobects的像素位置

  • 我的表有两个浮动列,分别表示纬度和经度坐标。 我想使用PostGIS的ST_DWithin查找距离给定点一定距离内的所有记录。 的签名期望前两个参数是几何体或地理数据类型,因此我非常确定解决方案是将纬度/液化天然气坐标转换为地理坐标,但我无法让它工作。 以下是不起作用的: 我得到这个错误: 错误:函数st_geogfromtext(未知)不存在 第1行:从ST_DWithin(ST_GeogFro

  • 我正在尝试将极坐标的图像转换为笛卡尔坐标。 将图像转换为极坐标的示例显式执行-想要一个光滑的矩阵方法 我原以为使用上述方法是小菜一碟,但事实并非如此!!如果有人发现我的代码有错误,请告诉我! 我发现非常奇怪的是,当我改变phi时,它会做出根本性的改变,而不是以我期望的方式! 干杯

  • 问题内容: 请有人可以发布SQL函数将东/北转换为经度/纬度。我知道它非常复杂,但是我还没有找到任何在T-SQL中对其进行过文档记录的人。 此javascript代码有效,但我在将其转换为SQL时遇到了麻烦。 我有16,000个坐标,需要将它们全部转换为纬度/经度。 到目前为止,这是我所掌握的,但是并没有超越while循环。 问题答案: 我最终使用了以下javascript函数来转换值。我知道这不