问题描述
我正在读取一个县的 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 或任何其他包中获取此信息?
解决方法
total_bounds
返回一个元组,其中包含整个系列边界的 minx
、miny
、maxx
、maxy
值。
bounds
返回一个包含 minx
、miny
、maxx
、maxy
列值的 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
:
total_bounds
: