0

更新:我能够使用 Baba 的代码并对其进行修改以完成我希望完成的任务,但是,现在我想将内容写入 CSV 文件。每次创建文件时它都是空的。如何将数据导入 CSV 文件?

我正在尝试创建一个允许我计算位置之间距离的 php 脚本。例如 (A 到 B, A 到 C, A 到 D),(B 到 C, B 到 D),(C 到 D) 其中 A,B,C,D 是纬度和经度对。

Lat 和 Long 是从 CSV 文件中读取的,我希望最终使用 php 创建一个包含所有位置和相互距离的 CSV 文件。截至目前,我能够回显每一列,但无法通过距离函数传递它。任何帮助将不胜感激。

距离.php

<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) *cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
    return ($miles * 1.609344);
} else if ($unit == "N") {
    return ($miles * 0.8684);
} else {
    return $miles;
}
}

$file = file("geocode2.csv");
$outfile = fopen('geodis.csv','w');
do {list($lat1, $lon1) = explode(",", array_shift($file));
foreach ( $file as $data ) {
    list($lat2, $lon2) = explode(",", $data);

    $line = printf("%f,%f to %f,%f = %f Miles <br>",$lat1,$lon1,$lat2,$lon2,distance($lat1, $lon1, $lat2, $lon2, "M"));
fputcsv($outfile, $line);
                        }
}
while(!feof($file));
fclose($outfile);
?>

地理编码.csv

bg_lat,bg_long
40.8394936,-73.3203704
40.8297023,-73.0942956
40.8853812,-73.1019428
40.858722,-73.20665
40.7503593,-73.1310993
4

2 回答 2

0

(A to B, A to C, A to D),(B to C, B to D),(C to D)有很多10,000积分操作,我不建议你应该这样做PHP

尝试使用具有Geospatial支持的数据库,例如MongoDB

这是一个使用的基本示例

(A to B, A to C, A to D)
 ^       ^       ^
 |       |       |
 A is constant for every other element in the list 

这是一个有趣的解决方案,文件中的列表元素是A

我的log.txt长相是这样的

40.8394936,-73.3203704
40.8297023,-73.0942956
40.8853812,-73.1019428
40.858722,-73.20665
40.7503593,-73.1310993

因此40.8394936,-73.3203704将是A

编码

// Load Files
$file = new SplFileObject("log.txt");

// Get A ( The first Lat , Lon )
list($lat1, $lon1) = explode(",", $file->fgets());

// Loop
while (!$file->eof()) {
    // Get Other Lat , Lon )
    list($lat2, $lon2) = explode(",",  $file->fgets());

    // Get Distance and print result
    printf("%f,%f to %f,%f = %f KM \n",
        $lat1,$lon1,
        $lat2,$lat2,
        distance($lat1, $lon1, $lat2, $lon2, "K")
    );
}

输出

40.839494,-73.320370 to 40.829702,40.829702 = 19.049914 KM 
40.839494,-73.320370 to 40.885381,40.885381 = 19.063217 KM 
40.839494,-73.320370 to 40.858722,40.858722 = 9.800795 KM 
40.839494,-73.320370 to 40.750359,40.750359 = 18.763195 KM 

距离函数

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);
    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}
于 2013-08-02T15:25:32.503 回答
0

我不明白你的问题,从我看到的脚本可以很好地工作,但不知道如何将一行与另一行进行比较,对吗?骨头 A 到 B

编辑:

我的答案。

<?php
ini_set('display_errors', 'On');
function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{

return round((acos(sin(deg2rad ($lat1)) * sin(deg2rad ($lat2)) + 
             cos(deg2rad ($lat1)) * cos(deg2rad ($lat2)) * 
             cos(deg2rad ($lng1) - deg2rad ($lng2))) * 6378),2);

}  

$file = fopen('geocode.csv','r');

$data0 = array();

while(($data = fgetcsv($file,0,",")) !== FALSE){
array_push($data0, $data);

}

$num = count($data0);
$data1 = $data0;

for ($x=0; $x < $num; $x++) {
for ($x2=0; $x2 < $num; $x2++) {

    echo $data0[$x][0].','.$data0[$x][1].' - ' .$data1[$x2][0]. ','.$data1[$x2][1]. '  =  '.
    distance($data0[$x][0],$data0[$x][1],$data1[$x2][0],$data1[$x2][1])."<br>";
}


 }

fclose($file);
?>
于 2013-08-02T15:40:37.053 回答