【问题标题】:Lightweight tool for generating random coordinates for specific region / country?为特定地区/国家生成随机坐标的轻量级工具?
【发布时间】:2018-05-21 10:08:30
【问题描述】:

目前,我仅使用 random 模块生成地理位置:

from random import uniform
geo_position =  (uniform(-90, 90), uniform(-180, 180))

显然,这种方法可以在海洋的某处或其附近产生一个点。所以我希望能够指定某个地区(例如亚洲)甚至国家并从该地区获取点。

是否有任何工具/sn-ps 用于此?

【问题讨论】:

  • 您是否在均匀分布的球体上生成随机位置?!?
  • @Stéphane 是的。是不是走错路了?
  • 假设您要在冰岛生成一个随机位置。你想用它做什么?

标签: python random coordinates geo


【解决方案1】:

我正在回答我自己的问题,答案很简单,所以我认为它适用于大多数情况。您需要做的就是:

  1. 下载城市数据库(link, maxmind.com)。它以 .gz 格式重达 33mb,包含 3.173.959 个城市的名称和坐标。

  2. 提取.txt 文件并随机播放行。可以通过一个简单的命令sort -R worldcitiespop.txt -o shuffled_cities.txt

  3. 来完成
  4. 选择随机线就完成了!这是 100 个随机选择的城市的可视化:

【讨论】:

    【解决方案2】:

    不得不做类似的事情,但在这种情况下希望在欧洲真正统一传播。

    import shapefile
    from shapely.geometry import Point, shape
    from numpy.random import uniform
    from collections import Counter 
    
    shp = shapefile.Reader('shapefiles/TM_WORLD_BORDERS-0.3.shp')
    # Adjust for your case:
    EU3 = ['ARM', 'BIH', 'BIH', 'CYP', 'DNK', 'IRL', 'AUT', 'EST', 'CZE', 'FIN' 
          , 'FRA', 'DEU', 'GRC', 'HRV', 'HUN', 'ISL', 'ITA', 'LTU', 'LVA', 'BLR'
          , 'MLT', 'BEL', 'AND', 'GIB', 'LUX', 'MCO', 'NLD', 'NOR', 'POL', 'PRT'
          , 'ROU', 'MDA', 'ESP', 'CHE', 'GBR', 'SRB', 'SWE', 'ALB', 'MKD', 'MNE'
          , 'SVK', 'SVN'] # 'TUR'
    EU = [(boundary, record) for boundary, record in 
                 zip(shp.shapes(), shp.records()) if record[2] in EU3]
    
    # Adjust the borders 
    count = Counter()  # small optimisation to check for big shapes first
    def sample(shapes, min_x=-11, max_x=26, min_y=37, max_y=71):
        while True:
            point = (uniform(interval_x), uniform(interval_y)) 
            for boundary, record in sorted(shapes, key=lambda x: -count[x[1][2]]):
                if Point(point).within(shape(boundary)): 
                    count[record[2]] += 1
                    return point 
    

    这将为您提供所需的样本。下面是来自欧洲的 5000 点样本图。获取一个样本使用

    sample(EU)
    

    【讨论】:

      【解决方案3】:

      我有一个类似的用例,因为我没有找到任何可用的库,所以我开发了这个 python 库:PyCristoforo。 Github链接:https://github.com/AleNegrini/PyCristoforo

      版本 1.0.0 仅支持欧洲国家,但我计划很快发布其他国家。

      【讨论】:

        猜你喜欢
        • 2020-12-09
        • 2019-07-02
        • 1970-01-01
        • 2021-06-05
        • 2015-10-07
        • 2018-10-27
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多