0

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?

linusrg
  • 3
  • 1
  • If you remove the call to `Large_Sub_OSM_Footprint_Query` will it run for all iterations? (It will not write the file or do the request of course, but just to check) – ChatterOne Aug 18 '20 at 07:02
  • Just checked, yes it does. – linusrg Aug 18 '20 at 07:05
  • When you have the function enabled, does it write one file or two? If it writes only one file the issue is with the `sub` function, if it writes two it means that the first `for` loop is somehow wrong. But I'm willing to bet it writes only one file. – ChatterOne Aug 18 '20 at 07:23
  • Yes it only writes one file. The file is fine though, as it should be. The loop also continues (printing "1") after creating that one file... I don't see what could be wrong with the sub function – linusrg Aug 18 '20 at 08:06
  • I'd say just debug step by step with `pudb3` or maybe just put incremental prints after every line in the sub function to see where it stops – ChatterOne Aug 18 '20 at 10:58

1 Answers1

-2

Loops look fine, check elsewhere (maybe the Large_Sub_OSM_Footprint_Query is not handling issues properly?)