如何在python中以给定点(lat,long)和距离的给定点(lat,long)和距离获取边界框(minx,miny,maxx,maxy)?

发布于 2025-02-09 18:47:08 字数 520 浏览 0 评论 0原文

如何使用给定点(LAT,长)和以python为单位(lat,long)的边界盒(Minx,Miny,Maxx,Maxy)元组?

给定的距离是我要寻找的边界框的对角线的一半。

paris_point = (48.8588548, 2.347035)
distance_km = 20

#Get bounding_box
def get_bounding_box(point, distance):
    ???
    return (minx, miny, maxx, maxy)

result = get_bounding_box(paris_point, distance_km)

Minx是西南角的经度,

是西南角的纬度

Maxx的纬度是东北角Maxy的经度,

Maxy是东北角的纬度,

我尝试了Geopandas,但我什么也没发现... 有没有可以做到这一点的自由?

你能帮我吗? 谢谢

How to get bounding-box (minx, miny, maxx, maxy) tuple for an given point (lat, long) and given distance in kilometers (int or float) with python ?

The given distance is the half of the diagonal of the bounding-box I am looking for.

paris_point = (48.8588548, 2.347035)
distance_km = 20

#Get bounding_box
def get_bounding_box(point, distance):
    ???
    return (minx, miny, maxx, maxy)

result = get_bounding_box(paris_point, distance_km)

minx is the longitude of the southwestern corner

miny is the latitude of the southwestern corner

maxx is the longitude of the northeastern corner

maxy is the latitude of the northeastern corner

I try with geopandas but I don't find anything...
Is there a lib that can do this?

Can you help me please ?
Thanks

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

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

发布评论

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

评论(1

时光礼记 2025-02-16 18:47:08

在此上下文中的边界框可能会令人困惑,因为只有一个点对象。让我们称其为正方形缓冲区。
geopanadas 在这里有点过分,它便利地包装&amp&amp;提供您需要的所有功能。但是,对于较少的依赖项,您可以使用它的基础库 - shapely &amp; <

import geopandas as gpd
import pyproj
from shapely.geometry import Point
from shapely.ops import transform
from math import sqrt

# with GeoPandas
def get_buffer_box_geopandas(point_lat_long, distance_km):
    # distance is d/2 of the square buffer around the point,
    # from center to corner;
    # find buffer width in meters
    buffer_width_m = (distance_km * 1000) / sqrt(2)
    (p_lat, p_long) = point_lat_long

    # Geopandas Geodataframe with a single point
    # EPSG:4326 sets Coordinate Reference System to WGS84 to match input
    wgs84_pt_gdf = gpd.GeoDataFrame(geometry = gpd.points_from_xy([p_long],[p_lat], crs='EPSG:4326'))
    
    # find suitable projected coordinate system for distance
    utm_crs = wgs84_pt_gdf.estimate_utm_crs()
    # reproject to UTM -> create square buffer (cap_style = 3) around point -> reproject back to WGS84
    wgs84_buffer = wgs84_pt_gdf.to_crs(utm_crs).buffer(buffer_width_m, cap_style=3).to_crs('EPSG:4326')
    # wgs84_buffer.bounds returns bounding box as pandas dataframe, 
    # .values[0] will extract first row as an array
    return wgs84_buffer.bounds.values[0]

# with Shapely & pyproj
def get_buffer_box_shapely(point_lat_long, distance_km):
    buffer_width_m = (distance_km * 1000) / sqrt(2)
    (p_lat, p_long) = point_lat_long

    # create Shapely Point object, coodrinates as x,y
    wgs84_pt = Point(p_long, p_lat)
    # set up projections WGS84 (lat/long coordinates) for input and 
    # UTM to measure distance
    # https://epsg.io/4326
    wgs84 = pyproj.CRS('EPSG:4326')
    # sample point in France - UTM zone 31N 
    # Between 0°E and 6°E, northern hemisphere between equator and 84°N
    # https://epsg.io/32631
    utm = pyproj.CRS('EPSG:32631')
    
    # transformers:
    project_wgs84_to_utm = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
    project_utm_to_wgs84 = pyproj.Transformer.from_crs(utm, wgs84, always_xy=True).transform

    # tranform Point to UTM
    utm_pt = transform(project_wgs84_to_utm, wgs84_pt)
    # create square buffer (cap_style = 3) around the Point
    utm_buffer = utm_pt.buffer(buffer_width_m, cap_style=3)
    # tranform buffer back to WGS84 and get bounds in lat / long 
    wgs84_bounds = transform(project_utm_to_wgs84, utm_buffer).bounds
    return wgs84_bounds

paris_point = (48.8588548, 2.347035)
distance_km = 20

get_buffer_box_geopandas(paris_point, distance_km)
# array([ 2.15209704, 48.73039383,  2.54099598, 48.98700028])

get_buffer_box_shapely(paris_point, distance_km)
# (2.152097043192064, 48.73039383319076, 2.540995981145973, 48.98700027642409)

在大多数用例中,使用圆形缓冲区和半径而不是方形缓冲区以及从中心到角落的距离可能更为实用。

Bounding box in this context can be bit confusing as there's only a single point object. Lets just call it a square buffer.
While Geopanadas is kind of overkill here, it conveniently wraps & provides all the functions you'd need. But for less dependencies you could use it's underlying libraries - Shapely & pyproj - directly as well.

import geopandas as gpd
import pyproj
from shapely.geometry import Point
from shapely.ops import transform
from math import sqrt

# with GeoPandas
def get_buffer_box_geopandas(point_lat_long, distance_km):
    # distance is d/2 of the square buffer around the point,
    # from center to corner;
    # find buffer width in meters
    buffer_width_m = (distance_km * 1000) / sqrt(2)
    (p_lat, p_long) = point_lat_long

    # Geopandas Geodataframe with a single point
    # EPSG:4326 sets Coordinate Reference System to WGS84 to match input
    wgs84_pt_gdf = gpd.GeoDataFrame(geometry = gpd.points_from_xy([p_long],[p_lat], crs='EPSG:4326'))
    
    # find suitable projected coordinate system for distance
    utm_crs = wgs84_pt_gdf.estimate_utm_crs()
    # reproject to UTM -> create square buffer (cap_style = 3) around point -> reproject back to WGS84
    wgs84_buffer = wgs84_pt_gdf.to_crs(utm_crs).buffer(buffer_width_m, cap_style=3).to_crs('EPSG:4326')
    # wgs84_buffer.bounds returns bounding box as pandas dataframe, 
    # .values[0] will extract first row as an array
    return wgs84_buffer.bounds.values[0]

# with Shapely & pyproj
def get_buffer_box_shapely(point_lat_long, distance_km):
    buffer_width_m = (distance_km * 1000) / sqrt(2)
    (p_lat, p_long) = point_lat_long

    # create Shapely Point object, coodrinates as x,y
    wgs84_pt = Point(p_long, p_lat)
    # set up projections WGS84 (lat/long coordinates) for input and 
    # UTM to measure distance
    # https://epsg.io/4326
    wgs84 = pyproj.CRS('EPSG:4326')
    # sample point in France - UTM zone 31N 
    # Between 0°E and 6°E, northern hemisphere between equator and 84°N
    # https://epsg.io/32631
    utm = pyproj.CRS('EPSG:32631')
    
    # transformers:
    project_wgs84_to_utm = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
    project_utm_to_wgs84 = pyproj.Transformer.from_crs(utm, wgs84, always_xy=True).transform

    # tranform Point to UTM
    utm_pt = transform(project_wgs84_to_utm, wgs84_pt)
    # create square buffer (cap_style = 3) around the Point
    utm_buffer = utm_pt.buffer(buffer_width_m, cap_style=3)
    # tranform buffer back to WGS84 and get bounds in lat / long 
    wgs84_bounds = transform(project_utm_to_wgs84, utm_buffer).bounds
    return wgs84_bounds

paris_point = (48.8588548, 2.347035)
distance_km = 20

get_buffer_box_geopandas(paris_point, distance_km)
# array([ 2.15209704, 48.73039383,  2.54099598, 48.98700028])

get_buffer_box_shapely(paris_point, distance_km)
# (2.152097043192064, 48.73039383319076, 2.540995981145973, 48.98700027642409)

For most use cases it's probably more practical to use a round buffer and radius instead of square buffer and distance from center to the corner.

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