2

各ポイントの緯度と経度を含む空港のデータベースがあります。PHP スクリプトを実行して、特定の空港の近くにあるすべての空港を距離と相対方向で検索したいと考えています。

すなわち空港 KLDJ (40-37-02.810N 074-14-40.539W) の場合

近くの空港
KJFK - ジョン F ケネディ空港 (21.2 nm NE) (40-38-23.104N 073-46-44.132W)

http://www.movable-type.co.uk/scripts/latlong.htmlのコードを使用して距離を見つけ、それを使用して方位を見つけようとしましたが、正しくない可能性があります。

//BEARING RHUMB LINE
$phi = log(tan($lat2/2+pi/4)/tan($lat1/2+pi/4));
$distance['bearing'] = (rad2deg(atan2($theta, $phi)) +180) % 360;

私は基本的に、このスクリプトを介してすべてのポイントを実行し、距離を見つけたいと思っています。(つまり、北、南、西、東、北西、南西、北東、南東)

4

3 に答える 3

6

hereのテクニックを大いに借りて、hereのデータを使用して、この例をまとめました

<?php

$chicago = array(
    'lat' => 41.9
  , 'lng' => 87.65
);

$dallas = array(
    'lat' => 32.73
  , 'lng' => 96.97
);

$ftworth = array(
    'lat' => 32.82
  , 'lng' => 97.35
);

$bearing = getBearingBetweenPoints( $dallas, $chicago );

echo "Bearing: $bearing&deg;<br>";
echo "Direction: " . getCompassDirection( $bearing );

function getBearingBetweenPoints( $point1, $point2 )
{
  return getRhumbLineBearing( $point1['lat'], $point2['lng'], $point2['lat'], $point1['lng'] );
}

function getRhumbLineBearing($lat1, $lon1, $lat2, $lon2) {
  //difference in longitudinal coordinates
  $dLon = deg2rad($lon2) - deg2rad($lon1);

  //difference in the phi of latitudinal coordinates
  $dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));

  //we need to recalculate $dLon if it is greater than pi
  if(abs($dLon) > pi()) {
    if($dLon > 0) {
      $dLon = (2 * pi() - $dLon) * -1;
    }
    else {
      $dLon = 2 * pi() + $dLon;
    }
  }
  //return the angle, normalized
  return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
}

function getCompassDirection( $bearing )
{
  static $cardinals = array( 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N' );
  return $cardinals[round( $bearing / 45 )];
}
于 2010-05-18T19:13:05.000 に答える
1

同じウェブサイトの別のページを見る:

$lat = 0;  // latitude of centre of bounding circle in degrees
$lon = 0;  // longitude of centre of bounding circle in degrees
$rad = 0;  // radius of bounding circle in chosen units

// Choose your unit of measure... 
$r = 6371;  // earth's radius in km
$r = 3959;  // earth's radius in miles
// or
$r = 3440;  // earth's radius in nautical miles

// convert point of origin to radians
$lat = deg2rad($lat);
$lon = deg2rad($lon);

// first-cut bounding box (in radians)
$maxLat = $lat + $rad / $r;
$minLat = $lat - $rad / $r;
// compensate for longitude getting smaller with increasing latitude
$maxLon = ( $lon + $rad / $r ) / cos( $lat );
$minLon = ( $lon - $rad / $r ) / cos( $lat );

クエリと組み合わせる:

$query = 'SELECT
  id
, lat
, lon
, ACOS(SIN('.$lat.') * SIN(RADIANS(lat)) + COS('.$lat.') * COS(RADIANS(lat)) * COS(RADIANS(lon) - '. $lon.')) * '.$r.' AS distance
FROM (
   SELECT
     id
   , lat
   , lon
   FROM MyTable
   WHERE 
     RADIANS(lat) > $minLat AND RADIANS(lat) < '.$maxLat.'
    AND 
     RADIANS(lon) > $minLon AND RADIANS(lon) < '.$maxLon.'
) AS FirstCut 
WHERE ACOS(SIN('.$lat.') * SIN(RADIANS(lat)) + COS('.$lat.') * COS(RADIANS(lat)) * COS(RADIANS(lon) - '. $lon.')) * '.$r.' < '.$rad;

指定した境界円内の位置 (および円の原点からの距離) のリストを選択します。

あとは、返された位置の方位を計算するだけです。これは、ラムライン式で行うことができます.

上記は、データベースに度単位の座標が入力されていることを前提としています。

位置をジオメトリ オブジェクトとして保存している場合は、MySQL に組み込まれている空間関数を使用できます。MySQLには外接する四角形しか含まれていないと確信していますが。

于 2010-05-18T19:14:06.807 に答える
1

You can check, if the latitude of position B is larger or smaller than the latitude of position A to determine if it's in the east or in the west. Same thing with the longitude for north and south. Finally you need a threshold, to detect, in which case the drift is large enough to switch to an intermediate direction.

于 2010-05-18T18:31:44.133 に答える