7

It's the first time I post in this community, I hope my question belongs here.

I'm currently developing an app where a user can add a location to his post, just like Instagram does (or Facebook). To do so, the user could either manually search for a location, or agrees to share his location so the app finds out where he is (geo-loc).

In the backend, for each post, I'll store the location name, location coordinates and a location ID.

I obviously thought about Google Maps in the first place, I then discover that Facebook has a Places API too. They both provide the data I need.

1.I was wondering if there's another API somewhere that could fit my requirements and that would be a bit more open and less intrusive regarding user privacy. I don't need a big precision, I believe only cities would be fine. I would also like to not put any money into that for now, as the app is a side project and might never generate any revenue.

2.Related question but maybe not for this stack exchange community: would it make sense to host a database of all cities myself (if that ever exists), or use an external service ?

Thanks

2
  • Please edit your question. You are essentially asking three things: (1) A repository where your app can store its data (2) A source of city location data and (3) Would it be wise to host such a source yourself? Comments: (1) Valid question, I don't know if it is has been asked here before; use the search box (2) Has been asked here several times, search the site for term like city, location, latitude. (3) Valid question, but a separate one. We like one question per question on SE sites, so make (3) a separate question, but maybe wait until (1) and (2) are more clear.
    – user4293
    Commented Aug 21, 2017 at 7:59
  • 1
    I'd start with geonames.org, and use one of their smaller data dumps (eg, all cities over 10,000 population or whatever). I do something like this at github.com/barrycarter/bcapps/tree/master/GEONAMES (it may or may not be helpful)
    – user3856
    Commented Aug 22, 2017 at 5:19

6 Answers 6

5

I suggest to try Geonames, which is a well-recognized open database of geolocalized places worldwide. Geonames links the name of places (from hotel to cities) to coordinates (longitude & latitude, in WGS84) as well as other information when available (population, zip code, ...). You can have a look of webservices based on geonames here.

Among the webservices, the FindNearby might suit your need:

Example here : http://api.geonames.org/findNearbyJSON?lat=49.6842&lng=5.8143&radius=10&username=demo

However, I don't know how to filter to output only "big cities" (and not the smallest populated place). An option is to make a query using a certain radius and then filter by population > your_threshold.

As stated here, do not use username=demo in production but create an account. For your second, related question, have a look at the terms of use of Geonames or contact them to check if you can use their services directly or if you have to implement them on a dedicated server.

1
  • Geonames has dumps that are easily accessible, including several dumps of cities with a certain threshold (> 500, > 1000, > 15000) which is what I ended up going for. download.geonames.org/export/dump/readme.txt
    – Link14
    Commented Mar 25, 2019 at 18:29
8

Why don't you use OSM's API?

A similar question was asked on gis: How to get a list of OSM relations (id numbers) of cities and countries only?

1
4

check (and collaborate!) at Data Packaged Core Datasets the specific datasets:

Both are related to https://github.com/datasets/country-codes

PS: about OSM and its APIs, see also http://nominatim.openstreetmap.org/


Others: specialized datasets or building a dataset from a "BigData of Datasets"

3

Check out citypopulation.de, a well-curated website of all the world's big citites and agglomerations.

The guy who maintains the site (I think it is only one person, Thomas Brinkhoff - see the Terms of Use) has put a lot of thought into how to draw borders around "the cities", on how to define accurate city limits and also estimate the populations living within wider metropolitan areas.

I don't think there is an API but hte data is suitable for screen-scraping, or donwloading in bulk.

Actually this type of data often needs to be downloaded only once (or rarely at best), so there is no need for a fast responsive API that can scale up to thousands of concurrent users.

3

I've developed an online REST service called GeoDB Cities that wraps the GeoNames data (and also leverages WikiData for translations). It should give you exactly what you need.

  1. You'll need to get the user's current GPS location (which you can do via the GeoLocation API supported by most modern browsers).

  2. Now just make a call to GeoDB to get the nearest city to that location (with a certain minimum population to exclude small towns). You can also optionally sort by largest cities first (if your use-case considers larger cities as more relevant).

For example, let's say you get the user's location as +33.915007-118.404626. Call the GeoDB Free tier to find the closest cities with at least 100,000 people and within 50 miles of these coordinates, sorted by population descending (largest cities first). Like this:

GET http://geodb-free-service.wirefreethought.com/v1/geo/locations/+33.915007-118.404626/nearbyCities?minPopulation=100000&types=CITY&radius=50&sort=-population&offset=0&limit=1

Now just take the first result, which should be Los Angeles.

NOTES:

  • Setting limit=1 ensures that only a single result is returned.
  • Setting types=CITY ensures that counties are filtered out. (Otherwise, you'll get back both types.)

Learn more about this use-case here:

http://geodb-cities-api.wirefreethought.com/guides/getting-user-current-city

And experiment here with a live demo:

http://geodb-cities-api.wirefreethought.com/api-reference/find-nearby-cities-for-location-api

The free instance lets you do up to 1 request/sec. If you need more, you can upgrade to one of the Pro tier plans.

Good luck!

0

I've recently been using here.com using the R library hereRand it seems to provide all the functionality I need, mainly geocoding text addresses and route finding between locations. Their 'Freemium' model allows up to 250k transactions per month.

Another, much more complex process would be setting up you own PostGIS server and loading the shape files for the area/country you need. I've done this for the US using the Census Brueau's TIGER/Line shapefiles and it works well once up and running but it can be complex process, especially if you are new to GIS. Also, I have no idea how it would work for areas outside the US.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.