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
| <?php
/**
*
* Décrit un point gps
* @author Dureuil Eric
*
*/
class gpsPoint
{
public $_lat,$_lon,$_alt,$_time;
public function __construct($la,$lo,$alt,$time)
{
$this->_lat=$la;
$this->_lon=$lo;
$this->_alt=$alt;
$this->_time=$time;
}
}
/**
*
* Décrit un chemin composé de points gps
* @author Dureuil Eric
*
*/
class gpsPath
{
private $_origin,$_reduced;
private $_la_max,$_la_min,$_lo_max,$_lo_min;
private $_e,$_w,$_h;
private $_name;
private $_dpt;
public function __construct($file)
{
$this->_origin=array();
$this->_reduced=array();
$obj = simplexml_load_file($file, 'SimpleXMLIterator');
$p=true;
foreach($obj->trk->trkseg->trkpt as $trkpt)
{
$la=doubleval($trkpt['lat']);
$lo=doubleval($trkpt['lon']);
if($p)
{
$this->_la_max=$la;
$this->_la_min=$la;
$this->_lo_max=$lo;
$this->_lo_min=$lo;
}
else
{
if($la>$this->_la_max)
$this->_la_max=$la;
elseif($la<$this->_la_min)
$this->_la_min=$la;
if($lo>$this->_lo_max)
$this->_lo_max=$lo;
elseif($lo<$this->_lo_min)
$this->_lo_min=$lo;
}
$this->_origin[]=new gpsPoint($la,$lo,floatval($trkpt->ele),$trkpt->time);
$p=false;
}
$this->_w=$this->_lo_max-$this->_lo_min;
if($this->_w==0)
$this->_w=1;
$this->_h=$this->_la_max-$this->_la_min;
if($this->_h==0)
$this->_h=1;
$this->_name=basename($file);
}
/**
*
* Trace les versions pleine résoluyion et réduite du chemin
* @param int $size taille de l'image résultat
*/
public function graph($size=800)
{
$size=intval($size);
header('Content-type: image/png');
//mise à l'échelle
$ew=$size/$this->_w;
$eh=$size/$this->_h;
$x=$this->_lo_min;
$y=$this->_la_min;
//génération de l'image
$bandeau=50;
$d=$size+$bandeau;
$im=imagecreatetruecolor($size,$size+$bandeau);
imagesetthickness($im, 1);
imagerectangle($im, 0, $bandeau, $size-1, $d-1, 0x00ffffff);
imagestring($im,5,5,0,"{$this->_name} {$this->_dpt}",0x00ffff00);
//tracé de la courbe d'origine
$cc=0x00800000;
$cp=0x00ff0000;
$j=sizeof($this->_origin);
imagestring($im, 5, 5, 15, 'original: '.$j.' pts'.($j>0?" ({$this->_origin[0]->_lat},{$this->_origin[0]->_lon})-({$this->_origin[$j-1]->_lat},{$this->_origin[$j-1]->_lon})":''), $cp);
for($i=1;$i<$j;$i++)
{
$k=$i-1;
$x0=($this->_origin[$k]->_lon-$x)*$ew;
$y0=($this->_origin[$k]->_lat-$y)*$eh;
$x1=($this->_origin[$i]->_lon-$x)*$ew;
$y1=($this->_origin[$i]->_lat-$y)*$eh;
imagesetthickness($im, 3);
imageline($im,$x0,$d-$y0,$x1,$d-$y1,$cc);
imagesetthickness($im, 5);
imagesetpixel($im, $x0, $d-$y0, $cp);
imagesetpixel($im, $x1, $d-$y1, $cp);
}
//tracé de la courbe réduite
$cc=0x00008000;
$cp=0x0000ff00;
$j=sizeof($this->_reduced);
imagestring($im, 5, 5, 30, 'reduit: '.$j.' pts'.($j>0?" ({$this->_reduced[0]->_lat},{$this->_reduced[0]->_lon})-({$this->_reduced[$j-1]->_lat},{$this->_reduced[$j-1]->_lon})":''), $cp);
for($i=1;$i<$j;$i++)
{
$k=$i-1;
$x0=($this->_reduced[$k]->_lon-$x)*$ew;
$y0=($this->_reduced[$k]->_lat-$y)*$eh;
$x1=($this->_reduced[$i]->_lon-$x)*$ew;
$y1=($this->_reduced[$i]->_lat-$y)*$eh;
imagesetthickness($im, 3);
imageline($im,$x0,$d-$y0,$x1,$d-$y1,$cc);
imagesetthickness($im, 5);
imagesetpixel($im, $x0, $d-$y0, $cp);
imagesetpixel($im, $x1, $d-$y1, $cp);
}
imagepng($im);
imagedestroy($im);
}
/**
*
* Génère la version réduite du chemin
*/
public function reduce()
{
$n=sizeof($this->_origin);
if($n<3)
$this->_reduced=$this->origin;
else
{
$this->_dpt=pow(10,log10(min($this->_w,$this->_h))-7);
if(sizeof($this->_reduced)>0)
$this->_reduced=array();
$this->_reduced[]=$this->_origin[0];
$this->dpr(0, $n-1);
$this->_reduced[]=$this->_origin[$n-1];
}
}
/**
*
* Insère le chemin réduit dans la bd
* @param object $bdd l'objet qui encapsule la connexion la bd
* @param int $id_file l'identifiant du fichier gps dans la bd
*/
public function mysql($bdd,$id_file)
{
if(sizeof($this->_reduced)>0)
{
$sql_insert_coordonnee = $bdd->prepare('INSERT INTO pevtt_coordonnee_gps (id_fichier_gps, id_coord_gps, coord_gps_lat , coord_gps_lon, coord_gps_altitude, coord_gps_date)'
. ' VALUES (:id_fichier_gps, :id_coord_gps, :coord_gps_lat, :coord_gps_lon, :coord_gps_altitude, :coord_gps_date)');
for($i=0;$i<sizeof($this->_reduced);$i++)
{
$tableau_gpx = array(
':coord_gps_lat' => $this->_reduced[$i]->_lat,
':coord_gps_lon' => $this->_reduced[$i]->_lon,
':coord_gps_altitude' => $this->_reduced[$i]->_alt,
':coord_gps_date' => $this->_reduced[$i]->_time,
':id_fichier_gps' => $id_file,
':id_coord_gps' => $i);
$sql_insert_coordonnee->execute($tableau_gpx);
}
}
}
private function dpr($begin,$end)
{
if($end<=$begin+1)
return;
$dmax=0.0;
$j=0;
for($i=$begin+1;$i<$end;$i++)
{
$d=$this->orthogonalDistance($begin, $i, $end);
if($d>$dmax)
{
$dmax=$d;
$j=$i;
//echo "$dmax $j <br/>";
}
}
if($dmax>$this->_dpt)
{
$this->dpr($begin, $j);
$this->_reduced[]=$this->_origin[$j];
$this->dpr($j, $end);
}
}
private function orthogonalDistance($begin,$point,$end)
{
$area=abs(
$this->_origin[$begin]->_lat * $this->_origin[$end]->_lon
+$this->_origin[$end]->_lat * $this->_origin[$point]->_lon
+$this->_origin[$point]->_lat * $this->_origin[$begin]->_lon
-$this->_origin[$end]->_lat * $this->_origin[$begin]->_lon
-$this->_origin[$point]->_lat * $this->_origin[$end]->_lon
-$this->_origin[$begin]->_lat * $this->_origin[$point]->_lon)/2.0;
$bottom=sqrt(pow($this->_origin[$begin]->_lat+$this->_origin[$end]->_lat,2)+pow($this->_origin[$begin]->_lon+$this->_origin[$end]->_lon,2));
return $area/$bottom*2.0;
}
}
?> |
Partager