File: point.inc.php

package info (click to toggle)
zoph 1.0.1-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 16,480 kB
  • sloc: php: 27,195; javascript: 10,374; sql: 416; sh: 152; makefile: 4
file content (237 lines) | stat: -rw-r--r-- 7,241 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
 * A point is a GPS position + time, used for Geotagging
 *
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Jeroen Roos
 * @package Zoph
 */

namespace geo;

use db\delete;
use db\param;
use db\clause;
use db\select;

use XMLReader;
use PDO;

/**
 * This class describes a point, which is a GPS position + timestamp
 *
 * @author Jeroen Roos
 * @package Zoph
 */
class point extends \zophTable {
    /** @var string The name of the database table */
    protected static $tableName="point";
    /** @var array List of primary keys */
    protected static $primaryKeys=array("point_id");
    /** @var array Fields that may not be empty */
    protected static $notNull=array();
    /** @var bool keep keys with insert. In most cases the keys are set by
                  the db with auto_increment */
    protected static $keepKeys = false;
    /** @var string URL for this class */
    protected static $url;
    /**
     * Create object from XML-snippet
     * @param string snippet of XML-code
     */
    public static function readFromXML($xmldata) {
        date_default_timezone_set("UTC");
        $point=new point();
        $xml=new XMLReader();
        $xml->xml($xmldata);
        $xml->read();
        $point->set("lat", $xml->getAttribute("lat"));
        $point->set("lon", $xml->getAttribute("lon"));
        while ($xml->read()) {
            if ($xml->nodeType==XMLReader::ELEMENT) {
                switch ($xml->name) {
                case "name":
                    $xml->read();
                    $name=$xml->value;
                    $point->set("name", $name);
                    break;
                case "ele":
                    $xml->read();
                    $point->set("ele", $xml->value);
                    break;
                case "speed":
                    $xml->read();
                    $point->set("speed", $xml->value);
                    break;
                case "time":
                    $xml->read();
                    $datetime=strtotime($xml->value);
                    $point->set("datetime", date("Y-m-d H:i:s", $datetime));
                    break;
                default:
                    // unrecognized element, ignore
                    break;
                }
            }
        }
        return $point;
    }

    /**
     * Create a query to find the next and previous points
     * this query will be expanded by the @see getNext() and @see getPrevious() methods
     * @return array (select, clause);
     */
    private function getNextPrevQry() {
        $qry=new select(array("pt" => "point"));
        $where=new clause("track_id=:trackid");

        $qry->addParams(array(
            new param(":trackid", (int) $this->get("track_id"), PDO::PARAM_INT),
            new param(":datetime", $this->get("datetime"), PDO::PARAM_STR)
        ));

        $qry->addLimit(1);
        return array($qry, $where);
    }

    /**
     * Get the next (in time) point from a track
     */
    public function getNext() {
        list($qry, $where)=$this->getNextPrevQry();

        $where->addAnd(new clause("datetime>:datetime"));
        $qry->where($where);

        $qry->addOrder("datetime");

        $points=static::getRecordsFromQuery($qry);
        if (is_array($points) && sizeof($points) > 0) {
            return $points[0];
        } else {
            return null;
        }
    }

    /**
     * Get the  previous (in time) point from a track
     */
    public function getPrev() {
        list($qry, $where)=$this->getNextPrevQry();

        $where->addAnd(new clause("datetime<:datetime"));

        $qry->where($where);

        $qry->addOrder("datetime DESC");

        $points=static::getRecordsFromQuery($qry);
        if (is_array($points) && sizeof($points) > 0) {
            return $points[0];
        } else {
            return null;
        }
    }

    /**
     * Calculate the distance to another point
     *
     * @param point Point to calculate distance to
     * @param string "km" or "miles"
     * @return int distance
     */
    private function getDistanceTo(point $p2, $entity="km") {
        $p1=$this;
        $lat1=$p1->get("lat");
        $lon1=$p1->get("lon");
        $lat2=$p2->get("lat");
        $lon2=$p2->get("lon");

        $distance=(6371 * acos(
            cos(deg2rad($lat1)) *
            cos(deg2rad($lat2)) *
            cos(deg2rad($lon2) - deg2rad($lon1)) +
            sin(deg2rad($lat1)) *
            sin(deg2rad($lat2))));

        if ($entity=="miles") {
            $distance=$distance / 1.609344;
        }

        return $distance;
    }

    /**
     * Interpolate between points to find out the location on a
     * certain moment
     *
     * This is an approximate calculation
     * and could be very inaccurate if the distance between the
     * points is large, therefore you can give a max distance in km
     *
     * The longer time there is between 2 point the smaller the
     * chance is you actually travelled in a straight line between
     * the points, so you can also give a max time, in seconds.
     * @param point is the point where you are at t1
     * @param point is the point where you are at t2
     * @param int t3 is the time you want to calculate the position for
     * @param int maximum distance to to calculation for
     * @param string entity of distances ("km" or "miles")
     * @param int maximum time between two points
     * @return point this function will return where you are at t3
     * @todo the "return false" should both be changed into an Exception
     */
    public static function interpolate(point $p1, point $p2,
        $t3, $maxdist=null, $entity="km", $maxtime=null) {

        $t1 = strtotime($p1->get("datetime"));
        $t2 = strtotime($p2->get("datetime"));

        if ((!($t2 >= $t3 && $t3 >= $t1)) || ($maxtime && (abs($t1 - $t2) > $maxtime))) {
            return false;
        }

        if ($maxdist) {
            $dist=$p1->getDistanceTo($p2, $entity);
            if ($dist > $maxdist) {
                return false;
            }
        }
        $lat1=$p1->get("lat");
        $lon1=$p1->get("lon");

        $lat2=$p2->get("lat");
        $lon2=$p2->get("lon");

        // Calculate the deltas
        $dlat=$lat2-$lat1;
        $dlon=$lon2-$lon1;
        $dt=$t2-$t1;
        $dt3=$t3-$t1;

        $lat3=$lat1 + (($dlat/$dt) * $dt3);
        $lon3=$lon1 + (($dlon/$dt) * $dt3);

        $p3 = new point();
        $p3->set("lat", $lat3);
        $p3->set("lon", $lon3);

        return $p3;
    }
}
?>