Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Search within distance" API for OpenStreetMap [closed]

Is there any API for OpenStreetMap which allows me to obtain a list of POIs within a certain distance (e.g. 10 miles) from a reference location?

like image 393
Robert Mircea Avatar asked Sep 05 '09 22:09

Robert Mircea


People also ask

Does OpenStreetMap have an API?

OpenStreetMap has an editing API for fetching and saving raw geodata from/to the OpenStreetMap database — this is the entry page for the documentation.

Is OSM API free?

The OpenStreetMap API is free.

How do I replace Google Maps with OpenStreetMap?

Go to OpenStreetMap.org. Search for the location you want as the centre of the map, or use the arrow to Show My Location. Select the desired layers, such as Standard, Cycle Map, Transport etc. Zoom and pan as necessary, using the + and - icons and your mouse.

How do I add a point in OpenStreetMap?

You can add a point to the map by clicking on the button named “Point” at the top the window. Then click on the map where you would like to add a new point. Select from the menu on the left what type of location it is that you are adding to the map.


2 Answers

As far as I know, the API doesn't support this directly. The simplest approach is to select a (quasi-)rectangular bounding box that your circle fits into, and use this to retrieve your POIs. Then you can do a distance calculation to each point of interest, and discard those that exceed your radius. This will remove the small fraction of POIs that lie close to the corners of the box, and so aren't within your circle. You want to do it in this order so that you only have to do the distance calculation on a relatively small number of target locations.

Don't forget that the bounding box is defined by lat/long corners, so it isn't truly rectangular. Longitude lines converge at the poles, so the top of your box is not the same width as the bottom. How much this affects you depends on how close you are to a pole (a degree of long ~= (40000km / 360) * cos (lat)).

If you don't require supreme accuracy, then you calculate your distances using Pythagoras's theorem, remembering the cosine variation in longitude, and the factor 2 unit difference (360 degrees of longitude, but only 180 of latitude). If you do require accuracy, then you're into the realms of spherical trigonometry, and also need to consider the ellipsoidal earth. Here's an online calculator, complete with equations and open source code, that is helpful in this regard.

like image 161
ire_and_curses Avatar answered Oct 13 '22 14:10

ire_and_curses


Yes. The Overpass API has an "Around" feature which does exactly this (search for items within a radius of the given point). You can combine that with other requirements (eg to get a list of a particular item type):

http://wiki.openstreetmap.org/wiki/Overpass_API#Around

For example using OverPass Turbo Api (List all towns near of "Manzanares, Spain" with latitude and longitude with a radius of 150 Km, try it live!):

<osm-script output="json" timeout="25">
  <id-query {{nominatimArea:Spain}} into="area"/>
  <query type="node">
    <has-kv k="place" modv="" v="town"/>
  <around lat="38.996507" lon="-3.371946" radius="150000"/>
</query>

  <print e="" from="_" geometry="skeleton" limit="" mode="body" n="" order="id" s="" w=""/>
</osm-script>
like image 20
Dan Stowell Avatar answered Oct 13 '22 12:10

Dan Stowell