【问题标题】:How to download buildings data from OSM with a polygon (shapefile) as the bounding box?如何以多边形(shapefile)作为边界框从 OSM 下载建筑物数据?
【发布时间】:2021-05-11 00:43:38
【问题描述】:

我正在努力完成这项任务。我正在尝试 OSMnx,它可用于从上面的 OSM 下载数据,但是在尝试下载数据时使用其 from_polygon 功能时出现错误。另外我不确定这些数据是否包括建筑物数据。

我将 shapefile 加载到 geopandas 中,然后可以查看它并与之交互

这是代码

Building_data = ox.graph_from_polygon(my_shapefile, network_type='all')
ox.plot_graph(Building_data)

但是我得到了这个错误

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

编辑:所以我尝试改用 OSMnx 库:

import osmnx as ox
import shapefile
import geopandas
from shapely.geometry import shape

shp = shapefile.Reader('shapefile.shp')
feature = shp.shapeRecords()[0]
first = feature.shape.__geo_interface__

#convert to shapely shape
shp_geom = shape(first)

fprints = ox.geometries.geometries_from_polygon(shp_geom, {'buildings':True})

fprints.to_file('footprints.shp', driver='ESRI Shapefile')

但是,即使我在 OSMnx 中使用 shapefile,我仍然会收到错误消息:

CRSError: Invalid projection: +proj=utm +zone=80957 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +type=crs: (Internal Proj Error: proj_create: Error -35 (invalid UTM zone number))

有什么想法吗?

【问题讨论】:

  • graph_from_polygon() 中的第一个参数应该是 shapely Polygon,而不是整个 shapefile。
  • 我设法将 shapefile 更改为一个匀称的多边形并再次运行 OSMnx 脚本,但是现在我收到此错误 CRSError: Invalid projection: +proj=utm +zone=80957 +ellps=WGS84 +datum =WGS84 +units=m +no_defs +type=crs:(内部项目错误:proj_create:错误 -35(无效的 UTM 区域编号))。 @swatchai 有什么建议吗?
  • 显示您当前的代码。没有它,任何人都无法有效地提供帮助。
  • @swatchai 我编辑了原帖,谢谢!
  • polygon 必须使用坐标:lat,long;以度为单位。参考:help(ox.graph_from_polygon)

标签: gis openstreetmap geopandas osmnx


【解决方案1】:

我无法从 OSM 下载以多边形(shapefile)为边界框的建筑物数据,但是我可以使用以下代码使用距点的距离:

import osmnx as ox 
import ast

point = 'point coordinates'
dist = 'distance in m'
buildings = ox.geometries.geometries_from_point(point, {'building': True}, dist=dist)

并转换为地理数据框:

buildings_save = buildings.applymap(lambda x: str(x) if isinstance(x, list) else x)

然后我使用 geopandas 将建筑物数据裁剪到边界:

import geopandas as gpd

boundary = gpd.read_file('C:/boundary.shp')
buildings_final = gpd.clip(buildings_save, boundary)

绘制要检查的数据:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (15,12))
buildings_final.plot(ax = ax, color = 'red', edgecolor = 'black',)
plt.title('Buildings Data')
plt.show()

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 2011-07-20
    • 2013-04-30
    • 2021-05-11
    • 2018-06-11
    • 2021-12-03
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多