This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

[Overpass-api] How to find a random city?

1

I'm in a process of making a game based on OSM data. I will have the player starting in a random city.

I have something that is kinda working. It is not producing a result every time, it is slow, and possibly stressful for the server:

node["place"~"village|town|city"] (around: 100000, -32.950,23.325); out 1;

I am generating a random coordinates and search for any node with place=village|town|city tag within 100 km. The problem is that some of the random coordinates appear in the ocean or in deserts where there are no cities in that radius. I tried increasing it, but the query is getting even slower (expected) and I'm not sure if it is not bad for the Overpass server.

Can you see any way to improve this query? Can you suggest entirely different method of finding a random city?

asked 02 Feb '16, 07:30

ivanatora's gravatar image

ivanatora
2.7k355568
accept rate: 7%

edited 02 Feb '16, 07:50


One Answer:

3

How about retrieving all cities of the world once, storing them in a list and choosing a random list element every time you need it? This seems to be a better approach since there isn't a new city appearing every 5 minutes.

answered 03 Feb '16, 08:14

scai's gravatar image

scai ♦
33.3k21309459
accept rate: 23%

1

This might be getting me to some place. I managed to filter world cities based on population between 100000 - 399999 and it seems there are only 800 cities matching this criteria. I did not mean to use the pop filter in the game, but it is okay way to reduce the size of the list that will be kept locally. http://overpass-turbo.eu/s/e8c

(02 Feb '16, 08:32) ivanatora

Source code available on GitHub .