Rev 1472 | Blame | Last modification | View Log | RSS feed
<?php
class CartoGroupage {
const MARQUEUR_GROUPE = 'GROUPE';
const MARQUEUR_COMMUNE = 'COMMUNE';
const MARQUEUR_STATION = 'STATION';
private static $seuilClusterisation = 200;
private static $zoomDefaut = 3;
private static $zoomMaxClustering = 12;
private static $pasZoomDefaut = 1;
private static $pasZoomMaxClustering = 0.05;
private static $profondeurMin = 0;
private static $profondeurMax = 8;
private static $pasCorrectionCentre = null;
private static $coefficientReductionPas = null;
private static $coefficientProfondeurMax = null;
private static $nbElements = array('stations' => 0,'communes' => 0, 'points' => 0);
private static $listeNoeudsSelectionnes = array();
private static $bornesMax = array('latMin' => null, 'lngMin' => null, 'latMax' => null, 'lngMax' => null);
private static $id_traites = array();
/*
+---------+---------+
| | |
| A | B |
| | |
+---------*---------+
| | |
| D | C |
| | |
+---------+---------+
Quatres cadrans sont considérés par le quad tree
* = centre de la fenetre
*/
public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
if(count($markers) > self::$seuilClusterisation) {
self::calculerProfondeurMax($zoom);
self::calculerPasCorrectionCentre($zoom);
$noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);
} else {
foreach($markers as &$marker) {
if(!self::estUnPointAExclure($marker)) {
$emplacement = self::formaterPointPourAjout(&$marker);
self::mettreAJourBornes(&$marker);
$points = array($marker);
$noeudSimple = array('points' => $points, 'nbrePoints' => 1);
self::$nbElements[$emplacement]++;
self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
}
self::$nbElements['points']++;
}
}
return self::$listeNoeudsSelectionnes;
}
private function calculerCoefficientReductionPas() {
if(self::$coefficientReductionPas == null) {
self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
}
return self::$coefficientReductionPas;
}
private function calculerPasCorrectionCentre($zoom) {
self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
}
private function calculerCoefficientProfondeurMax() {
if(self::$coefficientProfondeurMax == null) {
self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
}
return self::$coefficientProfondeurMax;
}
private function calculerProfondeurMax($zoom) {
if($zoom > self::$zoomDefaut) {
self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
} else {
self::$profondeurMax = 1;
}
}
public static function getNbElements() {
return self::$nbElements;
}
public static function mettreAJourBornes(&$point) {
if(!is_numeric($point['lat']) || abs($point['lat']) > 90) return;
if(!is_numeric($point['lng']) || abs($point['lng']) > 180) return;
self::$bornesMax['latMin'] = ($point['lat'] < self::$bornesMax['latMin'] || self::$bornesMax['latMin'] == null) ? $point['lat'] : self::$bornesMax['latMin'] ;
self::$bornesMax['lngMin'] = ($point['lng'] < self::$bornesMax['lngMin'] || self::$bornesMax['lngMin'] == null) ? $point['lng'] : self::$bornesMax['lngMin'] ;
self::$bornesMax['latMax'] = ($point['lat'] > self::$bornesMax['latMax'] || self::$bornesMax['latMax'] == null) ? $point['lat'] : self::$bornesMax['latMax'] ;
self::$bornesMax['lngMax'] = ($point['lng'] > self::$bornesMax['lngMax'] || self::$bornesMax['lngMax'] == null) ? $point['lng'] : self::$bornesMax['lngMax'] ;
}
public static function getBornes() {
return self::$bornesMax;
}
/**
*
* @param mixed $noeud Le noeud à traiter par le quadtree
* @param float $neLat Latitude du coin nord est de la fenetre
* @param float $neLng Longitude du coin nord est de la fenetre
* @param float $swLat Latitude du coin sud ouest de la fenetre
* @param float $swLng Longitude du coin sud ouest de la fenetre
* @param int $profondeur profondeur courante de l'arbre
*/
private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
foreach ($noeud['points'] as &$point) {
if(!self::estUnPointAExclure($point)) {
$emplacement = self::formaterPointPourAjout(&$point);
self::mettreAJourBornes(&$point);
$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
self::ajouterFils($noeud,$cadran,$point);
self::$nbElements[$emplacement]++;
}
self::$nbElements['points']++;
}
$profondeur++;
if($profondeur <= self::$profondeurMax) {
(isset($noeud['A']) && $noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
(isset($noeud['B']) && $noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
(isset($noeud['C']) && $noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
(isset($noeud['D']) && $noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
}
if(self::estUnParentFeuilles($noeud)) {
self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
}
}
private static function estUnPointAExclure(&$point) {
return self::estSensible($point) &&
self::coordonneesCommuneSontNulles($point);
}
private static function coordonneesCommuneSontNulles(&$point) {
$coord_nulles = ($point['wgs84_latitude'] == null ||
$point['wgs84_latitude'] == '' ||
$point['wgs84_longitude'] == null ||
$point['wgs84_longitude'] == '');
return $coord_nulles;
}
private static function coordonneesSontNulles(&$point) {
$coord_nulles = ($point['latitude'] == '000null' ||
$point['latitude'] == 0 ||
$point['latitude'] == '' ||
$point['longitude'] == '000null' ||
$point['longitude'] == 0 ||
$point['longitude'] == '');
return $coord_nulles;
}
private static function estSensible(&$point) {
$sensible = isset($point['mots_cles_texte']) && substr_count(strtolower($point['mots_cles_texte']), 'sensible') != 0;
return $sensible;
}
private static function formaterNomStation(&$point, $type_emplacement) {
$station = '';
if($type_emplacement == 'stations' && $point['station'] != '' && $point['station'] != '000null') {
$station = $point['station'];
} else {
$id_zone_geo = $point['ce_zone_geo'];
$station = $point['zone_geo'].(($id_zone_geo != '' && $id_zone_geo != '000null') ? ' ('.self::formaterNomZoneGeo($id_zone_geo).')' : '');
}
return $station;
}
private static function formaterNomZoneGeo($zone_geo) {
$zone_geo = str_replace('INSEE-C:', '', $zone_geo);
$zone_geo = strlen($zone_geo) >= 2 ? substr($zone_geo, 0, 2) : $zone_geo;
return $zone_geo;
}
private static function formaterPointPourAjout(&$point) {
if(isset($point['type_emplacement'])) {
return $point['type_emplacement'];
}
if(self::coordonneesSontNulles($point) || self::estSensible($point)) {
$point_allege = array();
$point_allege['id'] = self::MARQUEUR_COMMUNE.':'.$point['wgs84_latitude'].'|'.$point['wgs84_longitude'];
$point_allege['type_emplacement'] = 'communes';
$point_allege['nom'] = self::formaterNomStation($point, 'communes');
$point_allege['lat'] = (float)$point['wgs84_latitude'];
$point_allege['lng'] = (float)$point['wgs84_longitude'];
$point = $point_allege;
} else {
$point_allege = array();
$point_allege['id'] = self::MARQUEUR_STATION.':'.$point['latitude'].'|'.$point['longitude'];
$point_allege['type_emplacement'] = 'stations';
$point_allege['nom'] = self::formaterNomStation($point, 'stations');
$point_allege['lat'] = (float)$point['latitude'];
$point_allege['lng'] = (float)$point['longitude'];
$point = $point_allege;
}
return $point['type_emplacement'];
}
private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
if ($point['lng'] < $lngCentre) {
if ($point['lat'] > $latCentre) {
$cadran = 'A';
} else {
$cadran = 'D';
}
} else {
if ($point['lat'] > $latCentre) {
$cadran = 'B';
} else {
$cadran = 'C';
}
}
return $cadran;
}
private static function ajouterFils(&$noeud, $cadran, &$point) {
if(!isset($noeud[$cadran])) {
$noeud[$cadran] = array('points' => array(),'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
}
$noeud[$cadran]['points'][] = $point;
$noeud[$cadran]['nbrePoints']++;
$noeud[$cadran]['latMoyenne'] += $point['lat'];
$noeud[$cadran]['lngMoyenne'] += $point['lng'];
}
private static function ajouterGroupeOuPoint(&$noeud) {
$groupe = array();
if ($noeud['nbrePoints'] > 1 && isset($noeud['latMoyenne']) && isset($noeud['lngMoyenne'])) {
$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
} else {
$groupe = $noeud['points'][0];
}
return $groupe;
}
private static function estUnParentFeuilles(&$noeud) {
return self::estUneFeuille($noeud['A']) &&
self::estUneFeuille($noeud['B']) &&
self::estUneFeuille($noeud['C']) &&
self::estUneFeuille($noeud['D']);
}
private static function estUneFeuille(&$noeud) {
return $noeud == null ||
(!isset($noeud['A']) || $noeud['A'] == null) &&
(!isset($noeud['B']) || $noeud['B'] == null) &&
(!isset($noeud['C']) || $noeud['C'] == null) &&
(!isset($noeud['D']) || $noeud['D'] == null);
}
}
?>