Min
Posted on February 19, 2020
Why might you want location data for cities and towns?
Have you ever wanted to geocode some address data, but only to the city/town level - say, for privacy reasons or entity resolution purposes?
Or maybe you just need to plot out all the major cities around the world on a map?
There is no easy way of obtaining such data.
You could try:
- Google (or some other commercial) geocoding APIs on your lists of city names and towns, but that will probably cost you money.
- Open Street Maps (and APIs that support using OSM), but this isn't easy. Especially if you have a bulk data you are trying to geocode.
WikiData has a very good source.
OSM is not the only open data available. WikiData also has quite a bit of such data.
This is how you would obtain the locations of cities and towns around the world.
Go to https://query.wikidata.org
and use the following sparql query:
SELECT DISTINCT ?citytownLabel ?countryLabel ?loc
WHERE {
?citytown wdt:P31/wdt:P279* wd:Q7930989 .
?citytown wdt:P625 ?loc .
OPTIONAL { ?citytown wdt:P17 ?country } .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
After about a minute, you should get something like:
You can simply download the data as csv or json.
You can also take a look by displaying the data on a map (there should be a dropdown on the left.
Interesting to see which communities are more active in open data contribution.
And if you are interested in contributing the location data, or are a school teacher who can make such "data contribution" a geography class project, then the following query will give you a list of cities and towns without a location data, and the link to their wikidata pages.
SELECT DISTINCT ?citytown ?citytownLabel ?countryLabel ?loc
WHERE {
?citytown wdt:P31/wdt:P279* wd:Q7930989 .
?citytown wdt:P17 ?country .
FILTER NOT EXISTS { ?citytown wdt:P625 ?loc }.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
You will also realise that the towns that do not have location data tends to be missing a lot of other data too, like their English labels.
WikiData and the community of contributors are amazing.
Posted on February 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.