首页 > 解决方案 > 循环坐标列表并将坐标列表中的位置增加一以执行请求

问题描述

我正在尝试遍历坐标列表以生成路线,我不能一次使用所有这些路线,因为我需要单独生成某些值。

所以我的坐标列表被定义为“坐标”,看起来像这样:

[[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001], [14.21055347, 47.5674345], [16.30826472, 48.13786834], [15.92248574, 47.65823679], [14.21055347, 47.5674345], [16.40964033, 48.18617251], [16.393805666666665, 48.26835166666667], [16.393009142857142, 48.26821028571428], [15.92248574, 47.65823679], [14.21055347, 47.5674345], [16.40964033, 48.18617251], [16.19483273, 47.75397909], [14.21055347, 47.5674345], [13.717176157894738, 48.01453281578947]]

调用路由数据的代码是:

body = {"coordinates":coords,"attributes":["percentage"],"extra_info":["waycategory"]}

headers = {
    'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
    'Content-Type': 'application/geo+json; charset=utf-8'
}
call = requests.post('http://127.0.0.1:8080/ors/v2/directions/driving-hgv/geojson', json=body, headers=headers)

weiter = call.text

data = json.loads(weiter)

上面的代码将获取整个坐标列表并请求路由并生成数据。我实际上需要为每个坐标对(坐标[x,x+1])生成路线和打印数据。所以我会生成以下请求:

[[14.21055347, 47.5674345], [16.39356558, 48.17711001]], [[16.39356558, 48.17711001], [14.21055347, 47.5674345]], etc.

所以我的猜测是有一个循环像这样遍历身体:

i = 0

while i < len(coords):
    
    body = {"coordinates":coords[int(i),int(i+1)],"attributes":["percentage"],"extra_info":["waycategory"]}

    headers = {
    'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
    'Content-Type': 'application/geo+json; charset=utf-8'
    }
    call = requests.post('http://127.0.0.1:8080/ors/v2/directions/driving-hgv/geojson', json=body, headers=headers)

    weiter = call.text

    data = json.loads(weiter)

    print(data)

我尝试过的这种方法和其他方法都行不通-如果您对我如何进行操作有建议,我将非常感激!

标签: pythonloopsiterationopenrouteservice

解决方案


推荐阅读