70

This probably seems like a duplicate, since there are identical questions out there.

Unfortunately all of the answers to all of these questions are missing one detail: the states.

Where can I get a list of countries, states and cities?

For example, I want this:

| Sydney  |  New South Wales  |  Australia  |

or

|  Miami  |  Florida          |  United States  |

I don't want something like this, which excludes the state:

|  Miami  |  United States  |

Is there any place where I can get this kind of data?

I've already tried the following data sources:

But all of them exclude the states.

j0k
  • 22,600
  • 28
  • 79
  • 90
Jonathan
  • 32,202
  • 38
  • 137
  • 208

6 Answers6

74

The UN maintains a list of countries and "states" / regions for economic trade. That DB is available here: http://www.unece.org/cefact/locode/welcome.html

Eric
  • 764
  • 6
  • 2
  • Thanks Eric!! This is much closer to what I need. Their CSV files have a format like this: Country Code | ... | Locality | Locality/City | State. This isn't ideal, but it gives me the info I need. All I need to do is use a string splitting function to parse out the City name, then I've got what I want: Locality | City | State. Thanks a tonne! – Jonathan Dec 18 '13 at 00:51
  • Hi Jonathan, may I know what the url for this list? Somehow I could not find country, state, city data in the UN website. – Daryl Wong Sep 11 '18 at 02:24
  • Unfortunately, it doesn't contain state/region/admin_level when I downloaded the [csv data](http://www.unece.org/cefact/codesfortrade/codes_index.html) and checked it today. See [manual](https://www.unece.org/fileadmin/DAM/cefact/locode/Service/LocodeColumn.htm). – kiatng Jul 09 '19 at 06:19
  • 1
    @kiatngI just downloaded this data and find the state-level data is included. `"MY","01","Johor","State"` `"MY","02","Kedah","State"` – Arefe Nov 15 '19 at 14:48
34

geonames.org has an api and a data dump of worldwide geographical places.

j0k
  • 22,600
  • 28
  • 79
  • 90
Shane Andrade
  • 2,655
  • 17
  • 20
  • I can't find any list of Countries, States and Cities on this website. All I can find is a list of countries, a list of capital cities and capitals. – Jonathan Mar 09 '12 at 01:36
  • 7
    I think you're looking at the browse features of the site. Look at the data that you can export from them here http://download.geonames.org/export/dump/ – Shane Andrade Mar 09 '12 at 05:33
  • I looked through the list of files. There seems to be nothing there that simply lists countries and states. There are lists of cities, highly detailed lists of locations, lists of countries and country-specific lists which contain a grab-bag of location types including states and small localities (with nothing to indicate which items are states and which aren't; since some countries would call them 'State of' but other countries would call them something else, e.g. 'Province of'). This isn't very useful I'm afraid. – Jonathan Mar 09 '12 at 18:19
  • 2
    I believe what you are looking for is what they call the admin 1 zones and can be found in the admin1CodesASCII.txt file from that previous link. – Shane Andrade Mar 09 '12 at 18:24
  • So this is slightly better. At least I can see states per country. So I can get this: 'New South Wales | Australia'. Unfortunately this still doesn't give me what I wanted originally. I want this: 'Sydney | New South Wales | Australia'. I want the city AND state AND country, not just the state and country. Perhaps I should just be happy with this and match the cities up by state name, but then I'd need a list of cities and states, which I still don't have. – Jonathan Oct 02 '12 at 01:32
  • check this out http://vikku.info/programming/geodata/geonames-get-country-state-city-hierarchy.htm – Roy Lee Feb 07 '14 at 09:28
  • @Roylee when it can't even get my first world country's city of 200,000 people included in the list, it obviously isn't complete. I wouldn't recommend that data. – bcsteeve Oct 11 '15 at 16:47
  • I've been using an import of the Geonames "cities1000" database pretty heavily over the last year - this theoretically includes all cities in the world over 1000 in population, and also happens to include many with fewer people. I haven't hit upon any notable omissions yet; makes me wonder where bcsteeve lives. – Apollo Grace Jan 25 '18 at 17:53
  • Countries: https://www.geonames.org/countries/ - is this list not accurate? It seems to be quite exhaustive with 252 entries, although you might argue that not everything in that list is a country. – Savage Dec 19 '18 at 09:44
10

geonames is nice. an export tool based on geonames:

https://github.com/yosoyadri/GeoNames-XML-Builder

https://github.com/yosoyadri/GeoNames-XML-Builder/blob/master/continents-countries-statesprovinces.xml

there's also the excellent pycountry module:

http://pypi.python.org/pypi/pycountry

jspcal
  • 50,847
  • 7
  • 72
  • 76
5

Check this out! It was built no longer ago in 2014.

Get a list of country/state/city in a hierarchy using geonames webservice

Roy Lee
  • 10,572
  • 13
  • 60
  • 84
4

Geonames has a lot of data on places (including towns and cities) but it seems to be contributed and perhaps not complete.

Perhaps also try SQL Dumpster, I've used this website a lot for these kinds of databases, cities, provinces, etc. Unfortunately it's not free but only appears to be a one-time fee.

Sibiza
  • 41
  • 1
2

This may be a sideways answer, but if you download Virtuemart (A Joomla component), it has a countries table and all the related states all set up for you included in the installation SQL. They're called jos_virtuemart_countries and jos_virtuemart_states. It also includes the 2 and 3 character country codes. I'd attach it to my answer, but don't see a way of doing it.

GDP
  • 8,109
  • 6
  • 45
  • 82
  • Please upload a file to dropbox, to enable the download. It would be very helpful. – Philip Enc Jul 26 '20 at 22:25
  • @philip-enc I don't have the database I was referring to anymore, but you can get what you need at https://www.geodatasource.com/world-cities-database/free or just google "free world city state country database" – GDP Aug 10 '20 at 15:53
  • Thank for the response but I already spent a lot of time with free databases and I found bugs, duplicated entries.... Now I am searching for something to pay and solve the problem. I you have some advice, you are welcome. Thanks – Philip Enc Aug 11 '20 at 16:20