geopandas:获取所有几何图形的最小、最大纬度和长度

2024-10-01 17:41:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读一个县的形状文件,需要提取所有几何体的最小和最大坐标。这似乎可以通过shapely在shapefile中的每个单独几何体实现,但不能跨越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或任何其他软件包中是否有获取此信息的属性或函数


Tags: 文件mapdataossf形状shapefilesan
1条回答
网友
1楼 · 发布于 2024-10-01 17:41:18

^{}返回一个元组,该元组包含整个序列边界的minxminymaxxmaxy

^{}返回一个数据帧,其列minxminymaxxmaxy值包含每个几何体的边界

--

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

相关问题 更多 >

    热门问题