Java 中的边界框计算

发布于 2024-09-18 18:13:57 字数 550 浏览 7 评论 0原文

我正在尝试创建一种方法,给定纬度、经度和范围(以英里或公里为单位)返回边界框的东北角和西南角?

我在某处找到了一个函数,但经过一些测试后它似乎不起作用(见下文):

        double latrange=range/69.172;
    double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
    double minlat=inLat-latrange;
    double maxlat=inLat+latrange;
    double minlon=inLong-longrange;
    double maxlon=inLong+longrange;

    MapCoord min = new MapCoord(minlat,minlon);
    MapCoord max = new MapCoord(maxlat,maxlon);
    MapCoord [] rs = new MapCoord[2];
    rs[0] = min;
    rs[1] = max;
    return rs;  

I'm trying to create a method that given a latitude and longitude and range (in miles or km) returns the NE and SW corners of a bounding box?

I had a function that I found somewhere but after some testing it doesn't seem to work( see below):

        double latrange=range/69.172;
    double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
    double minlat=inLat-latrange;
    double maxlat=inLat+latrange;
    double minlon=inLong-longrange;
    double maxlon=inLong+longrange;

    MapCoord min = new MapCoord(minlat,minlon);
    MapCoord max = new MapCoord(maxlat,maxlon);
    MapCoord [] rs = new MapCoord[2];
    rs[0] = min;
    rs[1] = max;
    return rs;  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-09-25 18:13:57

不清楚你所说的“不起作用”是什么意思,但从我所看到的代码确实会产生输入的两点 SW 和 NE 。然而,它将突破两极和国际日期变更线。

产生看起来合理的输出的简单测试程序:

public class LatLonBoundBox {

    public static class MapCoord {
        final double lat;
        final double lon;
        public MapCoord(double lat, double lon) {
            this.lat=lat;
            this.lon=lon;
        }
        @Override
        public String toString() {
            return "MapCoord [lat=" + lat + ", lon=" + lon + "]";
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        double range = 1.0; 
        double inLat = 51.350801;
        double inLong = -0.251850;

        double latrange=range/69.172;
        double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
        double minlat=inLat-latrange;
        double maxlat=inLat+latrange;
        double minlon=inLong-longrange;
        double maxlon=inLong+longrange;

        MapCoord min = new MapCoord(minlat,minlon);
        MapCoord max = new MapCoord(maxlat,maxlon);
        System.out.println(min);  
        System.out.println(max);  
    }

}

Not clear what you mean by "doesn't work" but from what I can see the code does result in two points SW and NE of the input. It will however break close to the poles and the international dateline.

Simple test program that produces reasonable looking output:

public class LatLonBoundBox {

    public static class MapCoord {
        final double lat;
        final double lon;
        public MapCoord(double lat, double lon) {
            this.lat=lat;
            this.lon=lon;
        }
        @Override
        public String toString() {
            return "MapCoord [lat=" + lat + ", lon=" + lon + "]";
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        double range = 1.0; 
        double inLat = 51.350801;
        double inLong = -0.251850;

        double latrange=range/69.172;
        double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
        double minlat=inLat-latrange;
        double maxlat=inLat+latrange;
        double minlon=inLong-longrange;
        double maxlon=inLong+longrange;

        MapCoord min = new MapCoord(minlat,minlon);
        MapCoord max = new MapCoord(maxlat,maxlon);
        System.out.println(min);  
        System.out.println(max);  
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文