18

I have a system where users put some coordinates (latitude/longitude), I need to check if specified coordinates are in the sea or not. Is there any service that could give me the answer. Or is it possible to do this using google maps.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Tornike
  • 1,245
  • 12
  • 35

3 Answers3

19

You can use the Google Maps Geocode API.

If your address is in land, the result_type of the response will be something like "administrative_area". if you are in the sea, the response will be "natural_feature".

Here are two examples:

Edit: Some more examples in response to comments:

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
  • 2
    +1 Very nice! Only one potential snag comes to mind: Sea areas claimed by a country or within the 12 mile zone - will they show up as "Administrative area" or as a "Natural feature"? That would be worth testing. – Pekka Sep 05 '10 at 10:15
  • 1
    I edited my answer to add a test within the UK 12 miles zone. Shows as natural feature. – Vivien Barousse Sep 05 '10 at 10:20
  • 3
    +1 Google thinks of **everything**. – BoltClock Sep 05 '10 at 10:22
  • @VivienBarousse here is a coordinate that is on the sea and it doesn't appear as natural_feature ... http://maps.google.com/maps/api/geocode/xml?address=43.8068533,28.585595&sensor=false and I can give you manny more! – opc0de Jun 21 '12 at 10:59
  • @opc0de: This point is located in a harbor in Romania. You are pointing to a corner case that is really difficult to solve: how do you make the difference between sea and harbor? This raises the question of what you consider being sea and what you consider being land. What about harbors, rivers, lakes? If you need that kind of precision, Google Maps is probably not the tool you should use for the job. – Vivien Barousse Jun 21 '12 at 12:58
  • yeah but sometimes you need exactly that precision. great answer tough probably the unique method at this time +1 ! – opc0de Jun 21 '12 at 18:31
  • 1
    @VivienBarousse This answer is in need of updating. Using the `0,0` point returns `ZERO_RESULTS`, in both the API and the Service. – bcdan Jul 13 '15 at 13:39
16

I have thought about this once before.

I would not use a web service. but I would use a pretty simple image. alt text

This image is taken from http://www.vectorworldmap.com/vectormaps/vector-world-map-v2.2-blank.jpg

EDIT from a great comment

I like the solution, but I'm not sure that map is totally accurate or equirectangular (which would be the easiest to work with). How about this one? naturalearth.springercarto.com/ne3_data/8192/masks/water_8k.png

//END

  • I would map lat/long over this map for know points so you know your map is correct.
  • For any give point, I would locate the pixel location on this map and figure out the colour (php can do this).
  • If the colour is white, I would say it is sea, black = land.
  • if you dont want lakes, open the map up and color all the lakes black.

If you need super high res maps, I would get a very large map and divide it up in a grid of smaller images that you can load.

If you need to make this system super duper fast. I would can this image pixel for pixel and create a database of this stuff so you can look it up pretty quick. But already this image below actually acts as an excellent database that can be easily checked or changed.

Did you want oceans and lakes?

Curious to know where you are going with this, as I like this problem.

John.

John Ballinger
  • 7,380
  • 5
  • 41
  • 51
  • 5
    +1 clever solution. I learned something today – Sandeepan Nath Sep 05 '10 at 10:13
  • thanks! appreciated. damn google, I would have got accepted answer. Mine is still pretty good for other things. And you could put in other sorts of features pretty easily. – John Ballinger Sep 05 '10 at 10:32
  • 3
    Clever! But you need to take the logo off first :D – Michael Clerx Sep 05 '10 at 11:16
  • 1
    Yeah, logos are the spice off life. It would end up being a new question on stackoverflow. "My geo-locating system is not working" - take logo off.... – John Ballinger Sep 05 '10 at 11:35
  • 1
    I like the solution, but I'm not sure that map is totally accurate or equirectangular (which would be the easiest to work with). How about this one? http://naturalearth.springercarto.com/ne3_data/8192/masks/water_8k.png – Fractaly Jul 15 '15 at 01:30
8

Expanding on John Ballinger, i have successfully used this trick:

  • Get a 1x1 pixel google image static map centered on your coordinates
  • Style map to black (land) and white (water) via Google Maps API styling
  • Find whether the pixel is black or white

In php:

$lat = your latitude
$lon = your longitude
$im = imagecreatefrompng('http://maps.googleapis.com/maps/api/staticmap?center='.$lat.','.$lon.'&zoom=21&format=png&sensor=false&size=1x1&maptype=roadmap&style=feature:administrative|visibility:off&style=feature:landscape|color:0x000000&style=feature:water|color:0xffffff&style=feature:road|visibility:off&style=feature:transit|visibility:off&style=feature:poi|visibility:off');
//get pixel color, put it in an array
$color_index = imagecolorat($im, 0, 0);
$color_tran = imagecolorsforindex($im, $color_index);

//if, for example, red value of the pixel is 0 we are on land
if($color_tran['red']==0){
    //do your thing
    echo "we are on land";
}
Emanuele Cipolla
  • 301
  • 3
  • 16
tctc
  • 81
  • 1
  • 1