Verify if a point is Land or Water in Google Maps

后端 未结 18 1676
不知归路
不知归路 2020-11-22 17:16

..and then Google-maps \"divide the waters from the waters\"

Well, not in the biblical sense but..

I would like to know what options I have in order to verif

相关标签:
18条回答
  • 2020-11-22 17:45

    I managed to get quite close by using the Google Elevation API. Here's an image of the results:

    screenshot of results

    You see the hexagons pretty much stay on land even though a rectangular perimeter is defined that goes partly over water. In this case I did a quick check from Google Maps itself and the minimum elevation on land was about 8-9m so that was my threshold. The code is mostly copy/pasted from Google documentation and Stack Overflow, here's the full gist:

    https://gist.github.com/dvas0004/fd541a0502528ebfb825

    0 讨论(0)
  • 2020-11-22 17:46

    Checkout this article. It accurately detects if something is on the water without needing a server. It's a hack that relies on the custom styling feature in Google Maps.

    0 讨论(0)
  • 2020-11-22 17:48

    This what I use and it is working not too bad... you can improve the test if you have more cpu to waste by adding pixels.

    function isItWatter($lat,$lng) {
    
        $GMAPStaticUrl = "https://maps.googleapis.com/maps/api/staticmap?center=".$lat.",".$lng."&size=40x40&maptype=roadmap&sensor=false&zoom=12&key=YOURAPIKEY";  
        //echo $GMAPStaticUrl;
        $chuid = curl_init();
        curl_setopt($chuid, CURLOPT_URL, $GMAPStaticUrl);   
        curl_setopt($chuid, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($chuid, CURLOPT_SSL_VERIFYPEER, FALSE);
        $data = trim(curl_exec($chuid));
        curl_close($chuid);
        $image = imagecreatefromstring($data);
    
        // this is for debug to print the image
        ob_start();
        imagepng($image);
        $contents =  ob_get_contents();
        ob_end_clean();
        echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
    
        // here is the test : I only test 3 pixels ( enough to avoid rivers ... )
        $hexaColor = imagecolorat($image,0,0);
        $color_tran = imagecolorsforindex($image, $hexaColor);
    
        $hexaColor2 = imagecolorat($image,0,1);
        $color_tran2 = imagecolorsforindex($image, $hexaColor2);
    
        $hexaColor3 = imagecolorat($image,0,2);
        $color_tran3 = imagecolorsforindex($image, $hexaColor3);
    
        $red = $color_tran['red'] + $color_tran2['red'] + $color_tran3['red'];
        $green = $color_tran['green'] + $color_tran2['green'] + $color_tran3['green'];
        $blue = $color_tran['blue'] + $color_tran2['blue'] + $color_tran3['blue'];
    
        imagedestroy($image);
        var_dump($red,$green,$blue);
        //int(492) int(570) int(660) 
        if($red == 492 && $green == 570 && $blue == 660)
            return 1;
        else
            return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 17:50

    Unfortunately this answer isn't within the Google Maps API and the referenced resource is not free, but there's a web service provided by DynamicGeometry that exposes an operation GetWaterOrLand which accepts a latitude/longitude pair (you can see a demo here).

    My understanding of how this is implemented is by using water body shape files. How exactly these shape files are used with the Google Maps API, but you might be able to get some insight from the linked demo.

    Hope that helps in some way.

    0 讨论(0)
  • 2020-11-22 17:50

    I have a different solution here. In current google map implementation, it does not calculate direction/distance from a water location to land location and vice versa. Why dont we use this logic to determine if the point is land or water.

    For example lets take this example

    if we want to determine, if a point x is land or water, then

    let us check the direction between point x and a known point y which is land. If it determines the direction/distance then point x is land or else it is water.

    0 讨论(0)
  • 2020-11-22 17:52

    In addition to the reverse geocoding -- as Dr Molle has pointed out, it may return ZERO_RESULTS -- you could use the Elevation service. If you get zero results by reverse geocoding, get the elevation of the location. Generally, the sea gets a negative number as the seabed is below sea level. There's a fully-worked example of the elevation service.

    Bear in mind that as Google don't make this information available any other method is just a guess and guesses are inherently inaccurate. However using the type returned by reverse geocoding, or the elevation if type is not available, will cover most eventualities.

    0 讨论(0)
提交回复
热议问题