Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 980 → Rev 981

/trunk/jrest/services/CelWidgetMap.php
23,7 → 23,9
*/
// TODO : supprimer le TRIM quand les obs seront reliées correctements aux localisations (sur le code INSEE par exemple)
class CelWidgetMap extends Cel {
const MARQUEUR_GROUPE = 'GROUPE';
const MARQUEUR_COMMUNE = 'COMMUNE';
const MARQUEUR_STATION = 'STATION';
/**
* Méthode appelée avec une requête de type GET.
*/
30,6 → 32,7
public function getElement($ressources) {
$retour = null;
extract($this->parametres);
//Chronometre::chrono("Avant groupage");
$action = array_shift($ressources);
if (isset($action)) {
42,7 → 45,9
} else {
$this->messages[] = "Vous devez indiquer le type de ressource.";
}
 
//Chronometre::chrono("Apres traitement");
//echo Chronometre::afficherChrono();
if (is_null($retour)) {
$info = 'Un problème est survenu : '.print_r($this->messages, true);
$this->envoyer($info);
49,25 → 54,53
} else if ($retour['type'] == 'jsonVar') {
$this->envoyerJsonVar($retour['variable_js'], $retour['donnees']);
} else if ($retour['type'] == 'jsonP') {
} else if ($retour['type'] == 'jsonP') {
$this->envoyerJsonp($retour['donnees']);
} else if ($retour['type'] == 'png') {
header("Content-type: image/png");
imagepng($retour['img']);
imagedestroy($retour['img']);
} else {
$this->envoyerJson($retour);
}
}
 
/**
* Les icones des groupes de stations
*/
public function getIconeGroupe($params) {
extract($this->parametres);
$img = imagecreatefrompng("/home/jpm/web/serveur/commun/icones/carto/groupe/g$type.png");
$noir = imagecolorallocate($img, 0, 0, 0);
$texte = (String) $nbre;
$x = (imagesx($img) - 6.0 * strlen($texte)) / 2;
$y = (imagesy($img) - 16) / 2;
imagestring($img, 3, $x, $y, $texte, $noir);
imagealphablending($img, false);
imagesavealpha($img, true);
return array('type' => 'png', 'img' => $img);
}
/**
* Les stations de la carte par défaut
*/
public function getStations($params) {
$json = null;
$requete = 'SELECT sector, x_utm, y_utm, wgs84_latitude AS commune_latitude, wgs84_longitude AS commune_longitude, '.
' coord_x AS point_latitude, coord_y AS point_longitude '.
$stations = null;
$requete = 'SELECT wgs84_latitude AS lat, wgs84_longitude AS lng, location AS nom '.
'FROM cel_inventory AS i '.
' LEFT JOIN locations AS l '.
' ON (l.name = i.location AND l.code = i.id_location) '.
"WHERE transmission = '1' ".
" AND (( ".
" (coord_x = '' OR coord_x IS NULL OR coord_x = '000null') ".
" AND (coord_y = '' OR coord_y IS NULL OR coord_y = '000null') ".
" ) OR mots_cles_texte LIKE '%sensible%') ".
$this->construireWhereRectangleCommune().
$this->construireWhereDept().
$this->construireWhereCommune().
$this->construireWhereUtilisateur().
77,70 → 110,231
$this->construireWhereCommentaire().
$this->construireWhereProjet().
$this->construireWhereTag();
//die($requete);
$resultats = $this->requeter($requete);
// Traitement des résultats
$json['donnees'] = $this->traiterStations($resultats);
$resultats_communes = $this->requeter($requete);
// Création des infos du widget
$json['type'] = (isset($this->formatRetour)) ? $this->formatRetour : 'jsonVar';
$json['variable_js'] = 'stations';
$requete = 'SELECT coord_x AS lat, coord_y AS lng, location AS nom '.
'FROM cel_inventory AS i '.
' LEFT JOIN locations AS l '.
' ON (l.name = i.location AND l.code = i.id_location) '.
"WHERE transmission = '1' ".
" AND coord_x != '' AND coord_x IS NOT NULL AND coord_x != '000null' ".
" AND coord_y != '' AND coord_y IS NOT NULL AND coord_y != '000null' ".
" AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE '%sensible%') ".
$this->construireWhereRectangleStation().
$this->construireWhereDept().
$this->construireWhereCommune().
$this->construireWhereUtilisateur().
$this->construireWhereNumTaxon().
$this->construireWhereNomTaxon().
$this->construireWhereDate().
$this->construireWhereCommentaire().
$this->construireWhereProjet().
$this->construireWhereTag();
$resultats_stations = $this->requeter($requete);
$stations = $this->traiterStations($resultats_communes, $resultats_stations);
return $json;
return $stations;
}
private function traiterNbreObs($resultats) {
$obs_nbre = 0;
if ($resultats !== false) {
$obs_nbre = count($resultats);
public function getTout($params) {
$emplacements = null;
$requete = 'SELECT DISTINCT location AS nom, '.
"IF ((coord_x != '000null' AND coord_x != '' ".
" AND coord_y != '000null' AND coord_y != ''), ".
"'stations', ".
"'communes' ".
") as type_emplacement, ".
"IF ((coord_x != '000null' AND coord_x != '' ".
" AND coord_y != '000null' AND coord_y != ''), ".
"ROUND(coord_x,5), ".
"ROUND(wgs84_latitude,5) ".
") as lat, ".
"IF ((coord_x != '000null' AND coord_x != '' ".
" AND coord_y != '000null' AND coord_y != ''), ".
"ROUND(coord_y,5), ".
"ROUND(wgs84_longitude,5) ".
") as lng, ".
"IF ((coord_x != '000null' AND coord_x != '' ".
" AND coord_y != '000null' AND coord_y != ''), ".
'CONCAT("'.self::MARQUEUR_STATION.':",ROUND(coord_x,5),"|",ROUND(coord_y,5)), '.
'CONCAT("'.self::MARQUEUR_COMMUNE.':",ROUND(wgs84_latitude,5),"|",ROUND(wgs84_longitude,5)) '.
") as id ".
'FROM cel_inventory AS i '.
' LEFT JOIN locations AS l '.
' ON (l.name = i.location AND l.code = i.id_location) '.
"WHERE transmission = '1' ".
" AND (".
"(".$this->construireWhereRectangleStationOR().
" AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE '%sensible%') ".
")".
" OR ".
" ( ".
$this->construireWhereRectangleCommuneOR().
" OR mots_cles_texte LIKE '%sensible%' ".
" ) ".
") ".
$this->construireWhereDept().
$this->construireWhereCommune().
$this->construireWhereUtilisateur().
$this->construireWhereNumTaxon().
$this->construireWhereNomTaxon().
$this->construireWhereDate().
$this->construireWhereCommentaire().
$this->construireWhereProjet().
$this->construireWhereTag();
$resultats_emplacements = $this->executerRequete($requete);
$emplacements = $this->traiterEmplacements($resultats_emplacements);
return $emplacements;
}
private function traiterEmplacements(&$emplacements) {
$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
$marqueurs = array(
'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
'points' => null
);
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree($emplacements, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
$nb_elements = CartoGroupage::getNbElements();
$marqueurs['stats']['stations'] = $nb_elements['stations'];
$marqueurs['stats']['communes'] = $nb_elements['communes'];
$marqueurs['stats']['observations'] = $nb_elements['observations'];
} else {
$marqueurs['points'] = $emplacements;
}
return $obs_nbre;
 
return $marqueurs;
}
private function traiterStations($resultats) {
$stations = array(
private function traiterStations($communes, $stations) {
$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
$marqueurs = array(
'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
'points' => null
);
$stations['stats']['observations'] = $this->traiterNbreObs($resultats);
$marqueurs['stats']['observations'] = $this->traiterNbreObs($communes) + $this->traiterNbreObs($stations);
$points = array();
if ($resultats !== false) {
foreach ($resultats as $enrg) {
if (!$this->etreNull($enrg['point_latitude'])&& !$this->etreNull($enrg['point_longitude'])) {
$lat = $enrg['point_latitude'];
$lng = $enrg['point_longitude'];
$id = $lat.'-'.$lng;
if ($communes !== false) {
foreach ($communes as $commune) {
if (is_numeric($commune['lat']) && is_numeric($commune['lng'])) {
extract($commune);
$id = self::MARQUEUR_COMMUNE.':'.$lat.'|'.$lng;
$this->definirLatLngMaxMin($marqueurs, $lat, $lng);
$lata = round($lat, 5);
$lnga = round($lng, 5);
if (!isset($points[$id])) {
$points[$id]['id'] = "POINT:$lat-$lng";
$points[$id]['lat'] = round($lat, 5);
$points[$id]['lng'] = round($lng, 5);
$points[$id]['id'] = $id;
$points[$id]['nom'] = $nom;
$points[$id]['lat'] = $lata;
$points[$id]['lng'] = $lnga;
$points[$id]['nbre'] = 1;
$stations['stats']['stations']++;
$marqueurs['stats']['communes']++;
} else {
$points[$id]['nbre']++;
}
} else if (!$this->etreNull($enrg['commune_latitude'])&& !$this->etreNull($enrg['commune_longitude'])) {
$lat = $enrg['commune_latitude'];
$lng = $enrg['commune_longitude'];
$id = $lat.'-'.$lng;
}
}
}
if ($stations !== false) {
foreach ($stations as $station) {
if (is_numeric($station['lat']) && is_numeric($station['lng'])) {
extract($station);
$id = self::MARQUEUR_STATION.':'.$lat.'|'.$lng;
$this->definirLatLngMaxMin($marqueurs, $lat, $lng);
$lata = round($lat, 5);
$lnga = round($lng, 5);
$nom = $this->etreNull($nom) ? $lata.','.$lnga : $nom;
if (!isset($points[$id])) {
$points[$id]['id'] = "COMMUNE:$lat-$lng";
$points[$id]['lat'] = round($lat, 5);
$points[$id]['lng'] = round($lng, 5);
$points[$id]['id'] = $id;
$points[$id]['nom'] = $nom;
$points[$id]['lat'] = $lata;
$points[$id]['lng'] = $lnga;
$points[$id]['nbre'] = 1;
$stations['stats']['communes']++;
$marqueurs['stats']['stations']++;
} else {
$points[$id]['nbre']++;
}
}
}
}
$stations['points'] = array_values($points);
}
return $stations;
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree(&$points, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
} else {
$marqueurs['points'] = $points;
}
$marqueurs['stats']['latDiff'] = abs($marqueurs['stats']['latMin'] - $marqueurs['stats']['latMax']);
$marqueurs['stats']['lngDiff'] = abs($marqueurs['stats']['lngMin'] - $marqueurs['stats']['lngMax']);
return $marqueurs;
}
private function definirLatLngMaxMin(&$marqueurs, $lat, $lng) {
if ($lat != null && $lng != null) {
$marqueurs['stats']['latMin'] = $marqueurs['stats']['latMin'] > $lat ? $lat : $marqueurs['stats']['latMin'];
$marqueurs['stats']['lngMin'] = $marqueurs['stats']['lngMin'] > $lng ? $lng : $marqueurs['stats']['lngMin'];
$marqueurs['stats']['latMax'] = $marqueurs['stats']['latMax'] < $lat ? $lat : $marqueurs['stats']['latMax'];
$marqueurs['stats']['lngMax'] = $marqueurs['stats']['lngMax'] < $lng ? $lng : $marqueurs['stats']['lngMax'];
}
}
private function traiterNbreObs($resultats) {
$obs_nbre = 0;
if ($resultats !== false) {
$obs_nbre = count($resultats);
}
return $obs_nbre;
}
private function verifierLatLng($lat, $lng) {
$ok_lat = $this->etreLatitude($lat) ? true : false;
$ok_lng = $this->etreLongitude($lng) ? true : false;
$ok = $ok_lat && $ok_lng;
return $ok;
}
private function etreLatitude($lat) {
$ok = false;
//$format = preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lat) ? true : false;
$ok = ($lat >= -90 && $lat <= 90) ? true : false;
return $ok;
}
private function etreLongitude($lng) {
$ok = false;
//$format = preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lng) ? true : false;
$ok = ($lng >= -180 && $lng <= 180) ? true : false;
return $ok;
}
private function etreObsSensible($tags) {
$sensible = true;
if (stristr($tags, 'sensible') === FALSE) {
$sensible = false;
}
return $sensible;
}
/**
* Données pour l'affichage des obs d'une station
*/
174,9 → 368,10
// Post-traitement
$observations = $this->traiterObservations($resultats, $total);
$observations = $this->ajouterImagesAuxObs($observations);
$observations = $this->ajouterAuteursAuxObs($observations);
//$observations = $this->ajouterImagesAuxObs($observations);
//$observations = $this->ajouterAuteursAuxObs($observations);
$observations = $this->supprimerIdDesObs($observations);
return $observations;
}
307,7 → 502,7
$this->construireWhereDate().
$this->construireWhereCommentaire().
$this->construireWhereProjet().
$this->construireWhereTag();
$this->construireWhereTag().
'ORDER BY nom_ret ASC '.
"LIMIT {$this->start},{$this->limit} ";
//$this->debug[] = $requete;
345,11 → 540,11
// Récupération des coordonnées depuis l'id station
extract($this->decomposerParametreStation());
if (isset($type)) {
if ($type == 'COMMUNE') {
if ($type == self::MARQUEUR_COMMUNE) {
$lat = $this->proteger($lat);
$lng = $this->proteger($lng);
$sql = " AND (wgs84_latitude = $lat AND wgs84_longitude = $lng) ";
} else if ($type == 'POINT') {
} else if ($type == self::MARQUEUR_STATION) {
$lat = $this->proteger($lat);
$lng = $this->proteger($lng);
$sql = " AND (coord_x = $lat AND coord_y = $lng) ";
583,6 → 778,92
return $sql;
}
private function construireWhereRectangleStation() {
$sql = '';
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$latMin = $sw['lat'];
$lngMin = $sw['lng'];
$latMax = $ne['lat'];
$lngMax = $ne['lng'];
// ATTENTION : coord_x correspond bien à la LATITUDE!
$sql = " AND (coord_x != 0 AND coord_y != 0) ".
" AND coord_x > $latMin ".
" AND coord_x < $latMax ".
" AND coord_y > $lngMin ".
" AND coord_y < $lngMax ";
}
return $sql;
}
private function construireWhereRectangleStationOR() {
$sql = '';
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$latMin = $sw['lat'];
$lngMin = $sw['lng'];
$latMax = $ne['lat'];
$lngMax = $ne['lng'];
// ATTENTION : coord_x correspond bien à la LATITUDE!
$sql = "( (coord_x != 0 AND coord_y != 0) ".
" AND coord_x > $latMin ".
" AND coord_x < $latMax ".
" AND coord_y > $lngMin ".
" AND coord_y < $lngMax )";
}
return $sql;
}
private function construireWhereRectangleCommune() {
$sql = '';
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$latMin = $sw['lat'];
$lngMin = $sw['lng'];
$latMax = $ne['lat'];
$lngMax = $ne['lng'];
$sql = "AND wgs84_longitude != 0 AND wgs84_latitude != 0 ".
" AND wgs84_latitude > $latMin ".
" AND wgs84_latitude < $latMax ".
" AND wgs84_longitude > $lngMin ".
" AND wgs84_longitude < $lngMax ";
}
return $sql;
}
private function construireWhereRectangleCommuneOR() {
$sql = '';
if (! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
$ne = $this->decomposerLatLng($this->parametres['ne']);
$sw = $this->decomposerLatLng($this->parametres['sw']);
$latMin = $sw['lat'];
$lngMin = $sw['lng'];
$latMax = $ne['lat'];
$lngMax = $ne['lng'];
$sql = "( wgs84_longitude != 0 AND wgs84_latitude != 0 ".
" AND wgs84_latitude > $latMin ".
" AND wgs84_latitude < $latMax ".
" AND wgs84_longitude > $lngMin ".
" AND wgs84_longitude < $lngMax )";
}
return $sql;
}
private function construireWhereDept() {
$sql = '';
// Récupération des coordonnées depuis l'id station
826,6 → 1107,15
return $mots_cles;
}
private function decomposerLatLng($coord) {
$lat_lng = array();
if (isset($coord)) {
list($lat, $lng) = explode('|', $coord);
$lat_lng = array('lat' => $lat, 'lng' => $lng);
}
return $lat_lng;
}
private function decomposerParametreStation() {
$station_infos = array();
if (isset($this->parametres['station'])) {
832,7 → 1122,7
$station = $this->parametres['station'];
$this->debug[] = $station;
list($type, $coord) = explode(':', $station);
list($lat, $lng) = explode('-', $coord);
list($lat, $lng) = explode('|', $coord);
$station_infos = array('type' => $type, 'lat' => $lat, 'lng' => $lng);
}