31

mysql、ポリゴンの幾何学的データ型に関する典型的な質問があります。

緯度と経度の配列の形式でポリゴンデータがあります。例:

[["x":37.628134,  "y":-77.458334],
["x":37.629867,   "y":-77.449021],
["x":37.62324,    "y":-77.445416],
["x":37.622424,   "y":-77.457819]]

そして、緯度と経度の座標を持つポイント(頂点)があります。例:

$location = new vertex($_GET["longitude"], $_GET["latitude"]);

今、この頂点 (ポイント) がポリゴンの内側にあるかどうかを調べたいと思います。どうすればphpでこれを行うことができますか?

4

8 に答える 8

55

これは、別の言語から PHP に変換した関数です。

$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424);    // x-coordinates of the vertices of the polygon
$vertices_y = array(-77.458334,-77.449021,-77.445416,-77.457819); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x) - 1;  // number vertices - zero-based array
$longitude_x = $_GET["longitude"];  // x-coordinate of the point to test
$latitude_y = $_GET["latitude"];    // y-coordinate of the point to test

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";


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

追加: その他の機能については、こちらから入手できる polygon.php クラスを使用することをお勧めします。頂点を使用してクラスを作成isInsideし、テストポイントを入力として関数を呼び出して、別の関数で問題を解決します。

于 2011-02-21T11:09:38.823 に答える
15

上記の一般的な回答にはタイプミスが含まれています。他の場所では、このコードはクリーンアップされています。修正されたコードは次のとおりです。

<?php
/**
  From: http://www.daniweb.com/web-development/php/threads/366489
  Also see http://en.wikipedia.org/wiki/Point_in_polygon
*/
$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424); // x-coordinates of the vertices of the polygon
$vertices_y = array(-77.458334,-77.449021,-77.445416,-77.457819); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x); // number vertices
$longitude_x = $_GET["longitude"]; // x-coordinate of the point to test
$latitude_y = $_GET["latitude"]; // y-coordinate of the point to test
//// For testing.  This point lies inside the test polygon.
// $longitude_x = 37.62850;
// $latitude_y = -77.4499;

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";


function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
  $i = $j = $c = 0;
  for ($i = 0, $j = $points_polygon-1 ; $i < $points_polygon; $j = $i++) {
    if ( (($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
    ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i]) ) ) 
        $c = !$c;
  }
  return $c;
}
?>
于 2012-05-15T00:11:24.603 に答える
5

ポリゴンが自己閉鎖している場合、つまり、最後の頂点が最後のポイントと最初のポイントの間の線である場合、変数と条件をループに追加して、最後の頂点を処理する必要があります。また、頂点の数をポイントの数と同じとして渡す必要があります。

自己閉鎖ポリゴンに対処するために修正された受け入れられた回答は次のとおりです。

$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424);    // x-coordinates of the vertices of the polygon
$vertices_y = array(-77.458334,-77.449021,-77.445416,-77.457819); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x);  // number vertices = number of points in a self-closing polygon
$longitude_x = $_GET["longitude"];  // x-coordinate of the point to test
$latitude_y = $_GET["latitude"];    // y-coordinate of the point to test

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";


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;
}

ありがとうございました!私はこのページを見つけました。受け入れられた回答は非常に役に立ち、このバリエーションを提供できることを誇りに思います。

于 2013-10-04T16:09:43.413 に答える
3

考えられるアルゴリズムは次のとおりです。

  1. 関心のあるポイントを中心にして、新しい座標系を定義します。
  2. 新しい座標系で、すべてのポリゴン頂点を極座標に変換します。
  3. 角度の正味の変化Δθを追跡しながら、ポリゴンをトラバースします。角度を変更するたびに、常に可能な限り小さい値を使用してください。
  4. ポリゴンをトラバースした後、合計∆θが0の場合、ポリゴンの外側にいます。一方、±2πの場合は、内部にいます。
  5. 偶然に∆θ>2πまたは∆θ&lt;-2πの場合、それは、それ自体が2倍になるポリゴンがあることを意味します。

コードの記述は演習として残されています。:)

于 2011-02-21T11:00:10.177 に答える