ADD порядок сортировки в релейшенах; функции перерасчёта дистанции для трека и его фрагментов.
This commit is contained in:
parent
e052bfaf57
commit
7860013f60
|
|
@ -18,11 +18,9 @@ class Geo
|
||||||
|
|
||||||
$distance = (6378137 * acos(cos($lat1) * cos($lat2) * cos($lng1 - $lng2) + sin($lat1) * sin($lat2)));
|
$distance = (6378137 * acos(cos($lat1) * cos($lat2) * cos($lng1 - $lng2) + sin($lat1) * sin($lat2)));
|
||||||
|
|
||||||
if (isset($latlng1['alt']) && isset($latlng2['alt']) && $latlng1['alt'] <> $latlng2['alt']) {
|
if (isset($latlng1['alt']) && isset($latlng2['alt']) && $latlng1['alt'] > 0 && $latlng1['alt'] <> $latlng2['alt']) {
|
||||||
// var_dump($distance, abs($latlng1['alt'] - $latlng2['alt']));
|
|
||||||
$altDelta = abs($latlng1['alt'] - $latlng2['alt']);
|
$altDelta = abs($latlng1['alt'] - $latlng2['alt']);
|
||||||
$distance = sqrt(pow($distance, 2) + pow($altDelta, 2));
|
$distance = sqrt(pow($distance, 2) + pow($altDelta, 2));
|
||||||
// var_dump($distance);die;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $distance;
|
return $distance;
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,9 @@ abstract class Model
|
||||||
return $model::model()->getOne($targetField, $this->$localField);
|
return $model::model()->getOne($targetField, $this->$localField);
|
||||||
break;
|
break;
|
||||||
case self::TO_MANY:
|
case self::TO_MANY:
|
||||||
|
if ($order) {
|
||||||
|
return $model::model()->getAll(array('where' => "`$targetField` = '" . $this->$localField . "'", 'order' => $order));
|
||||||
|
}
|
||||||
return $model::model()->getAll(array('where' => "`$targetField` = '" . $this->$localField . "'"));
|
return $model::model()->getAll(array('where' => "`$targetField` = '" . $this->$localField . "'"));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -115,13 +118,14 @@ abstract class Model
|
||||||
* @param const $relationType - тип связи (к одному / ко многим).
|
* @param const $relationType - тип связи (к одному / ко многим).
|
||||||
* @return obkect/array
|
* @return obkect/array
|
||||||
*/
|
*/
|
||||||
public function setRelation($nameOfRelation, $localField, $targetModel, $targetField, $relationType)
|
public function setRelation($nameOfRelation, $localField, $targetModel, $targetField, $relationType, $order = NULL)
|
||||||
{
|
{
|
||||||
return $this->_relations_[$nameOfRelation] = array(
|
return $this->_relations_[$nameOfRelation] = array(
|
||||||
'localField' => $localField,
|
'localField' => $localField,
|
||||||
'targetModel' => $targetModel,
|
'targetModel' => $targetModel,
|
||||||
'targetField' => $targetField,
|
'targetField' => $targetField,
|
||||||
'relationType' => $relationType,
|
'relationType' => $relationType,
|
||||||
|
'order' => $order,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -224,8 +224,6 @@ class EdittrackController extends Controller
|
||||||
|
|
||||||
|
|
||||||
if ($trk->id == $track->id) {
|
if ($trk->id == $track->id) {
|
||||||
// $prevPoint = false;
|
|
||||||
// $distanceTmp = 0;
|
|
||||||
$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ? AND `id` <= ?', 'order' => 'id'), array($trk->id, $trackPoint->id));
|
$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ? AND `id` <= ?', 'order' => 'id'), array($trk->id, $trackPoint->id));
|
||||||
foreach ($trackPoints as $tpt) {
|
foreach ($trackPoints as $tpt) {
|
||||||
TrackPoints::model()->add(
|
TrackPoints::model()->add(
|
||||||
|
|
@ -239,20 +237,8 @@ class EdittrackController extends Controller
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// if ($prevPoint) {
|
|
||||||
// $distanceTmp += Geo::distance($prevPoint, array('lat' => floatval($tpt->lat), 'lng' => floatval($tpt->lng)));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// $prevPoint = array(
|
|
||||||
// 'lat' => floatval($tpt->lat),
|
|
||||||
// 'lng' => floatval($tpt->lng),
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
// $newTrack->distance = $distanceTmp;
|
|
||||||
// $newTrack->save();
|
|
||||||
|
|
||||||
$prevPoint = false;
|
|
||||||
$distanceTmp = 0;
|
|
||||||
$newTrack = Tracks::model()->add(
|
$newTrack = Tracks::model()->add(
|
||||||
array(
|
array(
|
||||||
'tripId' => $trip->id,
|
'tripId' => $trip->id,
|
||||||
|
|
@ -274,18 +260,7 @@ class EdittrackController extends Controller
|
||||||
'speed' => $tpt->speed,
|
'speed' => $tpt->speed,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// if ($prevPoint) {
|
|
||||||
// $distanceTmp += Geo::distance($prevPoint, array('lat' => floatval($tpt->lat), 'lng' => floatval($tpt->lng)));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// $prevPoint = array(
|
|
||||||
// 'lat' => floatval($tpt->lat),
|
|
||||||
// 'lng' => floatval($tpt->lng),
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
// $newTrack->distance = $distanceTmp;
|
|
||||||
// $newTrack->save();
|
|
||||||
} else {
|
} else {
|
||||||
$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ?', 'order' => 'id'), array($trk->id));
|
$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ?', 'order' => 'id'), array($trk->id));
|
||||||
foreach ($trackPoints as $tpt) {
|
foreach ($trackPoints as $tpt) {
|
||||||
|
|
@ -307,6 +282,7 @@ class EdittrackController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$trip->json = '';
|
$trip->json = '';
|
||||||
|
$trip->calculateDist();
|
||||||
$trip->save();
|
$trip->save();
|
||||||
|
|
||||||
App::redirect('/tracks/edit/id/' . $trip->id);
|
App::redirect('/tracks/edit/id/' . $trip->id);
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ class Tracks extends Model
|
||||||
{
|
{
|
||||||
$this->_tableName_ = 'tracks';
|
$this->_tableName_ = 'tracks';
|
||||||
parent::__construct($fromArray);
|
parent::__construct($fromArray);
|
||||||
|
$this->setRelation('points', 'id', 'TrackPoints', 'trackId', self::TO_MANY, '`id` ASC');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function model()
|
static function model()
|
||||||
|
|
@ -18,4 +19,32 @@ class Tracks extends Model
|
||||||
{
|
{
|
||||||
return Geo::formatedDistance($this->distance);
|
return Geo::formatedDistance($this->distance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Считает/пересчитывает длину фрагмента трека и сохраняет значение в БД.
|
||||||
|
*/
|
||||||
|
function calculateDist()
|
||||||
|
{
|
||||||
|
$points = $this->points;
|
||||||
|
$distance = 0;
|
||||||
|
$prevPoint = array();
|
||||||
|
foreach ($points as $point) {
|
||||||
|
if ($prevPoint) {
|
||||||
|
$distance += Geo::distance(
|
||||||
|
$prevPoint,
|
||||||
|
array('lat' => floatval($point->lat), 'lng' => floatval($point->lng), 'alt' => floatval($point->alt)));
|
||||||
|
}
|
||||||
|
|
||||||
|
$prevPoint = array(
|
||||||
|
'lat' => floatval($point->lat),
|
||||||
|
'lng' => floatval($point->lng),
|
||||||
|
'alt' => floatval($point->alt),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->distance = $distance;
|
||||||
|
$this->save();
|
||||||
|
|
||||||
|
return $distance;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ class Trips extends Model
|
||||||
{
|
{
|
||||||
$this->_tableName_ = 'trips';
|
$this->_tableName_ = 'trips';
|
||||||
parent::__construct($fromArray);
|
parent::__construct($fromArray);
|
||||||
|
$this->setRelation('tracks', 'id', 'Tracks', 'tripId', self::TO_MANY, '`id` ASC');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function model()
|
static function model()
|
||||||
|
|
@ -22,4 +23,22 @@ class Trips extends Model
|
||||||
{
|
{
|
||||||
return Geo::formatedDistance($this->distance);
|
return Geo::formatedDistance($this->distance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Считает/пересчитывает длину трека и всех его фрагментов
|
||||||
|
*/
|
||||||
|
function calculateDist()
|
||||||
|
{
|
||||||
|
$tracks = $this->tracks;
|
||||||
|
$distance = 0;
|
||||||
|
|
||||||
|
foreach ($tracks as $track) {
|
||||||
|
$distance += $track->calculateDist();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->distance = $distance;
|
||||||
|
$this->save();
|
||||||
|
|
||||||
|
return $distance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue