0

我的数据库中的一个表包含许多邮政地址,这些地址也有经度和纬度信息,如下所示:

姓名----街道地址--------邮政编码----经度----纬度

弗雷德----君主街11号----- 4114 ---------- 57.317715 ---- 10.154355

巴尼 -- 4 Reign Street -------- 4114 ------------ 56.151112 ---- 10.087925

我有一个给定的区域(通过谷歌地图的 kml 坐标),定义如下:

        12.548740,55.694469,0.000000
        12.541320,55.687840,0.000000
        12.537410,55.690552,0.000000
        12.535310,55.694641,0.000000
        12.534499,55.695293,0.000000
        12.535787,55.696625,0.000000
        12.538100,55.696911,0.000000
        12.543890,55.697659,0.000000
        12.548740,55.694469,0.000000

我使用什么 mysql 查询来查找位于该区域内的地址?

4

1 回答 1

0

我不知道任何 MySQL 查询。

在这个答案中,我使用 PHP 函数来查找一个点是否在多边形中(4 点)。该函数基于Darel Rex Finley的C 代码Point in Polygon

您可以将它用于您的 8 点多边形。

$polySides  = 8; //how many corners the polygon has
$polyX    =  array(12.548740,12.541320,12.537410,12.535310,12.534499,12.535787,12.538100,12.543890,12.548740);//horizontal coordinates of corners
$polyY    =  array(55.694469,55.687840,55.690552,55.694641,55.695293,55.696625,55.696911,55.697659,55.694469);//vertical coordinates of corners
$x = 12.548740; 
$y = 55.694469;//Inside
//$y = 12.528740;//Outside

function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
  $j = $polySides-1 ;
  $oddNodes = 0;
  for ($i=0; $i<$polySides; $i++) {
    if ($polyY[$i]<$y && $polyY[$j]>=$y 
 ||  $polyY[$j]<$y && $polyY[$i]>=$y) {
    if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x)    {
    $oddNodes=!$oddNodes; }}
   $j=$i; }

  return $oddNodes; }


 if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";

您可以循环插入 lat,lng 到函数中的数据库。

如果数据库很大,您可以创建一个带有 north_west 和 south_east 坐标的边界框,以限制函数过滤的数据。

$sql = "SELECT lat,lng FROM `table` WHERE (lng BETWEEN '$west_lng' AND '$east_lng') AND (lat BETWEEN '$north_lat' AND '$south_lat')";

另一种解决方案是使用mysql空间扩展

于 2013-09-30T20:17:22.747 回答