I would like to query OpenStreetMap with a given longitude and latitude and check to see if it is in a body of water (i.e. if the location is in the ocean or in a lake, etc.). Is this possible? If so, can someone give me an example on how to do this?

My goal is to place markers on a map at random positions, however, I do not want the markers out in the middle of an ocean.

I am not trying to build a map with OSM. I just want to retrieve the data for use in another program.

I am very new to OpenStreetMap and would appreciate any help.

asked 18 Sep '14, 19:41

ponchoman1's gravatar image

accept rate: 0%

edited 18 Sep '14, 19:50


Do you need this for the whole planet ? Do you have some skills on postGIS database and queries ?

(19 Sep '14, 13:18) Pieren

Well, I am not sure how much these short answers may help “beginners” but at least as kind of guidelines should show the complexity of the issue. Note also that OSM is not a map but data used for creating maps (after complex preparation processing).
1. If you have a raster map you should detect the water colour code(s) from the stiles/decorations specification. Then, you should place your point(s) into the corresponding pixel(s) in the map and check the pixels colour code. If this is equal to the waters’ code, your point is in a water body. Of course, you may have many versions of this raster approach. As a rule, on large areas you need scales 1:4-5,000,000 or lower and the results are less accurate.
2. In a vector version you should have a vector-area data layers for the planet_sea (or planet_land), rivers, lakes, channels… (or a layer for union of these). Besides, you should have a function point-in-polygon (that returns info whether a point is inside, on the border or outside a polygon). There are many versions of this function with varying efficiency (you may find versions even in student books). The best ones are those close to hardware and consist of just a several lines of code. How to use this function is rather strait forward. Again, a tiled version of the water area data layers may radically increase the efficiency of you search. This, because you may apply a simple early filtering.
Here is an example, a case, similar to your issue for illustration. The river-line data contains roughly 250,000,000 (250 million) nodes. The river area objects around 53,000,000. Assume, you don’t want the river-line sections that are covered with the river areas (huge number of redundant points). If we run the search function for the nods on the large (none tiled) river objects it takes many, many hours on a powerful laptop. The same procedure on a tiled river objects takes only several (3-4) minutes.

permanent link

answered 21 Sep '14, 10:32

sanser's gravatar image

accept rate: 5%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 18 Sep '14, 19:41

question was seen: 4,262 times

last updated: 25 Sep '14, 10:51

powered by OSQA