I try to loop trough a bounding box and query buildings to populate a GIS project. Since the bounding box is quite large, I wrote a loop to split up the big bounding box into smaller ones which also result in separate GeoJSONS which I can import separately into a GIS system.
My loop stops on the second iteration, as you can see since I inserted some print functions to locate the error:
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, West, North, 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')
Running the code:
South, West = 139.3172836303711,34.672464504652
North, East = 148.46491241455078,38.800272350556824
Large_OSM_Footprint_Query(South, West, North, East)
Output:
0
139.3172836303711 139.8172836303711
0
34.672464504652 35.172464504652
('N', '0', 'E', '0') of 19 9 done
1
What am I missing with my nested loops?