如何从中心点计算矩形的纬度和经度

问题描述

假设给定的长度和宽度,假设分别为 4.5m 和 1.5m,我想根据中心点 lat 和 lon 绘制一个矩形。我想,我们也需要轴承。我通过在 Google Earth 上绘制一个矩形、获取位置并将它们放在我的代码中进行了模拟。但是,我需要一些自动的东西。我的问题是如何将笛卡尔坐标与以米为单位的这四个点(矩形)联系起来。

这是我的代码

import geopandas as gpd
from shapely.geometry import polygon

lat_point_list = [41.404928,41.404936,41.404951,41.404943]
lon_point_list = [2.177339,2.177331,2.177353,2.177365]

polygon_geom = polygon(zip(lon_point_list,lat_point_list))
import folium
m = folium.Map([41.4049364,2.1773560],zoom_start=20)
folium.GeoJson(polygon_geom).add_to(m)
folium.LatLngPopup().add_to(m)
m

我想要这个:

enter image description here

更新:

我知道这是基本的三角学。如果我将 rectsngle 分成三角形,我们可以找到不同的点。我知道它是简单练习的基础,但是,我不知道使用笛卡尔坐标时它会发生变化。然后,我的目标是得到点 A、B、C 和 D,知道矩形的中心在经纬度、长和宽。

enter image description here

解决方法

获取点的矩形 (NE,SW) 边界并将其用作 folium.Rectangle 的边界。

例如,使用您的数据。 4.5m和1.5m看矩形有点小:

import geopy
import geopy.distance
import math
import folium

def get_rectangle_bounds(coordinates,width,length):
    start = geopy.Point(coordinates)
    hypotenuse = math.hypot(width/1000,length/1000)

    # Edit used wrong formula to convert radians to degrees,use math builtin function
    northeast_angle = 0 - math.degrees(math.atan(width/length)) 
    southwest_angle = 180 - math.degrees(math.atan(width/length)) 

    d = geopy.distance.distance(kilometers=hypotenuse/2)
    northeast = d.destination(point=start,bearing=northeast_angle)
    southwest = d.destination(point=start,bearing=southwest_angle)
    bounds = []
    for point in [northeast,southwest]:
        coords = (point.latitude,point.longitude)
        bounds.append(coords)

    return bounds

# To get a rotated rectangle at a bearing,you need to get the points of the the recatangle at that bearing
def get_rotated_points(coordinates,bearing,length):
    start = geopy.Point(coordinates)
    width  = width/1000
    length = length/1000
    rectlength = geopy.distance.distance(kilometers=length)
    rectwidth = geopy.distance.distance(kilometers=width)
    halfwidth = geopy.distance.distance(kilometers=width/2)
    halflength = geopy.distance.distance(kilometers=length/2)

    pointAB = halflength.destination(point=start,bearing=bearing)
    pointA = halfwidth.destination(point=pointAB,bearing=0-bearing)
    pointB = rectwidth.destination(point=pointA,bearing=180-bearing)
    pointC = rectlength.destination(point=pointB,bearing=bearing-180)
    pointD = rectwidth.destination(point=pointC,bearing=0-bearing)

    points = []
    for point in [pointA,pointB,pointC,pointD]:
        coords = (point.latitude,point.longitude)
        points.append(coords)

    return points

start_coords = [41.4049364,2.1773560]
length = 4.50 #in meters
width = 1.50
bearing = 45 #degrees

m = folium.Map(start_coords,zoom_start=20)
bounds = get_rectangle_bounds(tuple(start_coords),length )
points = get_rotated_points(tuple(start_coords),length)

folium.Rectangle(bounds=bounds,fill=True,color='orange',tooltip='this is Rectangle'
               ).add_to(m)

# To draw a rotated rectangle,use folium.Polygon
folium.Polygon(points).add_to(m)