Lightweight tool for generating random coordinates for specific region / country?
Asked Answered
P

3

5

For now, I'm generating geo positions with only random module:

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

Obviously, this method can produce a dot somewhere in the ocean or about it. So I want to be able to specify some region (e.g. Asia) or even country and get the dot from that area.

Are there any tools/snippets for this?

Pleinair answered 7/12, 2017 at 10:20 Comment(3)
are you generating random positions on a sphere with uniform distributions ?!?Dele
@Stéphane yep. Is it the wrong way?Pleinair
Suppose you were to generate a random location in, say, Iceland. What would you want to be able to do with it?Germann
P
1

I'm answering my own question, and the answer is pretty simple, so I think it will work in the most of the cases. All you need to do is to:

  1. Download the databases of the cities (link, maxmind.com). It weights 33mb in .gz format and includes 3.173.959 cities with there names and coordinates.

  2. Extract the .txt file and shuffle the lines. It can be done with a simple command sort -R worldcitiespop.txt -o shuffled_cities.txt

  3. Select the random line and it's done! Here's a visualization for 100 randomly selected cities:

enter image description here

Pleinair answered 8/12, 2017 at 12:15 Comment(0)
I
5

Had to do a similar thing, but wanted to be really uniformly spread in Europe in this case.

import shapefile
from shapely.geometry import Point, shape
import numpy as np
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 = (np.random.uniform(min_x, max_X), np.random.uniform(min_y, max_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 

This gives you the samples you want. Below a plot of a 5000 point sample from Europe. To obtain one sample use

sample(EU)

Showcase

Individuate answered 5/6, 2018 at 15:5 Comment(4)
Could you please edit your code regarding what 'interval_x' and 'interval_y' mean?Harmony
Changed the line :)Individuate
could you also please add the code to plot the 5000 points as well? I am having trouble with the legend showing wrong countries. So would be very helpful if you could add the code for plotting the 5000 points as well.Harmony
anyone looking for the shp file, go to web.archive.org/web/20231220150759/https://thematicmapping.org/…Attire
E
4

I had a similar use case and since I did not find any library available, I developed this python library: PyCristoforo. Github link: https://github.com/AleNegrini/PyCristoforo

Version 1.0.0 only supports European countries, but I plan to release other countries soon.

Electrodynamometer answered 3/7, 2019 at 19:51 Comment(2)
Impressive. Can it be modified to get let's say n random coordinates on Indian ocean or Arctic ocean?Amplifier
I am currently not maintaining the project any more due to lack of time. However, you can fork it :)Electrodynamometer
P
1

I'm answering my own question, and the answer is pretty simple, so I think it will work in the most of the cases. All you need to do is to:

  1. Download the databases of the cities (link, maxmind.com). It weights 33mb in .gz format and includes 3.173.959 cities with there names and coordinates.

  2. Extract the .txt file and shuffle the lines. It can be done with a simple command sort -R worldcitiespop.txt -o shuffled_cities.txt

  3. Select the random line and it's done! Here's a visualization for 100 randomly selected cities:

enter image description here

Pleinair answered 8/12, 2017 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.