穿越一个国家的经纬度

2024-05-20 00:00:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有趣的项目。在

所以我有一个webapi,它接收两个参数,经度和纬度,它的响应是真是假,即在一个有中心(lat,long)和radiox(比如10英里)的圆圈中有一些资源。在

如果它的响应是真的,我必须再次调用它,直到它响应为False。在

如果它的回答是假的,我就不用再打了

当我得到错误的信息时,我必须改变(lat,long),这样我就可以在其他不同于前一个领域的资源进行搜索,直到我覆盖了一个国家的所有领土。 我想用python自动化它来覆盖所有的美国领土。我该怎么做?在

我想从圣地亚哥(美国左下角)出发,一直到西雅图或类似的地方。但是,我怎么知道美国领土的分界符(经纬度)。在

我不知道我是否正确地解释了我想做什么。如果没有,请告诉我,我会做得更好。在

谢谢你


Tags: 项目信息false参数错误资源中心long
1条回答
网友
1楼 · 发布于 2024-05-20 00:00:18

您可以使用geopy第三方模块上提供的vincenty distance函数。您必须使用pip install geopypypi安装geopy。在

下面是一个如何编写代码的示例:

from geopy.distance import vincenty
this_country = (latitude, longitude)
radius = 10  # 10 miles

while radius >= 0:
    other_country_within_circle_found = False
    # other_countries is a list of tuples which are lat & long
    # positions of other country eg. (-12.3456, 78.91011)
    for other_country in other_countries:
        # note: other_country = (latitude, longitude)
        if other_country == this_country:
            continue  # skip if other country is the same as this country.
        distance = vincenty(this_country, other_country).miles
        if distance <= radius:
            other_country_within_circle_found = True
            break
    if not other_country_within_circle_found:
        # the circle of this radius, have no other countries inside it.
        break
    radius -= 1  # reduce the circle radius by 1 mile.

有关更多信息,请参阅geopy文档:https://geopy.readthedocs.org/en/1.10.0/#module-geopy.distance

相关问题 更多 >