0

I am trying to write a function that takes lat and lon gps coordinates as variables for a query to overpy (openstreemap).
Here is what I have tried:

def query_overpass(lat, lon):
    
    import overpy
    api = overpy.Overpass()

    result = api.query("""
    way(around:5,""" + lat + """ , """ + long + """)
      ['highway' !~ 'elevator']
      ['highway' !~ 'bus_guideway']
      ['highway' !~ 'footway']
      ['highway' !~ 'cycleway']
      ['foot' !~ 'no']
      ['access' !~ 'private']
      ['access' !~ 'no'];
    (
      ._;
      >;
    );
    out;""")
    
    return result

Call the function:

query_overpass(40.74797, -73.81236)

Unfortunately it does not work.

Does someone have any hints how to manage that ?
Thanks

Kyv
  • 615
  • 6
  • 26

1 Answers1

1

Use an f-string to format your overpass query:

import overpy

def query_overpass(lat, lon):    
    api = overpy.Overpass()

    result = api.query(
        f"""
        way(around:5,{lat},{lon})
        ['highway' !~ 'elevator']
        ['highway' !~ 'bus_guideway']
        ['highway' !~ 'footway']
        ['highway' !~ 'cycleway']
        ['foot' !~ 'no']
        ['access' !~ 'private']
        ['access' !~ 'no'];
        (
          ._;
          >;
        );
        out;
      """
    )
    return result


res = query_overpass(40.74797, -73.81236)
for way in res.ways:
    print(way)

Out:

<overpy.Way id=284487961 nodes=[2882270459, 2882270744, 2882270769, 2882270775, 2882270940, 2882270752, 2882270701, 2882270478, 2882270459]>
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47