ios - 如何将给定坐标转换为“直径”x的边界框?

标签 ios geolocation gps coordinates geocoding

例如,我有一个十进制格式的经纬度(而不是像lat=44.1°9.5'30''这样的度-小时-分钟)。要搜索附近的对象,必须将搜索“半径”指定为具有四个值的矩形:

north = 44.1;
south = -9.9;
east = -22.4;
west = 55.2;

是否有一个公式或经验法则可以将十进制纬度/经度值转换为矩形边界框,使给定的纬度/经度位于该框的中心?
我必须摆弄自己与WGS84椭球算法或有开放的解决方案的问题?

最佳答案

我确实遇到了这个问题,解决方法并不是那么直接,但好消息是,经过大量工作(以及SO和Google的大量帮助),我认为我已经破解了这个问题。
像proj4这样的库有很多,它们提供了许多算法来执行所需的转换,但是当遇到困难时,我发现这一切都有点混乱,最后我编写了自己的代码(我总是想知道事情是如何工作的)。
我的解决方案基于ECEF并且它的工作方式如下…
我相信你已经知道了,纬度线之间的距离总是一样的(10度到20度之间的距离和20度到30度之间的距离是一样的),但是经度线在两极会合。所以赤道处经度在10度到20度之间的距离远大于两极附近的距离(两极的距离为0)。
所以你可以很容易地计算出2度纬度之间有多少米,但是要计算经度,你必须考虑到纬度。
在赤道附近,纬度的1度和长纬度的1度几乎是一样的距离,所以如果我们投影的地图的中心(0,0),我们可以简单地用纬度和长乘以一个常数,得到任何给定点距离地图中心的米数。
所以我的算法有效地旋转地球直到地图的实际中心在0,0。
假设中心真的在(50.52,-4.82)-我的情况就是这样。
想象一下,你拿着一个地球仪,俯视着它,在你正下方的可见中心有0拉特,0长。
我们需要做的是把我们现在正下方有(0,0)的地球仪往西(向右)旋转,直到(0,-4.82)在我们下方。
然后我们向南(向下)旋转地球直到(50.52,-4.82)低于我们。
作为第三步,我们可能需要顺时针或逆时针旋转它,以校正地图相对于正北的方向(如果正北在地图上是直的,或者如果您感兴趣的只是距离不是方位角,则不需要这样做)
所以从概念上来说这是我们需要做的,但是这和我们的算法有什么关系呢?
答案是一个变换(类),我们在其中输入三个旋转角度。这个类有一个公共函数,给定一个lat/long对,它将在旋转后返回地球上该点的一个新的lat/long对。
一旦我们知道了地球的半径,我们就可以把这对新的坐标转换成x和y坐标,表示与地图原点的距离。
我应该在这里提到,地球在赤道比在两极更宽,但是处理这个问题的数学方法根本不值得费心。不管你怎么计算你的x,y坐标,它们总是稍微偏出,因为地球不是平的,对我来说,下面的代码可以完成这项工作。
如果你的地图非常接近极点,我怀疑这个算法的结果可能会变得非常不准确-基本上lat/long在极点的效果不是很好(从上面看一下google earth)。
maptransform类需要设置一些东西。
setradius(1000);设置转换以使用半径为1000(单位)的球体
setbody(“earth”)使用地球的平均半径(以米为单位)设置变换
setrotation(x,y,z);设置变换以围绕z轴旋转z度、y轴旋转y度,然后x轴旋转x度。
-基本上,考虑到你的中心点(lat,long)和地图上的正北方向是直的,你需要如下:setrotation(0,lat,-long);
-旋转的顺序在这里是非常重要的,基于一个坐标系(回望你拿着的地球仪),其中Z轴与地球的旋转一致,Y轴上下旋转地球仪最近的表面,而X轴是你正在观察的轴-希望T他的解释很有道理,这是一个很难描述的概念-参见Rotation Matrix
考虑到您需要从纬度/经度到距离特定点的米数的地图,以上应该是您所需要的全部。
函数getMapPosition(lat,long)将从原点返回一个double[],其中包含以地图单位表示的x,y(如果以米为单位指定半径,则以米为单位)。
我的课程在将坐标应用于特定的地图平铺方面做得更进一步…
setmaporigin(x,y);设置贴图的旋转原点(旋转后观察者正下方的点)相对于贴图左下角的位置。名义上这应该以米为单位(当然,如果你使用了setbody(“earth”);)但是需要与指定的半径使用相同的单位。
setmapsize(w,h);设置地图的大小(以米为单位)或您决定使用的任何单位。
最后,setbitmapsize(w,h)允许您描述要将地图投影到的位图的大小(以像素为单位)。在我的应用程序中,我有一个地图区域的位图表示,并使用转换来提供位图上应该绘制点的像素的精确坐标。但是,这不是你问的问题的一部分,所以你可能不需要它。
真的希望这能有帮助-就像我一个月前看到的所有例子一样冗长复杂。

import java.text.DecimalFormat;

public class MapTransform {

    private double circumference;
    private RotationMatrix rotationMatrix;
    private double originX;
    private double originY;
    private double mapWidth;
    private double mapHeight;
    private int bitmapWidth;
    private int bitmapHeight;

    public MapTransform() {
        this.circumference = 0;
        this.rotationMatrix = new RotationMatrix();
        this.rotationMatrix.makeIdentity();
        this.originX = 0;
        this.originY = 0;
        this.mapWidth = 0;
        this.mapHeight = 0;
        this.bitmapWidth = 0;
        this.bitmapHeight = 0;
    }


    public void setCircumference(double circumference) {
        this.circumference = circumference;
    }
    public void setRadius(double radius) {
        this.circumference = 2 * Math.PI * radius;
    }
    public void setBody(String body) {
        if (body.toUpperCase().equals("EARTH")) {
            setRadius(6371009);     //mean radius of the earth in metres 
//          setRadius(6378137);     //equatorial radius of the earth in metres 
//          setRadius(6356752);     //polar radius of the earth in metres 
        }
        else {
            setRadius(0);
        }
    }

    public void setRotation(double xRotateDegrees, double yRotateDegrees, double zRotateDegrees) {
        RotationMatrix xMatrix = new RotationMatrix();
        RotationMatrix yMatrix = new RotationMatrix();
        RotationMatrix zMatrix = new RotationMatrix();
        xMatrix.makeRotateX(Math.toRadians(xRotateDegrees));
        yMatrix.makeRotateY(Math.toRadians(yRotateDegrees));
        zMatrix.makeRotateZ(Math.toRadians(zRotateDegrees));
        this.rotationMatrix = zMatrix.concatenate(yMatrix).concatenate(xMatrix);
    }

    public void setMapOrigin(double originX, double originY) {
        this.originX = originX;
        this.originY = originY;
    }

    public void setMapSize(double width, double height) {
        this.mapWidth = width;
        this.mapHeight = height;
    }

    public void setBitmapSize(int width, int height) {
        this.bitmapWidth = width;
        this.bitmapHeight = height;
    }


    public double[] getMapPosition(double[] geoPosition) {
        return getMapPosition(geoPosition[0], geoPosition[1]);
    }
    public double[] getMapPosition(double latitude, double longitude) {
        // convert the GeoPosition into an NVector
        NVector vec = new NVector(latitude, longitude);
        // rotate the vector in 3D
        vec = rotationMatrix.transform(vec);
        // convert the vector into 2D units by applying circumference to latitude/longitude and adding origins
        double x = vec.getLongitude() * this.circumference / 360;
        double y = vec.getLatitude() * this.circumference / 360;
        // return a MapPosition
        return new double[] {x, y};
    }


    public float[] getPixelPosition(double[] mapPosition) {
        return getPixelPosition(mapPosition[0], mapPosition[1]);
    }
    public float[] getPixelPosition(double mapX, double mapY) {
        // apply origin and scale based on map and bitmap widths 
        float x =  (float) ((this.originX + mapX) * this.bitmapWidth / this.mapWidth);
        // apply origin and scale based on map and bitmap heights, but invert to measure from top left instead of bottom left
        float y =  (float) (this.bitmapHeight - (this.originY + mapY) * this.bitmapHeight / this.mapHeight);
        return new float[] {x, y};
    }


    public class RotationMatrix {
        String name = "";
        public double array [][] = {{0,0,0},{0,0,0},{0,0,0}};

        public RotationMatrix() {}

        public RotationMatrix(String name) {
            this.name = name;
        }
        public void makeIdentity() {
            for(int x = 0; x <= 2; x++) {
                for (int y = 0; y <= 2; y++) {
                    array[x][y] = (x == y)? 1: 0;
                }
            }
        }

        public void makeRotateX(double thetaRadians) {
            double cosTheta = Math.cos(thetaRadians);
            double sinTheta = Math.sin(thetaRadians);
            makeIdentity();
            array[1][1] = cosTheta;
            array[2][1] = -sinTheta;
            array[1][2] = sinTheta;
            array[2][2] = cosTheta;
        }

        public void makeRotateY(double thetaRadians) {
            double cosTheta = Math.cos(thetaRadians);
            double sinTheta = Math.sin(thetaRadians);
            makeIdentity();
            array[0][0] = cosTheta;
            array[2][0] = sinTheta;
            array[0][2] = -sinTheta;
            array[2][2] = cosTheta;
        }

        public void makeRotateZ(double thetaRadians) {
            double cosTheta = Math.cos(thetaRadians);
            double sinTheta = Math.sin(thetaRadians);
            makeIdentity();
            array[0][0] = cosTheta;
            array[1][0] = -sinTheta;
            array[0][1] = sinTheta;
            array[1][1] = cosTheta;
        }

        public NVector transform(NVector vec) {
            NVector vec2 = new NVector();
            vec2.x = vec.x * array[0][0] + vec.y * array[1][0] + vec.z * array[2][0];
            vec2.y = vec.x * array[0][1] + vec.y * array[1][1] + vec.z * array[2][1];
            vec2.z = vec.x * array[0][2] + vec.y * array[1][2] + vec.z * array[2][2];
            return vec2;
        }

        public void output() {
            if (this.name != null && this.name.length() == 0) {
                System.out.println(this.name + "-------");
            }
            DecimalFormat df = new DecimalFormat("0.00");
            for(int y = 0; y <= 2; y++) {
                String out = "| ";
                double test = 0;
                for(int x = 0; x <= 2; x++) {
                    String f = df.format(array[x][y]);
                    if (f.length() < 5) f = " " + f;
                    out += f + " ";
                    test = test + array[x][y] * array[x][y];
                }
                if (test > 0.99 && test < 1.01) {test = 1.0;}
                out += "| (=" + test + ")";
                System.out.println(out);
            }
            System.out.println();
        }

        public RotationMatrix concatenate(RotationMatrix m2) {
            RotationMatrix outputMatrix = new RotationMatrix();
            for(int x = 0; x <= 2; x++) {
                for(int y = 0; y <=2; y++) {
                    outputMatrix.array[x][y] = 0;
                    for (int q = 0; q <= 2; q++) {
                        outputMatrix.array[x][y] += this.array[x][q] * m2.array[q][y];
                    }
                }
            }
            return outputMatrix;
        }

    }

    public class NVector {
        double x;
        double y;
        double z;

        public NVector() {
            this.x = 0;
            this.y = 0;
            this.z = 0;
        }

        public NVector(double x, double y, double z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public NVector(double latitude, double longitude) {
            setLatitudeLongitude(latitude, longitude);
        }

        public NVector(double[] geoPosition) {
            setLatitudeLongitude(geoPosition[0], geoPosition[1]);
        }

        private void setLatitudeLongitude(double latitude, double longitude) {
            double latitudeRadians = Math.toRadians(latitude);
            double longitudeRadians = Math.toRadians(longitude);
            double cosLatitude = Math.cos(latitudeRadians);
            double cosLongitude = Math.cos(longitudeRadians);
            double sinLatitude = Math.sin(latitudeRadians);
            double sinLongitude = Math.sin(longitudeRadians);

            this.x = cosLatitude * cosLongitude;
            this.y = cosLatitude * sinLongitude;
            this.z = sinLatitude;
        }

        public double getLatitude() {
            return Math.toDegrees(Math.atan2(this.z, Math.sqrt(this.x * this.x + this.y * this.y)));
        }
        public double getLongitude() {
            return Math.toDegrees(Math.atan2(this.y, this.x));
        }
        public double[] getGeoPosition() {
            double[] geoPosition = new double[] {this.getLatitude(), this.getLongitude()};
            return geoPosition;
        }

        public void output() {
            output("");
        }

        public void output(String name) {
            if (name != null && name.length() == 0) {
                System.out.println("NVector: " + name);
            }
            DecimalFormat df = new DecimalFormat("0.00");
            String vector = df.format(this.x) + "," + df.format(this.y) + "," + df.format(this.z);
            String coords = "";
            try {
                coords = df.format(Math.toDegrees(this.getLatitude())) + "N " + df.format(Math.toDegrees(this.getLongitude())) + "E";
            }
            catch(Exception e) {
                coords = "(coords unknown)";
            }
            System.out.println("(" + vector + ") at " + coords);
        }
    }
}

关于ios - 如何将给定坐标转换为“直径”x的边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8459247/

相关文章:

ios - 如何使用 UILocalNotification 设置振动并且声音不会只振动 iphone

android - Xamarin.Forms 相机控制访问 DependencyService

http - IP地址定位检测技术

android - 获取android/iphone到服务器的准确时间

ios - 如何防止 NSTextAttachment 断行?

javascript - 检查应用程序是否存在于设备上

android - navigator.geolocation.getCurrentPosition 总是得到错误代码 3 : timeout expired

iphone - 获取特定地理位置的时间

java - logcat 在 Android 中显示无法解释的 libgps 错误

Android 每 2 小时以编程方式将 GPS 发送到 MySql 数据库