如何使用python获得给定点(lat, long)和给定距离(int或float)的bounding-box (minx, miny, maxx, max)元组?
给定的距离是我正在寻找的边界框对角线的一半。
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为西南角经度
miny为西南角的纬度
maxx为东北角经度
max为东北角的纬度
我试过了,但我什么也没找到…有lib可以做这个吗?
你能帮我一下吗?由于边界框在这种情况下可能有点混乱,因为只有一个点对象。我们就叫它方形缓冲吧。虽然Geopanadas在这里有点矫枉过正,但它很方便地包装了&提供您需要的所有功能。但是对于较少的依赖,你可以使用它的底层库——Shapely &
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)
对于大多数用例,使用圆形缓冲区和半径可能比使用正方形缓冲区和从中心到角落的距离更实用。