为特定国家生成随机坐标 [英] Generating Random Cordinates for Specific Country

查看:107
本文介绍了为特定国家生成随机坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am Trying to Generate Random Coordinates for a Country

我使用了这个库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 个用于测试.

But the problem in this is, it has a very limited set of coordinates around 30-40 we require at least 10,000 for testing.

我尝试了一个简单的方法

I tried a simple approach

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

但随后只有 10-20 个特定国家的坐标出现.

But then only 10-20 coordinates for Specific Country Comes.

我发现有很多引用可以通过 shape_files 生成,但在所有引用中,只有 geom 参数可用.

There were a lot of references I found that through shape_files we can generate but in all of them only geom parameters are only available.

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

I found a method through which I can check that these coordinates lie in that country or not via the Geom column.

但是在为一个国家/地区生成随机坐标时我仍然缺少一些东西.

But am still missing something in generating random coordinates for a country.

有没有简单直接的方法.

Is there any simple and direct approach.

正在使用

POST GIS Database
GeoDjango Server

注意:

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

推荐答案

你可以使用 Overpass API,查询 OSM 数据库,从而得到真实坐标.
例如获取印度的所有村庄:

You could use Overpass API, which queries the OSM database, so you get real coordinates.
For example fetching all villages in India:

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 是有速率限制的,所以你应该在本地保存所有坐标并从那里提取你的随机集!
此外,您可以使用仅获取城市或城镇的地点参数,或获取特定地区的餐厅位置,...

Note: Overpass API is rate limited, so you should save the all coordinates locally and extract your random set from there!
Additionally, you can play around with places parameter fetching just cities or towns, or fetch restaurant locations for a specific district, ...

这篇关于为特定国家生成随机坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆