问题描述
我尝试循环通过边界框并查询建筑物以填充GIS项目。 由于边界框很大,因此我编写了一个循环,将较大的边界框拆分为较小的边界框,这也产生了单独的GeoJSONS,可以将其分别导入GIS系统。
我的循环在第二次迭代时停止,如您所见,因为我插入了一些打印功能来定位错误:
import geojson
from geojson import Point,Polygon,Feature,FeatureCollection,dump
import osm2geojson
import requests
import math
api_url = "https://overpass.kumi.systems/api/interpreter" #I use my own Overpass instance,not to overload a public one,no worries!
#Set output folder
path = "C:\\osm_query_out"
#Create the folder,in case it does not exsist yet:
if not os.path.exists(path):
os.makedirs(path)
def Large_Sub_OSM_Footprint_Query(South,West,North,East,name):
overpass_url = f"{api_url}?data=[bbox];way[building];out;out;node(w);out;&bbox={South},{West},{North},{East}"
r = requests.get(overpass_url)
osm_xml = r.content
geojson = osm2geojson.xml2geojson(osm_xml,filter_used_refs=True,log_level='ERROR')
with open(f'{path}\part_{name}.geojson','w') as f:
dump(geojson,f)
def Large_OSM_Footprint_Query(South,East):
SNsteps = math.ceil((North - South)/0.5)
WEsteps = math.ceil((East - West)/0.5)
for SNstep in range(0,SNsteps):
print(SNstep)
South_Sub = (South + (SNstep*0.5))
North_Sub = (South + (SNstep*0.5) + 0.5)
print(South_Sub,North_Sub)
for WEstep in range(0,WEsteps):
print(WEstep)
name = 'N',str(SNstep),'E',str(WEstep)
West_Sub = (West + (WEstep*0.5))
East_Sub = (West + (WEstep*0.5) + 0.5)
Large_Sub_OSM_Footprint_Query(South_Sub,West_Sub,North_Sub,East_Sub,name)
print(West_Sub,East_Sub)
print(name,'of',SNsteps,WEsteps,'done')
运行代码:
South,West = 139.3172836303711,34.672464504652
North,East = 148.46491241455078,38.800272350556824
Large_OSM_Footprint_Query(South,East)
输出:
0
139.3172836303711 139.8172836303711
0
34.672464504652 35.172464504652
('N','0','0') of 19 9 done
1
我的嵌套循环缺少什么?
解决方法
循环看起来不错,请在其他地方检查(也许Large_Sub_OSM_Footprint_Query无法正确处理问题?)