简体   繁体   English

尝试在经纬度点周围绘制正方形

[英]Trying to draw a square around a latitude and longitude point

I am working on a C# script to do some mapping. 我正在使用C#脚本进行一些映射。 I have one function I am working on where I am given a latitude and longitude, a distance, and an angle of rotation. 我有一个功能正在处理给我的经度,纬度,距离和旋转角度。

I then want to create a rectangle, that has the width of the distance passed in (length is dependant on something else), this square would have the latitude and longitude point at it's centre, and would be rotated any angle from 0 - 360. 然后,我想创建一个矩形,该矩形具有传入的距离的宽度(长度取决于其他因素),这个正方形将在其中心具有经度和纬度点,并且可以从0-360任意角度旋转。

The distance double that is passed in can either be in feet, or in meters, and the units are determined using the isMetric boolean. 传入的距离double可以以英尺或米为单位,并且使用isMetric布尔值确定单位。

The problem I believe has to do with my formula's that I am using because when it draws the square from the 4 points, the size of the square is too large. 我认为问题与我正在使用的公式有关,因为当它从4个点绘制正方形时,正方形的大小太大。 As well the angle of rotation seems to be set at 45 degrees when you pass it a angle of 0.0. 同样,当您将旋转角度设置为0.0时,旋转角度似乎设置为45度。

Here is what I have so far: 这是我到目前为止的内容:

Parameters: Latitude, Longitude (in decimal format), (Double) distance, (double) angle 参数:纬度,经度(十进制格式),(双精度)距离,(双精度)角度

            double diagonal = Math.Sqrt(Math.Pow((distance / 2), 2) * 2); //a^2 + b^2 = d^2

        if (isMetric) //Convert meters to km.
        {
            diagonal = diagonal / 1000;
        }

        else   //Convert feet to km.
        {
            diagonal = diagonal * 0.0003048;
        }

        MessageBox.Show("Diagonal: " + diagonal, "DEBUG"); //DEBUG

        double pt1_lat = latDistance(diagonal * Math.Sin(angle), latitude);
        double pt1_long = longDistance(diagonal * Math.Cos(angle), latitude, longitude);

        double pt2_lat = latDistance(diagonal * Math.Cos(angle), latitude);
        double pt2_long = longDistance(-diagonal * Math.Sin(angle), latitude, longitude);

        double pt3_lat = latDistance(-diagonal * Math.Sin(angle), latitude);
        double pt3_long = longDistance(-diagonal * Math.Cos(angle), latitude, longitude);

        double pt4_lat = latDistance(-diagonal * Math.Cos(angle), latitude);
        double pt4_long = longDistance(diagonal * Math.Sin(angle), latitude, longitude); 

The remaining methods are below: 其余方法如下:

        private double latDistance(double distance, double latitude)
    {
        return latitude + degToRad(distance / EARTH_RADIUS);
    }

    private double longDistance(double distance, double latitude, double longitude)
    {
        return longitude + degToRad(distance / EARTH_RADIUS / Math.Cos(latitude));
    }

    private double degToRad(double degrees)
    {
        return (degrees * Math.PI) / 180;
    }

    public double radToDeg(double radians)
    {
        return (180.0 * radians) / Math.PI;
    }

Any help is greatly appreciated :) 任何帮助是极大的赞赏 :)

First of all I assume you have a "small" square with respect to EARTH radius. 首先,我假设您相对于EARTH半径有一个“小”正方形。 In this case if distance is the length of the side and angle is the rotation angle, you should compute dx,dy which are the cartesian coordinates of one vertex with respect to the center of the square: 在这种情况下,如果距离是边的长度,角度是旋转角度,则应计算dx,dy,它们是一个顶点相对于正方形中心的直角坐标:

dx = 0.5 * Math.Sqrt(2.0) * distance * Math.Cos(angle+0.25*Math.PI);
dy = 0.5 * Math.Sqrt(2.0) * distance * Math.Sin(angle+0.25*math.PI);

(here I assume that angle is in radians, otherwise you should convert it) The cartesian coordinates of the other 3 vertices are, respectively: (-dy,dx), (-dx,-dy), (dy,-dx) (这里我假设角度以弧度表示,否则应将其转换)其他3个顶点的笛卡尔坐标分别为:(-dy,dx),(-dx,-dy),(dy,-dx)

To convert the cartesian coordinates to (longitude,latitude) you can use your formulas: 要将笛卡尔坐标转换为(经度,纬度),可以使用以下公式:

pt_lat = latDistance(latitude,dy)
pt_long = longDistance(dx,latitude,longitude)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM