为特定国家生成随机坐标

Pra*_*pta 3 python gis geocoding postgis geodjango

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

我用过这个库Faker

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

Run Code Online (Sandbox Code Playgroud)

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

我尝试了一个简单的方法

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

Run Code Online (Sandbox Code Playgroud)

但特定国家/地区只有 10-20 个坐标。

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

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

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

有没有什么简单直接的办法。

我正在使用

POST GIS Database
GeoDjango Server
Run Code Online (Sandbox Code Playgroud)

笔记:

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

Mau*_*yer 5

您可以使用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")
Run Code Online (Sandbox Code Playgroud)

输出:

Got 102420 village coordinates!
(9.9436615, 77.8978759)
Run Code Online (Sandbox Code Playgroud)

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