2009-10-26 32 views
3

我需要在php或javascript中找到在kml文件中给出的地面覆盖图的纬度/经度。从kml文件计算地面覆盖角落的经度/ lng

I.e.对于一个具体的例子,我需要得到:

<LatLonBox> 
    <north>60.406505416667</north> 
    <south>60.400570555556</south> 
    <east>5.3351572222222</east> 
    <west>5.3190577777778</west> 
    <rotation>3.7088732260919</rotation> 
    </LatLonBox> 

到顶点坐标

SW: 60.400316388889;5.3194425 
SE: 60.400824722222;5.3355405555556 
NE: 60.406759444444;5.3347738888889 
NW: 60.406251388889;5.3186730555556 

我可以

$w=($nw_lng+$sw_lng)/2; 
$e=($ne_lng+$se_lng)/2; 
$n=($ne_lat+$nw_lat)/2; 
$s=($se_lat+$sw_lat)/2; 
$rot= rad2deg (atan (($nw_lng - $sw_lng)/($sw_lat - $nw_lat)/2 )); 

得到其他方式(大约至少,PHP代码中给出)应该很容易回来,但我已经用了几个小时没有到达那里。有小费吗?

+0

Python的实现将会很好 - 我可以将其转换为JavaScript/PHP。我需要在一个项目中根据GPS数据在地图上显示您当前的位置(例如Garmin自定义地图kmz文件)。一款适用于手机的网络应用程序 - 大部分已完成,但这部分内容未能实现。不是作业。 – jankoc 2009-10-26 13:14:09

回答

3

您需要使用spherical trigonometry,作为spherical geometry的一部分才能获得完整的准确性。然而,由于你只处理球体的一小部分,如果你记住一件事情,欧几里德几何图形就会做。

随着纬度的增加,经度线越来越接近。例如,在北极附近,纬线几乎接触。因此,要处理你的纬度差异,通过多因素cos(纬度)来减少它们。这会为您的应用带来足够的准确度。

$n = 60.406505416667; 
$s = 60.400570555556; 
$e = 5.3351572222222; 
$w = 5.3190577777778; 
$rotn = 3.7088732260919; 

$a = ($e + $w)/2.0; 
$b = ($n + $s)/2.0; 
$squish = cos(deg2rad($b)); 
$x = $squish * ($e - $w)/2.0; 
$y = ($n - $s)/2.0; 

$ne = array(
    $a + ($x * cos(deg2rad($rotn)) - $y * sin(deg2rad($rotn))) /$squish, 
    $b + $x * sin(deg2rad($rotn)) + $y *cos(deg2rad($rotn)) 
    ); 
$nw = array(
    $a - ($x * cos(deg2rad($rotn)) + $y * sin(deg2rad($rotn))) /$squish, 
    $b - $x * sin(deg2rad($rotn)) + $y *cos(deg2rad($rotn)) 
    ); 
$sw = array(
    $a - ($x * cos(deg2rad($rotn)) - $y * sin(deg2rad($rotn))) /$squish, 
    $b - $x * sin(deg2rad($rotn)) - $y *cos(deg2rad($rotn)) 
    ); 
$se = array(
    $a + ($x * cos(deg2rad($rotn)) + $y * sin(deg2rad($rotn))) /$squish, 
    $b + $x * sin(deg2rad($rotn)) - $y *cos(deg2rad($rotn)) 
    ); 
print_r(array(
'sw'=>$sw, 
'se'=>$se, 
'ne'=>$ne, 
'nw'=>$nw, 
)); 

我的$squish变量是我提到的cos(lat)。有水平长度的相对部分去除。正弦表看起来像这样:

NE: (a + x cos A - y sin A, b + x sin A + y cos A) 
NW: (a - x cos A - y sin A, b - x sin A + y cos A) 
SW: (a - x cos A + y sin A, b - x sin A - y cos A) 
SE: (a + x cos A + y sin A, b + x sin A - y cos A) 

也许tttppp可能会解释tttppp表中的差异。

+0

距离对我来说似乎很小,所以也许一个欧几里德近似就足够了 – Victor 2009-10-26 13:20:46

+0

我已经知道我需要使用某种三角。我认为一个欧几里德近似可以做到(这就是反向公式所使用的,它对我认为的地图工作正常)。但是,即使在工作了几个小时之后,我仍然没有设法为前向案例获得三角法。逆公式很容易计算出来...... – jankoc 2009-10-26 13:26:36

+0

现在看起来非常好!也许我的逆公式还需要一些修复才能找回确切的答案。这给了Array([sw] => Array([0] => 5.3194632953201 [1] => 60.400319597408)[se] => Array([0] => 5.3355290212874 [1] => 60.400833943604)[ne] => Array ([0] => 5.3347517046799 [1] => 60.406756374815)[nw] => Array([0] => 5.3186859787126 [1] => 60.406242028619)) – jankoc 2009-10-26 15:47:52