Retrieve cities list of a country
Asked Answered
C

4

6

Which is the best api to retrieve all the cities in given country? I tried downloading from geonames. but it seems data is not reliable.` Is it possible to query city, along with state and country in overpass api?

Can you please suggest a better way to download cities and its points?

Countenance answered 12/4, 2019 at 6:11 Comment(1)
If you have not done it already, I recommend searching and posting the question on the GIS site: gis.stackexchange.com. OSM also has its own help forum with similar questions like this one: help.openstreetmap.org/questions/13877/…Absence
D
2

I have done a similar task by making use of overpass api with the help of overpy which is a python package.

Firstly I have retrieved all states of country using the query

    [out:json];
    area["ISO3166-1"="IN"];
    (rel(area)["admin_level"="4"];);
    out;

Now I have fetched all districts for each state which is admin_level=5.

    [out:json];
    area["ISO3166-2"="IN-KL"];
    (rel(area)["admin_level"="5"];);
    out;

State ISO3166-2is from the data received from the API. Now you can fetch cities for each districts using the data from API.

    [out:json];
    area["ISO3166-2"="{0}"]["admin_level"="4"];
    (rel["name"="Thiruvananthapuram"](area);)->.center;
    node(around.center:10000)["place"];
    out;"""

This worked for me. Remember fetching all the cities in a country is a really huge task for the OSM Servers. Fetch the data that you really need.

Deductible answered 9/5, 2019 at 15:14 Comment(0)
R
1

if you are specific to the name of the cites or state or country then u can use this API. https://pypi.org/project/geosky/

This API is having a record of all countries, states, and cities. Here is the sample code...

from geosky import geo_plug

geo_plug.all_CountryNames()

geo_plug.all_Country_StateNames()

geo_plug.all_State_CityNames(name)# name == 'all' or state name
geo_plug.all_State_CityNames('Odisha')
Ruble answered 1/7, 2020 at 19:56 Comment(0)
L
1

If you are searching for APIs to get a list of countries and a list of cites of a particular country, then try https://github.com/shivammathur/countrycity

  1. You can simply GET https://shivammathur.com/countrycity/countries to get a list of all countries.
  2. https://shivammathur.com/countrycity/countries/ind to get only counties having "ind" in them.
  3. https://shivammathur.com/countrycity/cities/India to get all cities of India.
  4. You may face a CORS issue for the above-mentioned URLs for use in frontend applications, in that case, please try https://countrystatecity.in/ . For which you can get an API key in a day by filling up a google form.

Or if you are looking for JSON holding this data, please refer to: https://mcmap.net/q/1092104/-getting-list-of-countries-cities-and-their-areas-states-towns-respectively-closed

Lengthwise answered 2/11, 2021 at 14:34 Comment(0)
A
0

I will answer your second question regarding Overpass API. You can query cities for a particular country using that API. Just to demonstrate its capabilities, there is a web-based query tool for OSM called Overpass Turbo (http://overpass-turbo.eu) where you can submit queries and display and download data. The following query will provide all cities in the United States, for example:

{{geocodeArea:"United States"}}->.searchArea;
(
  node["place"="city"](area.searchArea);
);
out body;
>;
out skel qt;

The query will return the following:

enter image description here

Per OSM Wiki, the "city" tag is used "to identify the largest settlement or settlements within a territory, including national, state, and provincial capitals, and other major conurbations." On the same Wiki page, there is a note about differentiating between cities and towns. In the above query, "city" can be replaced with "town". Other possible values (including country and state) are listed here: https://wiki.openstreetmap.org/wiki/Key:place#Values

If you are looking to obtain city-to-state relationships from the data, you may not always get reliable results. For example, you may get the following detailed GeoJSON entry for Duluth, MN:

    {
      "type": "Feature",
      "properties": {
        "@id": "node/19188464",
        "is_in": "Minnesota USA",
        "is_in:continent": "North America",
        "is_in:country": "USA",
        "is_in:country_code": "US",
        "is_in:state": "Minnesota",
        "name": "Duluth",
        "name:ja": "ダルース",
        "name:oj": "Onigamiinsing",
        "name:ru": "Дулут",
        "place": "city",
        "population": "86265",
        "population:source": "2010",
        "wikidata": "Q485708",
        "wikipedia": "en:Duluth, Minnesota"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -92.1251218,
          46.7729322
        ]
      },
      "id": "node/19188464"
    },

but only the following entry for Madison, WI:

{
      "type": "Feature",
      "properties": {
        "@id": "node/29941752",
        "alt_name:ru": "ÐœÑдиÑон",
        "capital": "4",
        "is_in:continent": "North America",
        "is_in:country": "USA",
        "name": "Madison",
        "name:en": "Madison",
        "name:pl": "Madison",
        "name:ru": "МадиÑон",
        "name:ta": "மேடிசனà¯",
        "name:uk": "МедіÑон",
        "place": "city",
        "population": "243344",
        "state_capital": "yes",
        "website": "http://www.cityofmadison.com/",
        "wikidata": "Q43788",
        "wikipedia": "en:Madison, Wisconsin"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -89.3837613,
          43.074761
        ]
      },
      "id": "node/29941752"
    },
Absence answered 12/4, 2019 at 8:9 Comment(1)
One line to emphasize here is the one with > ... this so-called recursion makes the syntax pretty concise.Hitoshi

© 2022 - 2024 — McMap. All rights reserved.