Find Point in polygon PHP

后端 未结 8 862
半阙折子戏
半阙折子戏 2020-11-30 19:34

i have a typical question with the Geometric datatype of mysql, polygon.

I have the polygon data, in the form of an array of latitudes and longitudes, ex:

         


        
8条回答
  •  佛祖请我去吃肉
    2020-11-30 20:18

    I have created code in php codeigniter, in my controller i have create two functions like below

    public function checkLatLng(){
        $vertices_y = array(22.774,22.174,22.466,22.666,22.966,22.321);    // x-coordinates of the vertices of the polygon (LATITUDES)
        $vertices_x = array(70.190,70.090,77.118,77.618,77.418,77.757); // y-coordinates of the vertices of the polygon (LONGITUDES)
        $points_polygon = count($vertices_x)-1; 
        $longitude_x = $this->input->get("longitude");  // Your Longitude
        $latitude_y = $this->input->get("latitude");    // Your Latitude
        if ($this->is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
            echo "Is in polygon!";
        }
        else
            echo "Is not in polygon";
    }
    

    Another function for check the lat-lng is below

    public function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y){
        $i = $j = $c = $point = 0;
        for ($i = 0, $j = $points_polygon ; $i < $points_polygon; $j = $i++) {
            $point = $i;
            if( $point == $points_polygon )
                $point = 0;
            if ( (($vertices_y[$point]  >  $latitude_y != ($vertices_y[$j] > $latitude_y)) && ($longitude_x < ($vertices_x[$j] - $vertices_x[$point]) * ($latitude_y - $vertices_y[$point]) / ($vertices_y[$j] - $vertices_y[$point]) + $vertices_x[$point]) ) )
                $c = !$c;
        }
        return $c;
    }
    

    For your testing purpose i passed below things

    latitude=22.808059

    longitude=77.522014

    My Polygon

提交回复
热议问题