【问题标题】:Generating Random Cordinates for Specific Country为特定国家生成随机坐标
【发布时间】:2020-12-09 17:20:00
【问题描述】:

我正在尝试为一个国家/地区生成随机坐标

我用过这个库Faker

def geo_point():
    """make random cordinates"""
    faker = factory.Faker('local_latlng', country_code = 'IN')
    coords = faker.generate()
    return (coords[1], coords[0])

但问题在于,它的坐标集非常有限,大约在 30-40 之间,我们至少需要 10,000 个坐标来进行测试。

我尝试了一个简单的方法

def random_geo_cordinate():
    """make random geocordinates"""
    x, y = uniform(-180,180), uniform(-90, 90)
    return (y, x)

但是特定国家只有 10-20 个坐标。

我发现有很多参考资料可以通过 shape_files 生成,但在所有参考资料中,只有 geom 参数可用。

我找到了一种方法,可以通过 Geom 列检查这些坐标是否位于该国家/地区。

但是在为一个国家生成随机坐标时仍然缺少一些东西。

有没有简单直接的方法。

正在使用

POST GIS Database
GeoDjango Server

注意:

  1. 我使用 GDAL 获取国家/地区的 shapefile

【问题讨论】:

    标签: python gis postgis geocoding geodjango


    【解决方案1】:

    您可以使用Overpass API,它查询 OSM 数据库,因此您可以获得真实坐标。
    例如获取印度的所有村庄:

    import requests
    import json
    
    overpass_url = "http://overpass-api.de/api/interpreter"
    overpass_query = """
    [out:json];area[name="India"];(node[place="village"](area););out;
    """
    
    response = requests.get(
        overpass_url, 
        params={'data': overpass_query}
    )
    
    coords = []
    if response.status_code == 200:
        data = response.json()
        places = data.get('elements', [])
        for place in places:
            coords.append((place['lat'], place['lon']))
         print ("Got %s village coordinates!" % len(coords))
         print (coords[0])
    else:
         print("Error")
    

    输出:

    Got 102420 village coordinates!
    (9.9436615, 77.8978759)
    

    注意: Overpass API 是速率限制的,因此您应该将所有坐标保存在本地并从那里提取您的随机集!
    此外,您可以使用仅获取城市或城镇的地点参数,或获取特定地区的餐厅位置,...

    【讨论】:

      【解决方案2】:

      https://3geonames.org/randomland.IN 是一个免费的 API,它返回世界任何国家/地区的随机位置。

      【讨论】:

        猜你喜欢
        • 2018-05-21
        • 2019-07-02
        • 2015-10-07
        • 2018-10-27
        • 1970-01-01
        • 2021-10-03
        • 2013-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多