geopandas :获取所有几何图形的 min 、 max lat 和 long

时间:2021-06-30 16:30:28

标签: python geopandas shapely

我正在读取一个县的 shapefile,需要提取所有几何图形的最小和最大坐标。似乎这对于 shapefile 中的每个单独的几何图形都是可能的,shapely 但不能跨越 shapefile 中的所有几何图形。

sf_shp = os.getcwd() + '/data/map/San_Mateo/SAN_MATEO_COUNTY_STREETS.shp'
sfgeodata = gpd.read_file(sf_shp)

sfgeodata.total_bounds <-- for bounding box. 

是否有属性或函数可以在 geopandas 或任何其他包中获取此信息?

1 个答案:

答案 0 :(得分:0)

total_bounds 返回一个元组,其中包含整个系列边界的 minxminymaxxmaxy 值。

bounds 返回一个包含 minxminymaxxmaxy 列值的 DataFrame,其中包含每个几何图形的边界。

——

import geopandas as gpd
from shapely.geometry import box
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.bounds
world.total_bounds

# bounds for individual geometries
poly_geom = world.bounds
b = poly_geom.apply(lambda row: box(row.minx, row.miny, row.maxx, row.maxy), axis=1)
boxes = gpd.GeoDataFrame(poly_geom, geometry=b)

# visualize
ax = world.plot()
boxes.boundary.plot(color='r', ax=ax)
plt.show()

# total bounds for all geometries
world_box = gpd.GeoSeries(box(*world.total_bounds))

# visualize
ax = world.plot()
world_box.boundary.plot(color='r', ax=ax)
plt.show()

bounds

enter image description here

total_bounds

enter image description here

相关问题