穿越一个国家的经纬度

Going through latitude and longitude of a country

我有一个有趣的项目。

所以我有一个 WEB API,它接收两个参数,经度和纬度,并且响应 true 或 false 是真的,在一个以(纬度,经度)和无线电 X(比方说 10 英里)。

如果它响应为真,我必须再次调用它直到它响应为假。

如果它响应为 false,我就不必再次调用它

当我得到 false 时,我必须更改 (lat,long),所以我在与前一个区域不同的其他区域中搜索这些资源,直到我覆盖了一个国家的所有领土。 我想用 python 使其自动化以覆盖例如所有美国领土。我该怎么做?

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

我不知道我是否没有正确解释我想做什么。如果没有,请告诉我,我会努力的。

谢谢

您可以使用 geopy 第三方模块上提供的 vincenty 距离函数。您必须使用 pip install geopy.

pypi 安装 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