public function run() { $limit = ' LIMIT 0,200'; $sql = "SELECT station_id,location_x,location_y FROM " . DB_PREFIX . "station WHERE location_x !=0.0 AND location_y!=0.0 AND baidu_x = '' AND baidu_y = '' " . $limit; $q = $this->db->query($sql); while ($r = $this->db->fetch_array($q)) { $key = md5($r['station_id'] . $r['location_x'] . $r['location_y']); $baidu_zb[$key] = $r; } //hg_pre($baidu_zb,0); if (!empty($baidu_zb)) { foreach ($baidu_zb as $key => $val) { $res = ''; $sql = "SELECT station_id,baidu_x,baidu_y FROM " . DB_PREFIX . "baidu_zb WHERE md5_key = '{$key}'"; $res = $this->db->query_first($sql); if ($res['station_id'] && $res['baidu_x'] && $res['baidu_y']) { $sql = "UPDATE " . DB_PREFIX . "station SET baidu_x = '{$res['baidu_x']}',baidu_y = '{$res['baidu_y']}' WHERE station_id = {$res['station_id']}"; $this->db->query($sql); } else { $sql = "DELETE FROM " . DB_PREFIX . "baidu_zb WHERE station_id = {$val['station_id']}"; $this->db->query($sql); $zb_tmp = array(); $zb_tmp = FromGpsToBaidu($val['location_x'] . ',' . $val['location_y'], BAIDU_AK); if (empty($zb_tmp)) { continue; } $sql = "UPDATE " . DB_PREFIX . "station SET baidu_x = '{$zb_tmp['x']}',baidu_y = '{$zb_tmp['y']}' WHERE station_id = {$val['station_id']}"; $this->db->query($sql); $sql = "INSERT INTO " . DB_PREFIX . "baidu_zb SET station_id = '{$val['station_id']}', baidu_x = '{$zb_tmp['x']}', baidu_y = '{$zb_tmp['y']}',md5_key='{$key}'"; $this->db->query($sql); } } } }
private function update_line_station($database) { if (!$database) { return false; } //处理起始站点 $sql = "SELECT t1.line_no,t1.line_direct, t2.station_name FROM {$database}." . DB_PREFIX . "line_station t1 \n \t\t\t\tLEFT JOIN {$database}." . DB_PREFIX . "station t2 \n\t \t\t\t\tON t1.station_id=t2.station_id\n\t WHERE t1.line_direct =1 ORDER BY t1.station_no ASC"; //echo $sql; $q = $this->db->query($sql); $line_station = array(); while ($r = $this->db->fetch_array($q)) { $line_station[$r['line_no']][] = $r; } //hg_pre($line_station,0); if (!empty($line_station)) { $tmp = array(); foreach ($line_station as $key => $val) { if (empty($val)) { continue; } $len = ''; $len = count($val); $val[0]['station_name'] = str_replace('-1', '', $val[0]['station_name']); $val[0]['station_name'] = str_replace('-2', '', $val[0]['station_name']); $tmp[$key]['start_station'] = $val[0]['station_name']; if ($len > 1) { $val[$len - 1]['station_name'] = str_replace('-1', '', $val[$len - 1]['station_name']); $val[$len - 1]['station_name'] = str_replace('-2', '', $val[$len - 1]['station_name']); $tmp[$key]['end_station'] = $val[$len - 1]['station_name']; } } if (!empty($tmp)) { foreach ($tmp as $k => $v) { $sql = "UPDATE {$database}." . DB_PREFIX . "line SET start_station = '{$v['start_station']}', end_station = '{$v['end_station']}' WHERE line_no = '{$k}'"; $this->db->query($sql); } } } //坐标处理 $sql = "SELECT station_id,location_x,location_y FROM {$database}." . DB_PREFIX . "station WHERE location_x !=0.0 AND location_y!=0.0 AND baidu_x = '' AND baidu_y = '' " . $limit; $q = $this->db->query($sql); while ($r = $this->db->fetch_array($q)) { $key = md5($r['station_id'] . $r['location_x'] . $r['location_y']); $baidu_zb[$key] = $r; } //hg_pre($baidu_zb,0); if (!empty($baidu_zb)) { foreach ($baidu_zb as $key => $val) { $res = ''; $sql = "SELECT station_id,baidu_x,baidu_y FROM {$database}." . DB_PREFIX . "baidu_zb WHERE md5_key = '{$key}'"; $res = $this->db->query_first($sql); if ($res['station_id'] && $res['baidu_x'] && $res['baidu_y']) { $sql = "UPDATE {$database}." . DB_PREFIX . "station SET baidu_x = '{$res['baidu_x']}',baidu_y = '{$res['baidu_y']}' WHERE station_id = {$res['station_id']}"; $this->db->query($sql); } else { $sql = "DELETE FROM {$database}." . DB_PREFIX . "baidu_zb WHERE station_id = {$val['station_id']}"; $this->db->query($sql); $zb_tmp = array(); $zb_tmp = FromGpsToBaidu($val['location_x'] . ',' . $val['location_y'], BAIDU_AK); if (empty($zb_tmp)) { continue; } $sql = "UPDATE {$database}." . DB_PREFIX . "station SET baidu_x = '{$zb_tmp['x']}',baidu_y = '{$zb_tmp['y']}' WHERE station_id = {$val['station_id']}"; $this->db->query($sql); $sql = "INSERT INTO {$database}." . DB_PREFIX . "baidu_zb SET station_id = '{$val['station_id']}', baidu_x = '{$zb_tmp['x']}', baidu_y = '{$zb_tmp['y']}',md5_key='{$key}'"; $this->db->query($sql); } } } }
public function show() { $lat = $this->input['lat']; $lng = $this->input['lng']; if (!$lat || !$lng) { $return = array('error_message' => '请开启定位'); echo json_encode($return); exit; } /*if($this->input['appid'] == $this->settings['android_appid']) { $gps_res = FromBaiduToGpsXY($lng,$lat); if(!empty($gps_res)) { $lng = $gps_res['GPS_x']; $lat = $gps_res['GPS_y']; } }*/ $rad = $this->input['rad']; $rad = $rad ? $rad / 1000 : 1; //默认1千米范围 $condition = ''; if ($lat && $lng && $rad) { $range = 180 / pi() * $rad / 6372.797; //rad单位km $lngR = $range / cos($lat * pi() / 180); //echo $range;exit() $maxLat = $lat + $range; //最大纬度 $minLat = $lat - $range; //最小纬度 $maxLng = $lng + $lngR; //最大经度 $minLng = $lng - $lngR; //最小经度 if ($rad <= 10) { if ($this->input['appid'] == $this->settings['android_appid']) { $condition .= ' WHERE baidu_x >=' . $minLng . ' AND baidu_x <=' . $maxLng . ' AND baidu_y >=' . $minLat . ' AND baidu_y <= ' . $maxLat; } else { $condition .= ' WHERE location_x >=' . $minLng . ' AND location_x <=' . $maxLng . ' AND location_y >=' . $minLat . ' AND location_y <= ' . $maxLat; } } else { $limit = ' LIMIT 0,10'; } $order_by = ' ORDER BY Lng, Lat ASC'; if ($this->input['appid'] == $this->settings['android_appid']) { $sql = "SELECT *,ABS(ABS(baidu_x)-ABS(" . $lng . ")) AS Lng, ABS(ABS(baidu_y)-ABS(" . $lat . ")) AS Lat FROM " . DB_PREFIX . "station " . $condition . $order_by . $limit; } else { $sql = "SELECT *,ABS(ABS(location_x)-ABS(" . $lng . ")) AS Lng, ABS(ABS(location_y)-ABS(" . $lat . ")) AS Lat FROM " . DB_PREFIX . "station " . $condition . $order_by . $limit; } } //echo $sql; $q = $this->db->query($sql); $station_id = array(); $station = array(); while ($r = $this->db->fetch_array($q)) { ############站点名称处理############ $r['station_name'] = str_replace('-1', '', $r['station_name']); $r['station_name'] = str_replace('-2', '', $r['station_name']); ################################# $station[$r['station_id']]['station_name'] = $r['station_name']; $station[$r['station_id']]['location_x'] = $r['location_x']; $station[$r['station_id']]['location_y'] = $r['location_y']; $zb = array(); //站点表中没记录站点百度坐标,查找坐标缓存文件 if ($r['location_x'] && $r['location_y'] && !$r['baidu_x'] && !$r['baidu_y']) { if (file_exists('./data/' . $r['station_id'] . '.txt')) { $res = ''; $res = file_get_contents('./data/' . $r['station_id'] . '.txt'); if ($res) { $zb = json_decode($res, 1); $r['baidu_x'] = $zb['baidu_x']; $r['baidu_y'] = $zb['baidu_y']; } } } //缓存文件不存在,将gps坐标转换百度坐标 if ($r['location_x'] && $r['location_y'] && !$r['baidu_x'] && !$r['baidu_y']) { $zb_tmp = array(); $zb_tmp = FromGpsToBaidu($r['location_x'] . ',' . $r['location_y'], BAIDU_AK); if (!empty($zb_tmp)) { $r['baidu_x'] = $zb_tmp['x']; $r['baidu_y'] = $zb_tmp['y']; $arr_tmp = array('station_id' => $r['station_id'], 'name' => $r['station_name'], 'location_x' => $r['location_x'], 'location_y' => $r['location_y'], 'baidu_x' => $r['baidu_x'], 'baidu_y' => $r['baidu_y']); $file_name = ''; $file_name = $r['station_id'] . '.txt'; file_put_contents('./data/' . $file_name, json_encode($arr_tmp)); } else { $r['baidu_x'] = ''; $r['baidu_y'] = ''; } } $station[$r['station_id']]['baidu_x'] = $r['baidu_x']; $station[$r['station_id']]['baidu_y'] = $r['baidu_y']; $station_id[] = $r['station_id']; } //hg_pre($station_id,0); if (empty($station_id)) { $return = array('error_message' => '附近没有站点'); echo json_encode($return); exit; } $station_ids = ''; $station_ids = implode(',', $station_id); $sql = "SELECT t1.*,t2.*,start_time,stop_time,sub_start_time,sub_stop_time FROM " . DB_PREFIX . "line_station t1 \n\t\t\t\t\tLEFT JOIN " . DB_PREFIX . "line t2\n\t\t\t\t\t\tON t1.line_no = t2.line_no \n\t\t\t\tWHERE t1.station_id IN (" . $station_ids . ")"; //echo $sql; $q = $this->db->query($sql); $line_info = array(); while ($r = $this->db->fetch_array($q)) { if ($r['start_time'] && $r['stop_time']) { $r['start_time'] = date('H:i', $r['start_time']); $r['stop_time'] = date('H:i', $r['stop_time']); } if ($r['sub_start_time'] && $r['sub_stop_time']) { $r['sub_start_time'] = date('H:i', $r['sub_start_time']); $r['sub_stop_time'] = date('H:i', $r['sub_stop_time']); } $arr = array(); $arr['routeid'] = $r['line_no']; $arr['segmentid'] = $r['station_no']; $arr['stationseq'] = $r['line_direct']; $arr['station_id'] = $r['station_id']; $arr['stationname'] = $station[$r['station_id']]['station_name']; $arr['longitude'] = $station[$r['station_id']]['location_x']; $arr['latitude'] = $station[$r['station_id']]['location_y']; $arr['blongitude'] = $station[$r['station_id']]['baidu_x']; $arr['blatitude'] = $station[$r['station_id']]['baidu_y']; if ($r['line_direct'] == 1) { if ($r['start_time'] && $r['stop_time']) { $arr['starttime'] = $r['start_time'] . '-' . $r['stop_time']; } else { $arr['starttime'] = ''; } } else { if ($r['sub_start_time'] && $r['sub_stop_time']) { $arr['starttime'] = $r['sub_start_time'] . '-' . $r['sub_stop_time']; } else { $arr['starttime'] = ''; } } $key = ''; $key = $r['line_no'] . '_' . $r['line_direct']; if ($r['line_direct'] == 2) { $start_tmp = ''; $start_tmp = $r['end_station']; $arr['start_station'] = $start_tmp; $arr['end_station'] = $r['start_station']; } else { $arr['start_station'] = $r['start_station']; $arr['end_station'] = $r['end_station']; } $line_info[$key]['routeid'] = $r['line_no']; $line_info[$key]['segmentid'] = $r['station_no']; $line_info[$key]['segmentname2'] = $r['line_name']; $line_info[$key]['station'][] = $arr; } //hg_pre($line_info,0); if ($line_info) { $time = TIMENOW; $line_info_tmp = $line_info_tmp1 = array(); foreach ($station as $key => $value) { foreach ($line_info as $kk => $val) { foreach ($val['station'] as $v) { if ($v['station_id'] != $key) { continue; } $se_time = $start_time = $end_time = ''; $se_time = explode('-', $v['starttime']); if ($se_time) { $start_time = strtotime(date('H:i', strtotime($se_time['0']))); $end_time = strtotime(date('H:i', strtotime($se_time['1']))); } if ($time >= $start_time && $time <= $end_time) { $line_info_tmp[$kk] = $val; } else { $line_info_tmp1[$kk] = $val; } break; } } } } if (!empty($line_info_tmp) || !empty($line_info_tmp1)) { $line_info_tmp = array_merge($line_info_tmp, $line_info_tmp1); $line_info = array(); foreach ($line_info_tmp as $v) { $line_info[] = $v; } echo json_encode($line_info); exit; } }
private function init_data($data, $sort_id) { if (!$data || !$sort_id) { return FALSE; } $user_name = $this->user['user_name']; $appid = $this->user['appid']; $appname = $this->user['display_name']; //$sql = "INSERT INTO ".DB_PREFIX."station (user_name, state, appid, appname, company_id, name, station_id, baidu_longitude, baidu_latitude, address, totalnum, currentnum, dateline, create_time)VALUES"; $val = ''; $convert_set = ''; //对坐标处理 $addlong = ''; //经度偏移量 $addlat = ''; //纬度偏移量 foreach ($data as $key => $value) { if (empty($value)) { continue; } //取运营单位里的配置 if ($this->company[$key]) { $convert_set = $this->company[$key]['convert_set']; $addlong = $this->company[$key]['addlong']; $addlat = $this->company[$key]['addlat']; } $val = ''; foreach ($value as $k => $v) { //需要处理坐标,并且坐标存在 if ($convert_set && $v['stationx'] && $v['stationy']) { $zb = ''; if ($convert_set == 2) { $zb = FromGpsToBaidu($v['stationx'] . ',' . $v['stationy'], BAIDU_AK); } else { if ($convert_set == 3) { $zb = FromGoogleToBaidu($v['stationx'] . ',' . $v['stationy'], BAIDU_AK); } else { if ($convert_set == 4 && $addlong && $addlat) { $zb['x'] = $v['stationx'] + $addlong; $zb['y'] = $v['stationy'] + $addlat; } else { if ($convert_set == 1) { $zb['x'] = $v['stationx']; $zb['y'] = $v['stationy']; } } } } if ($zb) { $v['stationx'] = $zb['x']; $v['stationy'] = $zb['y']; } } $info = array('title' => $v['station'], 'local_id' => $v['stationid'], 'address' => $v['address'], 'baidu_latitude' => $v['stationy'], 'baidu_longitude' => $v['stationx'], 'company_id' => $key, 'create_time' => TIMENOW, 'sort_id' => $sort_id, 'user_name' => $user_name, 'status' => 0); $sql = " INSERT INTO " . DB_PREFIX . "lbs SET "; foreach ($info as $kk => $vv) { $sql .= " {$kk} = '{$vv}',"; } $sql = trim($sql, ','); $this->db->query($sql); $sid = ''; $sid = $this->db->insert_id(); if (!$sid) { continue; } $sql = ""; $sql = " UPDATE " . DB_PREFIX . "lbs SET order_id = {$sid} WHERE id = {$sid}"; $this->db->query($sql); //扩展字段记录可停,可借数目 $this->insert_fieldontent($sid, $sort_id, $v['totalnum'], $v['currentnum']); //$val .= "('" . $user_name . "',1," . $appid . ",'" . $appname . "','" . $key . "','" . $v['station'] . "','".$v['stationid']."','".$v['stationx']."','".$v['stationy']."','".$v['address']."','".$v['totalnum']."','".$v['currentnum']."','".$v['dateline']."',".TIMENOW."),"; } //更新运营单位站点计数 $station_num = ''; $station_num = count($value); if ($station_num) { $up_sql = "UPDATE " . DB_PREFIX . "company SET station_count = station_count + " . $station_num . " WHERE id = " . $key; $this->db->query($up_sql); } } /*if($val) { $sql .= $val; $sql = rtrim($sql,','); $this->db->query($sql); }*/ }