滴滴热力图-php版(后面有js版本)

/**
* 获取边界
* @return mixed
* @return false|string
* @throws \Exception
*/
public function actionBorder()
{
$lock_file = "/tmp/process.lock";
$lock_file_handle = fopen($lock_file, 'w');
if ($lock_file_handle === false)
die("Can not create lock file {$lock_file}\n");
if (!flock($lock_file_handle, LOCK_EX + LOCK_NB)) {
die(date("Y-m-d H:i:s") . " Process already exists.\n");
}

$cache = \Yii::$app->cache;
// $cache->delete('amap_border');

$result = $cache->get('amap_border');
if($result) {
// return json_encode($result);
}

$gaode_url = 'https://restapi.amap.com/v3/config/district?keywords=110100&subdistrict=0&extensions=all&key=****';
$curl = new HttpCurl();
$response_json = $curl->get($gaode_url);
$response_arr = json_decode($response_json, true);
$left_lng = 0;
$up_lat = 0;
$right_lng = 0;
$down_lat = 0;
$lng_lat_arr = [];
$lng_lat_tmp_arr = [];
$result_arr_list = [];
if($response_arr['status'] == 1){
$lng_lat_str = isset($response_arr['districts'][0]['polyline']) ? $response_arr['districts'][0]['polyline'] : '';
$lng_lat_tmp_list = explode('|', $lng_lat_str);
$i = 1;
foreach ($lng_lat_tmp_list as $k=>$lng_lat_tmp){
$lng_lat_arr_tmp = explode(';', $lng_lat_tmp);
foreach ($lng_lat_arr_tmp as $lng_lat_arr_v) {
// 17152
if(!($i % 10)) {
$lng_lat_t_arr = explode(',', $lng_lat_arr_v);
$result_tmp['lng'] = $lng_lat_t_arr[0];
$result_tmp['lat'] = $lng_lat_t_arr[1];
$result_arr_list[$k][] = $result_tmp;
$lng_lat_arr[$k][] = $lng_lat_t_arr;
$lng_lat_tmp_arr[] = $lng_lat_t_arr;
}
$i++;
}
}
// var_dump(count($lng_lat_tmp_arr));die;

$tmp_lng_arr = ArrayHelper::getColumn($lng_lat_tmp_arr, '0');
$tmp_lat_arr = ArrayHelper::getColumn($lng_lat_tmp_arr, '1');
$left_lng =min($tmp_lng_arr);
$up_lat =max($tmp_lat_arr);
$right_lng =max($tmp_lng_arr);
$down_lat =min($tmp_lat_arr);
}
$boundary_arr = [];
foreach ($result_arr_list as $result_arr) {
// 获取6个点
$boundary_arr = $this->draw_other_lng_lat($left_lng, $up_lat, $result_arr, $right_lng, $down_lat);
$boundary_arr = array_merge($boundary_arr,$boundary_arr);
}

$result = [
'code' => 0,
// 'msg'=>'',
'data' => $lng_lat_arr,
'boundary_arr' => $boundary_arr,
'left_lng'=>$left_lng,
'up_lat'=>$up_lat,
'right_lng'=>$right_lng,
'down_lat'=>$down_lat,
];
$cache->set('amap_border', $result);
var_dump(count($boundary_arr));die;

}

public function draw_other_lng_lat($longitude, $latitude, $result_list, $right_lng, $down_lat) {
$latDistance = 1.0 / 111322 * cos(45*pi() / 180);//每一米代表多少度
$lonDistance = 1.0 / 111322;
$radius = 1000;//1000米
$boundary_arr = [];
$count_lenth = 1;
// for (var i_total=0; i_total<count_lenth; i_total+=10) {
while(1) {
$count_lenth++;
$mLongitude_tmp = $longitude + $count_lenth * 1.5 * $radius * $lonDistance;
$mLatitude_tmp = $latitude - 2 * $count_lenth * $radius * sin(60 * pi() / 180) * $latDistance;
if ($mLongitude_tmp > $right_lng && $mLatitude_tmp < $down_lat) { // 到边界了
break;
}
if($count_lenth > 5000) break; // 安全值 防止死循环
}
for ($i = 0; $i <= $count_lenth; $i++) {
$mLongitude = $longitude + $i * 1.5 * $radius * $lonDistance;
for ($j = 0; $j <= $count_lenth; $j++) {
$mLatitude = $latitude - 2 * $j * $radius * sin(60 * pi() / 180) * $latDistance;
if ($i % 2 != 0) {
$mLatitude += $radius * sin(60 * pi() / 180) * $latDistance;
}
if($mLongitude > $right_lng && $mLatitude < $down_lat){
echo "运行结束-";
break 2;
}else{
if($i == $count_lenth){
$count_lenth = $count_lenth+2;
$i--;
echo $count_lenth ." \n";
break;
}
}
$inRing = $this->is_point_in_polygon(['lng'=>$mLongitude,'lat'=>$mLatitude], $result_list); // 是否在多边形内

if($inRing == true) {
$height = $radius * sin(60*pi() / 180) * $latDistance;
$lng1 = $mLongitude - $radius / 2 * $lonDistance; $lat1 = $mLatitude + $height;
$lng2 = $mLongitude + $radius / 2 * $lonDistance; $lat2 = $mLatitude + $height;
$lng3 = $mLongitude + $radius * $lonDistance; $lat3 = $mLatitude;
$lng4 = $mLongitude + $radius / 2 * $lonDistance; $lat4 = $mLatitude - $height;
$lng5 = $mLongitude - $radius / 2 * $lonDistance; $lat5 = $mLatitude - $height;
$lng6 = $mLongitude - $radius * $lonDistance; $lat6 = $mLatitude;
$boundary_tmp = [$lng1.','.$lat1,$lng2.','.$lat2,$lng3.','.$lat3,$lng4.','.$lat4,$lng5.','.$lat5,$lng6.','.$lat6];
$boundary_arr[] = $boundary_tmp;
}
}
// if($break) break;
}
// }
return $boundary_arr;
}

/**
* 判断一个坐标是否在一个多边形内(由多个坐标围成的)
* 基本思想是利用射线法,计算射线与多边形各边的交点,如果是偶数,则点在多边形外,否则
* 在多边形内。还会考虑一些特殊情况,如点在多边形顶点上,点在多边形边上等特殊情况。
* @param $point 要查询的点
* @param $pts 要查询的范围
* @return bool
*/
public function is_point_in_polygon($point, $pts) {
$N = count($pts);
$boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
$intersectCount = 0;//穿越点数
$precision = 2e-10; //浮点类型计算时候与0比较时候的容差
$p1 = 0;
$p2 = 0;
$p = $point;

$p1 = $pts[0];
for ($i = 1; $i <= $N; ++$i) {
if ($p['lng'] == $p1['lng'] && $p['lat'] == $p1['lat']) {
return $boundOrVertex;
}

$p2 = $pts[$i % $N];//right vertex
if ($p['lat'] < min($p1['lat'], $p2['lat']) || $p['lat'] > max($p1['lat'], $p2['lat'])) {//ray is outside of our interests
$p1 = $p2;
continue;
}

if ($p['lat'] > min($p1['lat'], $p2['lat']) && $p['lat'] < max($p1['lat'], $p2['lat'])) {//ray is crossing over by the algorithm (common part of)
if($p['lng'] <= max($p1['lng'], $p2['lng'])){//x is before of ray
if ($p1['lat'] == $p2['lat'] && $p['lng'] >= min($p1['lng'], $p2['lng'])) {//overlies on a horizontal ray
return $boundOrVertex;
}

if ($p1['lng'] == $p2['lng']) {//ray is vertical
if ($p1['lng'] == $p['lng']) {//overlies on a vertical ray
return $boundOrVertex;
} else {//before ray
++$intersectCount;
}
} else {//cross point on the left side
$xinters = ($p['lat'] - $p1['lat']) * ($p2['lng'] - $p1['lng']) / ($p2['lat'] - $p1['lat']) + $p1['lng'];//cross point of lng
if (abs($p['lng'] - $xinters) < $precision) {//overlies on a ray
return $boundOrVertex;
}

if ($p['lng'] < $xinters) {//before ray
++$intersectCount;
}
}
}
} else {//special case when ray is crossing through the vertex
if ($p['lat'] == $p2['lat'] && $p['lng'] <= $p2['lng']) {//p crossing over p2
$p3 = $pts[($i+1) % $N]; //next vertex
if ($p['lat'] >= min($p1['lat'], $p3['lat']) && $p['lat'] <= max($p1['lat'], $p3['lat'])) { //p.lat lies between p1.lat & p3.lat
++$intersectCount;
} else {
$intersectCount += 2;
}
}
}
$p1 = $p2;//next ray left point
}

if ($intersectCount % 2 == 0) {//在多边形外
return false;
} else { //在多边形内
return true;
}
}
posted on 2019-10-11 17:37  基本资料  阅读(464)  评论(0编辑  收藏  举报