/trunk/services/modules/0.1/commun/Maille.php |
---|
New file |
0,0 → 1,74 |
<?php |
class Maille { |
private $latitudeSud; |
private $longitudeOuest; |
private $latitudeNord; |
private $longitudeEst; |
private $points; |
private $nombrePoints = NULL; |
private $indexLatitude; |
private $indexLongitude; |
public function __construct($sud, $ouest, $nord, $est, $indexLat, $indexLng) { |
$this->latitudeSud = $sud; |
$this->longitudeOuest = $ouest; |
$this->latitudeNord = $nord; |
$this->longitudeEst = $est; |
$this->indexLatitude = $indexLat; |
$this->indexLongitude = $indexLng; |
$this->points = array(); |
} |
public function ajouterPoint($point) { |
$this->points[] = $point; |
} |
public function getLatitudeNord() { |
return $this->latitudeNord; |
} |
public function getLongitudeOuest() { |
return $this->longitudeOuest; |
} |
public function getLatitudeSud() { |
return $this->latitudeSud; |
} |
public function getLongitudeEst() { |
return $this->longitudeEst; |
} |
public function getIndexLatitude() { |
return $this->indexLatitude; |
} |
public function getIndexLongitude() { |
return $this->indexLongitude; |
} |
public function getNombrePoints() { |
if (!is_null($this->nombrePoints)) |
return $this->nombrePoints; |
return count($this->points); |
} |
public function setNombrePoints($nombrePoints) { |
$this->nombrePoints = $nombrePoints; |
} |
public function getPoint($index = 0) { |
return (!isset($this->points[$index])) ? NULL : $this->points[$index]; |
} |
public function totalNonNul() { |
return !is_null($this->nombrePoints); |
} |
} |
?> |
/trunk/services/modules/0.1/commun/Referentiel.php |
---|
New file |
0,0 → 1,139 |
<?php |
define("RANG_FAMILLE", 180); |
class Referentiel { |
private $nomComplet; |
private $nomCourt; |
private $taxon = null; |
private $bdd = null; |
public function __construct($nomReferentiel) { |
$this->nomComplet = $nomReferentiel; |
$this->nomCourt = current(explode('_', $nomReferentiel)); |
} |
public function getReferentiel() { |
return $this->nomComplet; |
} |
public function getTaxon() { |
return $this->taxon; |
} |
public function setTaxon($taxon) { |
$this->taxon = $taxon; |
} |
public function recupererTaxon($numTaxon) { |
if (is_null($numTaxon) || $numTaxon == 0) { |
return null; |
} |
$methodeChamps = 'nomsChamps' . ucfirst($this->nomCourt); |
extract($this->$methodeChamps()); |
// requete SQL a executer |
$requete = "SELECT {$nn} AS nn, {$nt} AS nt, {$ns} AS nom, rang FROM {$this->nomComplet}" |
. " WHERE {$nt}={$numTaxon} AND num_nom={$nn} ORDER BY rang, If(num_nom={$nn},0,1) LIMIT 0,1"; |
$taxon = $this->getBdd()->recuperer($requete); |
return ($taxon == false ? null : $taxon); |
} |
public function recupererSousTaxons($listeNn = null) { |
if ($this->taxon['rang'] < RANG_FAMILLE) { |
// recherche de sous taxons seulement si le rang du taxon precedemment recupere |
// est du niveau de la famille ou inferieur (genre, espece) |
return array(); |
} |
$methodeChamps = 'nomsChamps' . ucfirst($this->nomCourt); |
extract($this->$methodeChamps()); |
if (is_null($listeNn)) { |
$listeNn = $this->taxon['nn']; |
} |
$requete = "SELECT {$nn} AS nn, {$nt} As nt, {$ns} AS nom FROM {$this->nomComplet} WHERE" |
. " num_tax_sup IN ({$listeNn}) AND num_nom={$nn}"; |
$sousTaxons = $this->getBdd()->recupererTous($requete); |
$listeComplete = array(); |
$nouvelleListeNn = ''; |
foreach ($sousTaxons as $sousTaxon) { |
$listeComplete[] = $sousTaxon; |
$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn']; |
} |
// recherche de sous taxons au niveau inferieur (parcours recursif de la methode) |
if (count($listeComplete) > 0) { |
$listeComplete = array_merge($listeComplete, $this->recupererSousTaxons($nouvelleListeNn)); |
} |
return $listeComplete; |
} |
private function getBdd() { |
if (is_null($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE tb_eflore"); |
return $this->bdd; |
} |
private function nomsChampsBdtfx() { |
return array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci'); |
} |
private function nomsChampsBdtxa() { |
return array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci'); |
} |
public static function listeReferentielsDisponibles() { |
$tableau = array(); |
$tableauPartiel = explode(',', Config::get('referentielsDispo')); |
$tableauPartiel = array_map('trim', $tableauPartiel); |
foreach ($tableauPartiel as $champ) { |
if (strpos($champ, '=') === false) { |
$tableau[] = $champ; |
} else { |
list($cle, $val) = explode('=', $champ); |
$clePropre = trim($cle); |
$valeurPropre = trim($val); |
$tableau[$clePropre] = $valeurPropre; |
} |
} |
return $tableau; |
} |
} |
/*$nombreCriteres = 0; |
foreach ($masques as $masque => $valeur) { |
if ($masque == 'masque.nt') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . ""; |
$nombreCriteres ++; |
} elseif ($masque == 'masque.nn') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . "num_nom = (SELECT num_nom_retenu" |
. " FROM {$this->nomComplet} WHERE num_nom={$valeur})"; |
$nombreCriteres ++; |
} elseif ($masque == 'masque.ns') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . "({$ns} LIKE '{$valeur}%'" |
. " OR num_nom=(SELECT {$nn} FROM {$this->nomComplet} WHERE {$ns} LIKE '{$valeur}%'" |
. " ORDER BY rang LIMIT 0, 1))"; |
$nombreCriteres ++; |
} |
} |
$requete .= " ";*/ |
?> |
/trunk/services/modules/0.1/commun/Maillage.php |
---|
New file |
0,0 → 1,227 |
<?php |
class Maillage { |
/** |
* @var array les coordonnees des limites de l'espace de recherche |
*/ |
private $bbox; |
/** |
* @var int le niveau actuel de zoom sur la carte (cote navigateur client) |
*/ |
private $zoom; |
/** |
* @var array le tableau indiquant la coordonnee de debut de maille sur la longitude |
*/ |
private $indexLongitude; |
/** |
* @var array le tableau indiquant la coordonnee de debut de maille sur la latitude |
*/ |
private $indexLatitude; |
/** |
* @var array le tableau a deux dimensions recouvrant l'espace de la bbox de mailles |
*/ |
private $mailles; |
public function __construct($bbox, $zoom) { |
$this->bbox = $bbox; |
$this->zoom = $zoom; |
$this->indexLongitude = array(); |
$this->indexLatitude = array(); |
$this->mailles = array(); |
} |
public function __destruct() { |
while (count($this->indexLatitude) > 0) { |
array_pop($this->indexLatitude); |
} |
while (count($this->indexLongitude) > 0) { |
array_pop($this->indexLongitude); |
} |
while (count($this->mailles) > 0) { |
array_pop($this->mailles); |
} |
unset($this); |
} |
public function genererMaillesVides() { |
$this->getIndexDansBbox(); |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) { |
$ligne = array(); |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) { |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1], |
$intervalleLng[1], $indexLat, $indexLng); |
} |
$this->mailles[] = $ligne; |
} |
} |
private function getIndexDansBbox() { |
// recuperer toutes les mailles qui couvrent l'espace a requeter |
$bdd = new Bdd(); |
$requete = "SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=" . $this->zoom |
. " AND ((axe='lat' AND fin >= " . $this->bbox['sud'] . " AND debut <= " |
. $this->bbox['nord']. ")"; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$requete .= " OR (axe='lng' AND NOT(debut >= " . $this->bbox['ouest'] . " AND fin <= " |
. $this->bbox['est'] . ")))"; |
} else { |
$requete .= " OR (axe='lng' AND fin >= " . $this->bbox['ouest'] . " AND debut <= " |
. $this->bbox['est'] . "))"; |
} |
$requete .= " ORDER BY axe, position"; |
$bdd->requeter("USE tb_eflore"); |
$indexMailles = $bdd->recupererTous($requete); |
foreach ($indexMailles as $index) { |
if ($index['axe'] == 'lng') { |
$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']); |
} else { |
$this->indexLatitude[$index['position']] = array($index['debut'], $index['fin']); |
} |
} |
} |
public function ajouterPoints($points) { |
foreach ($points as $index =>$point) { |
if (!isset($point['type_site']) || $point['type_site'] == 'STATION') { |
$longitude = $point['longitude']; |
$latitude = $point['latitude']; |
} else { |
$longitude = $point['lng_commune']; |
$latitude = $point['lat_commune']; |
} |
// recherche de l'index de la maille dans lequel placer le point |
$indexLat = 0; |
$indexLng = 0; |
while ($indexLat < count($this->indexLatitude) - 1 |
&& $this->mailles[$indexLat][0]->getLatitudeNord() < $latitude) { |
$indexLat++; |
} |
while ($indexLng < count($this->indexLongitude) - 1 |
&& $this->mailles[$indexLat][$indexLng]->getLongitudeEst() < $longitude) { |
$indexLng++; |
} |
// ajout du point dans la maille correspondante |
$this->mailles[$indexLat][$indexLng]->ajouterPoint($point); |
} |
} |
public function resumePourReponseAJAX() { |
$mailles_resume = array(); |
foreach ($this->mailles as $ligne) { |
foreach ($ligne as $maille) { |
$nombrePoints = $maille->getNombrePoints(); |
if ($nombrePoints == 0) |
continue; |
$mailles_resume[] = array( |
'zoom' => $this->zoom, |
'sud' => $maille->getLatitudeSud(), |
'ouest' => $maille->getLongitudeOuest(), |
'nord' => $maille->getLatitudeNord(), |
'est' => $maille->getLongitudeEst(), |
'points' => $nombrePoints, |
'type_site' => 'MAILLE' |
); |
} |
} |
if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0) |
return array(); |
return $mailles_resume; |
} |
public function resumePourInsertionBdd() { |
$mailles_resume = array(); |
foreach ($this->mailles as $ligne) { |
foreach ($ligne as $maille) { |
if ($maille->getNombrePoints() > 0) { |
$mailles_resume[] = array( |
'zoom' => $this->zoom, |
'sud' => $maille->getLatitudeSud(), |
'ouest' => $maille->getLongitudeOuest(), |
'nord' => $maille->getLatitudeNord(), |
'est' => $maille->getLongitudeEst(), |
'indexLat' => $maille->getIndexLatitude(), |
'indexLng' => $maille->getIndexLongitude(), |
'points' => $maille->getNombrePoints() |
); |
} |
} |
} |
if (count($mailles_resume[0]) == 0) |
return array(); |
return $mailles_resume; |
} |
public function zoomer() { |
$this->zoom += 1; |
} |
// redecoupage des mailles en 4 (fonction quadtree) |
public function redecouperMailles() { |
$bdd = new Bdd(); |
while (count($this->indexLatitude) > 0) { |
array_pop($this->indexLatitude); |
} |
while (count($this->indexLongitude) > 0) { |
array_pop($this->indexLongitude); |
} |
$mailles = $this->resumePourInsertionBdd(); |
while (count($this->mailles) > 0) { |
array_pop($this->mailles); |
} |
foreach ($mailles as $maille) { |
$indexLat = 2 * $maille['indexLat']; |
$indexLng = 2 * $maille['indexLng']; |
// rechercher les nouvelles coordonnees des mailles au niveau de zoom inferieur |
$requete = "SELECT axe,position,debut,fin FROM mailles_index WHERE zoom=". $this->zoom ." AND ((axe='lat'" |
. " AND (position={$indexLat} OR position=" . ($indexLat+1) . ")) OR (axe='lng' AND" |
. " (position={$indexLng} OR position=" . ($indexLng+1) . "))) ORDER BY If(axe='lat',0,1)"; |
$resultats = $bdd->recupererTous($requete); |
$this->indexLatitude[$indexLat] = array($resultats[0]['debut'], $resultats[0]['fin']); |
$this->indexLatitude[$indexLat+1] = array($resultats[1]['debut'], $resultats[1]['fin']); |
$this->indexLongitude[$indexLng] = array($resultats[2]['debut'], $resultats[2]['fin']); |
$this->indexLongitude[$indexLng+1] = array($resultats[3]['debut'], $resultats[3]['fin']); |
} |
ksort($this->indexLatitude); |
ksort($this->indexLongitude); |
// creer et ajouter les nouvelles mailles a partir des nouveaux index |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) { |
$ligne = array(); |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) { |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1], |
$intervalleLng[1], $indexLat, $indexLng); |
} |
$this->mailles[] = $ligne; |
} |
} |
public function getNombreMailles() { |
return count($this->mailles); |
} |
} |
?> |
/trunk/services/modules/0.1/commun/FormateurJson.php |
---|
New file |
0,0 → 1,108 |
<?php |
class FormateurJson { |
public function __construct() {} |
public function formaterStations($stations) { |
$objetJSON = new StdClass(); |
$objetJSON->type = "FeatureCollection"; |
$objetJSON->features = array(); |
if (count($stations) == 0) { |
return $objetJSON; |
} |
foreach ($stations as $station) { |
$stationJSON = NULL; |
// construction d'un objet feature adapte a la structure des donnees spatiales |
if (isset($station['sud'])) { |
$stationJSON = $this->formaterMaille($station); |
} else { |
$stationJSON = $this->formaterPoint($station); |
} |
if (!is_null($stationJSON)) { |
$objetJSON->features[] = $stationJSON; |
} |
} |
return $objetJSON; |
} |
private function formaterPoint($station) { |
$json = new StdClass(); |
$json->type = "Feature"; |
$json->geometry = new StdClass(); |
$json->properties = new StdClass(); |
$json->geometry->type = "Point"; |
if (is_null($station['latitude']) || is_null($station['longitude'])) { |
$json->properties->typeSite = 'COMMUNE'; |
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']); |
} else { |
$json->properties->typeSite = 'STATION'; |
$json->geometry->coordinates = array($station['latitude'], $station['longitude']); |
} |
$nom = $json->properties->typeSite == 'COMMUNE' ? $station['nom_commune'] : $station['station']; |
$nomDefaut = 'station sans nom'; |
if ($json->properties->typeSite == 'COMMUNE') { |
$ce = substr($station['ce_zone_geo'],-5); |
$codeDept = ((int)substr($ce, 0, 2) > 95 ? substr($ce, 0, 3) : substr($ce, 0, 2)); |
$nom = $station['zone_geo'] . " ($codeDept)"; |
} else { |
if (strlen(trim($station['zone_geo']))==0) { |
$nom = $nomDefaut; |
} else { |
$nom = $station['zone_geo']; |
if (strlen(trim($station['ce_zone_geo']))!=0) { |
$ce = substr($station['ce_zone_geo'],-5); |
$nom .= ' ('.((int)substr($ce,0,2)>95 ? substr($ce,0,3) : substr($ce,0,2)).')'; |
} |
} |
} |
$json->properties->nom = $nom; |
return $json; |
} |
private function formaterMaille($maille) { |
if ($maille['points'] == 0) { |
return null; |
} |
$json = new StdClass(); |
$json->type = "Feature"; |
$json->geometry = new StdClass(); |
$json->geometry->type = "Polygon"; |
$json->geometry->coordinates = array( |
array(floatval($maille['sud']), floatval($maille['ouest'])), |
array(floatval($maille['sud']), floatval($maille['est'])), |
array(floatval($maille['nord']), floatval($maille['est'])), |
array(floatval($maille['nord']), floatval($maille['ouest'])), |
array(floatval($maille['sud']), floatval($maille['ouest'])) |
); |
$json->properties = new StdClass(); |
$json->properties->typeSite = $maille['type_site']; |
$json->properties->nombrePoints = $maille['points']; |
return $json; |
} |
public function formaterObservations($observations, $nomSite) { |
$objetJSON = new StdClass(); |
$objetJSON->site = $nomSite; |
$objetJSON->total = count($observations); |
$objetJSON->observations = array(); |
foreach ($observations as $observation) { |
$observationJson = new stdClass(); |
foreach ($observation as $colonne => $valeur) { |
$observationJson->$colonne = $valeur; |
} |
$objetJSON->observations[] = $observationJson; |
} |
return $objetJSON; |
} |
} |
?> |