如何在立交桥查询中连接字符串

How to concatenate strings in overpass query

我正在尝试编写一个函数,它将 latlon gps 坐标作为查询 overpy (openstreemap) 的变量。
这是我尝试过的:

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

调用函数:

query_overpass(40.74797, -73.81236)

很遗憾,它不起作用。

有人有任何提示如何管理吗?
谢谢

使用 f-string 格式化您的立交桥查询:

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)

输出:

<overpy.Way id=284487961 nodes=[2882270459, 2882270744, 2882270769, 2882270775, 2882270940, 2882270752, 2882270701, 2882270478, 2882270459]>