Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 976 → Rev 977

/trunk/jrest/lib/CartoGroupage.php
New file
0,0 → 1,301
<?php
class CartoGroupage {
const OFFSET = 268435456;// Moitié de la circonférence de la terre au zoom 21
const RADIUS = 85445659.4471; /* $offset / pi() */
private static $profondeurMax = 3;
private static $pasCorrectionCentre = 1;
private static $zoom = 1;
private static $seuilClusterisation = 180;
private static $debug = false;
private static $listeNoeudsSelectionnes = array();
private static $hilbert_map_1 = array ('a' => array (
"0, 0" => array (0, 'd'),
"0, 1" => array (1, 'a'),
"1, 0" => array (3, 'b'),
"1, 1" => array (2, 'a')
),
'b' => array (
"0, 0" => array (2, 'b'),
"0, 1" => array (1, 'b'),
"1, 0" => array (3, 'a'),
"1, 1" => array (0, 'c')
),
'c' => array (
"0, 0" => array (2, 'c'),
"0, 1" => array (3, 'd'),
"1, 0" => array (1, 'c'),
"1, 1" => array (0, 'b')
),
'd' => array (
"0, 0" => array (0, 'a'),
"0, 1" => array (3, 'c'),
"1, 0" => array (1, 'd'),
"1, 1" => array (2, 'd')
),
);
public static $cache = array('lat->y' => array(), 'lat->y' => array());
public static function creerGroupesManhattan($markers, $zoom) {
$clustered = array();
// Distance minimum entre deux marqueurs pour l'inclure dans un cluster à un niveau de zoom donné
$groupeRayon = (10000000 >> $zoom) / 100000;
/* Boucle permettant de définir les marqueurs de références. */
while (count($markers)) {
$marker = array_shift($markers);
$cluster = array();
/* Comparaison de la distance des autres marqueurs avec le marqueur de référence. */
foreach ($markers as $key => $target) {
$cibleDistance = abs($marker['lat'] - $target['lat']) + abs($marker['lng'] - $target['lng']);
 
if ($groupeRayon > $cibleDistance) {
unset($markers[$key]);
$cluster['cibles'][] = $target;
}
}
$clustered[] = self::ajouterGroupe($cluster, $marker);
}
return $clustered;
}
public static function creerGroupesPixel($markers, $zoom, $clusterDistance = 20) {
$clustered = array();
/* Loop until all markers have been compared. */
while (count($markers)) {
$marker = array_shift($markers);
$cluster = array();
/* Compare against all markers which are left. */
foreach ($markers as $key => $target) {
$cibleDistance = self::pixelDistance($marker['lat'], $marker['lng'], $target['lat'], $target['lng'], $zoom);
 
if ($clusterDistance > $cibleDistance) {
//$m = "Distance entre %s,%s et %s,%s vaut %d pixels.\n";
//printf($m, $marker['lat'], $marker['lng'], $target['lat'], $target['lng'], $pixels);
unset($markers[$key]);
$cluster['cibles'][] = $target;
$cluster['latMax'] = ($cluster['latMax'] < $target['lat']) ? $target['lat'] : $cluster['latMax'];
$cluster['lngMax'] = ($cluster['lngMax'] < $target['lng']) ? $target['lng'] : $cluster['lngMax'];
}
}
$clustered[] = self::ajouterGroupe($cluster, $marker);
}
return $clustered;
}
public static function ajouterGroupe($cluster, $marker) {
$groupe = array();
$nbreMarqueurs = count($cluster['cibles']);
if ($nbreMarqueurs > 0) {
$groupe['id'] = 'GROUPE:'.$marker['lat'].';'.$marker['lng'];
$groupe['lat'] = $marker['lat'];
$groupe['lng'] = $marker['lng'];
$groupe['latMax'] = $cluster['latMax'];
$groupe['lngMax'] = $cluster['lngMax'];
// +1 car le marqueur servant de point de départ n'est pas ajouté au cluster
$groupe['nbreMarqueur'] = $nbreMarqueurs + 1;
} else {
$groupe = $marker;
}
return $groupe;
}
public static function creerGroupesQuadtree2($markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
if(count($markers) > self::$seuilClusterisation) {
//$_GET['debug'] = true;
self::$zoom = $zoom;
self::$pasCorrectionCentre = 10/self::$zoom;
self::$profondeurMax = round(pow($zoom,1.05),0) - 1;
echo ($_GET['debug'] == true) ? '<pre>' : '';
echo ($_GET['debug'] == true) ? 'zoom :'.$zoom.' profondeur max '.self::$profondeurMax : '';
//echo "$neLat, $neLng, $swLat, $swLng";
$noeud = array('coordCentre' => null,'nbrePoints' => count($markers), 'points' => $markers);
self::attribuerAuCarre($noeud, $neLat, $neLng, $swLat, $swLng);
echo ($_GET['debug'] == true) ? '</pre>' : '';
} else {
foreach($markers as $marker) {
$points = array($marker);
$noeud_fictif = array('points' => $points, 'nbrePoints' => 1);
self::$listeNoeudsSelectionnes[] = self::ajouterGroupe2($noeud_fictif);
}
}
return self::$listeNoeudsSelectionnes;
}
private static function attribuerAuCarre(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
echo ($_GET['debug'] == true) ? "coordonnees parametres : $neLat, $neLng, $swLat, $swLng" : "";
//$latCentre = $noeud['coordCentre']['lat'] = ($neLat+$swLat)/2;
//$lngCentre = $noeud['coordCentre']['lng'] = ($neLng+$swLng)/2;
$latCentre = $noeud['coordCentre']['lat'] = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
$lngCentre = $noeud['coordCentre']['lng'] = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
echo ($_GET['debug'] == true) ? "\n\n profondeur : $profondeur\n" : "";
echo ($_GET['debug'] == true) ? "\t centre : $lngCentre $latCentre \n" : "";
foreach ($noeud['points'] as &$point) {
if ($point['lng'] < $lngCentre) {
if ($point['lat'] > $latCentre) {
//Carré A
$noeud['A']['points'][] = $point;
$noeud['A']['nbrePoints']++;
$noeud['A']['latMoyenne'] += $point['lat'];
$noeud['A']['lngMoyenne'] += $point['lng'];
} else {
//Carré D
$noeud['D']['points'][] = $point;
$noeud['D']['nbrePoints']++;
$noeud['D']['latMoyenne'] += $point['lat'];
$noeud['D']['lngMoyenne'] += $point['lng'];
}
} else {
if ($point['lat'] > $latCentre) {
//Carré B
$noeud['B']['points'][] = $point;
$noeud['B']['nbrePoints']++;
$noeud['B']['latMoyenne'] += $point['lat'];
$noeud['B']['lngMoyenne'] += $point['lng'];
} else {
//Carré C
$noeud['C']['points'][] = $point;
$noeud['C']['nbrePoints']++;
$noeud['C']['latMoyenne'] += $point['lat'];
$noeud['C']['lngMoyenne'] += $point['lng'];
}
}
}
$profondeur++;
$centreNoeud = array('id' => $latCentre.' '.$lngCentre, 'lat' => $latCentre, 'lng' => $lngCentre, 'nbreMarqueur' => 0);
//self::$listeNoeudsSelectionnes[] = $centreNoeud;
if($profondeur <= self::$profondeurMax) {
//echo "noeud A\n";
($noeud['A'] != null) ? self::attribuerAuCarre($noeud['A'], $neLat, $lngCentre , $latCentre, $lngSw, $profondeur) : '';
//echo "noeud B\n";
($noeud['B'] != null) ? self::attribuerAuCarre($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
//echo "noeud C\n";
($noeud['C'] != null) ? self::attribuerAuCarre($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
//echo "noeud D\n";
($noeud['D'] != null) ? self::attribuerAuCarre($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
}
//self::$listeNoeudsSelectionnes[] = self::ajouterGroupe2($noeud);
if(self::estUneFeuille($noeud['A']) && self::estUneFeuille($noeud['B']) && self::estUneFeuille($noeud['C']) && self::estUneFeuille($noeud['D'])) {
self::$listeNoeudsSelectionnes[] = self::ajouterGroupe2($noeud);
} else {
//self::$listeNoeudsSelectionnes[] = self::ajouterGroupe2($noeud);
}
}
public static function ajouterGroupe2($noeud) {
$groupe = array();
if ($noeud['nbrePoints'] > 1) {
$groupe['id'] = 'GROUPE:'.$noeud['coordCentre']['lat'].';'.$noeud['coordCentre']['lng'];
// TODO changer la génération de l'id
$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
echo ($_GET['debug'] == true) ? "\ncentre moyenne ".$groupe['lat']." ".$groupe['lng'] : "";
$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
//$groupe['latMax'] = $cluster['latMax'];
//$groupe['lngMax'] = $cluster['lngMax'];
} else {
//echo '<pre>'.print_r($noeud['points'][0],true).'</pre>';
$groupe = $noeud['points'][0];
}
return $groupe;
}
private static function estUneFeuille($noeud) {
return $noeud == null || ($noeud['A'] == null && $noeud['B'] == null && $noeud['C'] == null && $noeud['D'] == null);
}
public static function creerGroupesQuadtree($markers, $zoom) {
$ordre = $zoom - 3;
$clustered = array();
foreach ($markers as $key => $marker) {
$x = self::lonToX($marker['lng']);
$y = self::latToY($marker['lat']);
$payload = self::pointToQuadtree($x, $y, $ordre);
if (isset($clustered[$payload])) {
$clustered[$payload]['nbreMarqueur']++;
} else {
$cluster = array();
$cluster['id'] = 'GROUPE';
$cluster['lat'] = $marker['lat'];
$cluster['lng'] = $marker['lng'];
$cluster['nbreMarqueur'] = 1;
$clustered[$payload] = $cluster;
}
}
return array_values($clustered);
}
private static function pixelDistance($lat1, $lon1, $lat2, $lon2, $zoom) {
$x1 = self::lonToX($lon1);
$y1 = self::latToY($lat1);
 
$x2 = self::lonToX($lon2);
$y2 = self::latToY($lat2);
return sqrt(pow(($x1 - $x2), 2) + pow(($y1 - $y2), 2)) >> (21 - $zoom);
}
private static function lonToX($lon) {
if (! isset(self::$cache['lng->x'][$lon])) {
self::$cache['lng->x'][$lon] = round(self::OFFSET + self::RADIUS * $lon * pi() / 180);
}
return self::$cache['lng->x'][$lon];
}
private static function latToY($lat) {
if (! isset(self::$cache['lat->y'][$lat])) {
$logLat = log((1 + sin($lat * pi() / 180)) / (1 - sin($lat * pi() / 180)));
self::$cache['lat->y'][$lat] = round(self::OFFSET - self::RADIUS * $logLat / 2);
}
return self::$cache['lat->y'][$lat];
}
public static function haversineDistance($lat1, $lon1, $lat2, $lon2) {
$latd = deg2rad($lat2 - $lat1);
$lond = deg2rad($lon2 - $lon1);
$a = sin($latd / 2) * sin($latd / 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($lond / 2) * sin($lond / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return 6371.0 * $c;
}
private static function pointToQuadtree($x, $y, $order=16) {
$current_square = 'a' ;
$payload = 0;
foreach (range($order-1, 0, -1) as $i) {
$quad_x = $x & (1 << $i) ? 1 : 0;
$quad_y = $y & (1 << $i) ? 1 : 0;
list($quad_position, $current_square) = self::$hilbert_map_1[$current_square]["$quad_x, $quad_y"];
$payload .= $quad_position;
}
return $payload;
}
}
?>