Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 251 → Rev 252

/trunk/services/modules/0.1/chorodep/Cartes.php
25,6 → 25,7
const FORMAT_DEFAUT = '550';
const MIME_SVG = 'image/svg+xml';
const MIME_PNG = 'image/png';
const PRESENCE_CHOROLOGIE = '1';
 
private $config = array();
private $cheminCartesBase = '';
33,51 → 34,95
private $taxonsDemandes = array();
private $imgLargeur = 0;
private $imgHauteur = 0;
private $tableMeta = '';
private $tableOntologie = '';
private $tableChorodep = '';
private $metadonnees = '';
private $ontologies = '';
private $priorites = '';
private $version = '';
private $legende = array();
private $donnees = array();
 
public function __construct(Bdd $bdd = null, Array $config = null, CacheSimple $cache = null) {
$this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
$this->config = is_null($config) ? Config::get('Cartes') : $config;
 
$this->config = is_null($config) ? Config::get('Cartes') : $config['Cartes'];
$this->tableMeta = is_null($config) ? Config::get('bdd_table_meta') : $config['bdd_table_meta'];
$this->tableOntologie = is_null($config) ? Config::get('bdd_table_ontologies') : $config['bdd_table_ontologies'];
$this->chargerMetadonneesDerniereVersion();
$this->version = $this->metadonnees['version'];
$this->tableChorodep = 'chorodep_v'.str_replace('.', '_', $this->version);
$this->chargerOntologies();
$this->chargerLegende();
 
$this->chargerPrioritesLegende();
$this->cheminCartesBase = $this->config['chemin'];
 
$cacheOptions = array('mise_en_cache' => $this->config['cache']['miseEnCache'],
'stockage_chemin' => $this->config['cache']['stockageChemin'],
'duree_de_vie' => $this->config['cache']['dureeDeVie']);
//die(print_r($this->config, true));
$this->cache = is_null($cache) ? new CacheSimple($cacheOptions) : $cache;
}
 
private function chargerLegende() {
private function chargerMetadonneesDerniereVersion() {
$requete = 'SELECT * '.
'FROM chorodep_ontologies '.
"WHERE classe_id = 1 ";
"FROM {$this->tableMeta} ".
"ORDER BY date_creation DESC ".
"LIMIT 0,1 ";
$resultats = $this->Bdd->recuperer($requete);
 
if (!is_array($resultats) || count($resultats) <= 0) {
$message = "Les données de version n'a pu être chargée pour la ressource demandée";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
 
$this->metadonnees = $resultats;
 
}
 
private function chargerOntologies() {
$requete = 'SELECT * '.
"FROM {$this->tableOntologie} ";
$resultats = $this->Bdd->recupererTous($requete);
 
if (!is_array($resultats) || count($resultats) <= 0) {
$message = "La légende n'a pu être chargée pour la ressource demandée";
$message = "Les données de légende n'ont pu être chargées pour la ressource demandée";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
 
foreach ($resultats as $ontologie) {
$ontologie = $this->extraireComplementsOntologies($ontologie);
$this->legende[$ontologie['code']] = $ontologie['legende'];
$this->ontologies[$ontologie['id']] = $this->extraireComplementsOntologies($ontologie);
}
}
 
 
private function extraireComplementsOntologies($ontologie) {
$complements = explode(',', trim($ontologie['complements']));
foreach ($complements as $complement) {
list($cle, $val) = explode('=', trim($complement));
$ontologie[trim($cle)] = trim($val);
if ($ontologie['complements'] != '') {
$complements = explode(',', trim($ontologie['complements']));
foreach ($complements as $complement) {
list($cle, $val) = explode('=', trim($complement));
$ontologie[trim($cle)] = trim($val);
}
}
return $ontologie;
}
 
private function chargerLegende() {
foreach ($this->ontologies as $ontologie) {
if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE) {
$this->legende[$ontologie['code']] = $ontologie['legende'];
}
}
}
 
private function chargerPrioritesLegende() {
foreach ($this->ontologies as $ontologie) {
if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE && isset($ontologie['priorite'])) {
$this->priorites[$ontologie['code']] = $ontologie['priorite'];
}
}
}
 
public function consulter($ressources, $parametres) {
//$tpsDebut = microtime(true);
$this->parametres = $parametres;
85,7 → 130,7
 
$this->definirValeurParDefautDesParametres();
$this->verifierParametres();
$this->analyserIdentifiant();
$this->analyserRessources();
 
$resultat = new ResultatService();
$this->chargerDonnees();
114,9 → 159,6
private function verifierParametres() {
$erreurs = array();
 
if ($this->verifierIdentifiants() == false) {
$erreurs[] = "L'identifiant de ressource indiqué ne respecte pas le format attendu.";
}
if (isset($this->parametres['retour']) == false) {
$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
}
138,17 → 180,6
}
}
 
private function verifierIdentifiants() {
$ok = true;
if (isset($this->ressources[0])) {
$ids = $this->ressources[0];
$projetPattern = '(?:(?:[A-Z0-9]+(\.(?:nn|nt)?):)?(?:[0-9]+,)*[0-9]+)';
$patternComplet = "/^$projetPattern(?:;$projetPattern)*$/i";
$ok = preg_match($patternComplet, $ids) ? true : false;
}
return $ok;
}
 
private function verifierValeurParametreRetour() {
return in_array($this->parametres['retour'], $this->formats_supportes);
}
163,35 → 194,69
return $ok;
}
 
private function analyserIdentifiant() {
if (isset($this->ressources[0])) {
$ids = $this->ressources[0];
if (preg_match('/^[0-9]+$/', $ids)) {
$this->taxonsDemandes[self::CODE_REFTAX_DEFAUT]['nn'][] = $ids;
private function analyserRessources() {
if (count($this->ressources) == 1) {
$positionIds = 0;
if ($this->etreRessourceIdentifiants($positionIds)) {
$ids = $this->ressources[$positionIds];
$this->analyserIdentifiants($ids);
// TODO : charger une nouvelle classe executant le sous service.
} else {
// ceci contient potentiellement des formes ref_tax1.nn:1,2;ref_tax2.nt:3,4
throw new Exception("A implémenter : ressource id multiples");
$projetsListeEtNumNoms = explode(';', $nn);
if (count($projetsListeEtNumNoms) > 0) {
foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : self::CODE_REFTAX_DEFAUT.':'.$projetEtNumNoms;
list($projet, $numNoms) = explode(':', $projetEtNumNoms);
$this->ref_tax_demande[$projet] = explode(',', $numNoms);
}
throw new Exception("A implémenter : carte proportionnelle ensemble des infos");
}
} else if (count($this->ressources) == 2) {
if ($this->etreIdentifiants(0)) {
$ids = $this->ressources[$positionIds];
$this->analyserIdentifiants($ids);
}
if ($this->etreRessourceLegende(1)) {
// TODO : charger une nouvelle classe executant le sous service.
}
}
}
 
private function etreRessourceIdentifiants($position) {
$ok = true;
if (isset($this->ressources[$position])) {
$ids = $this->ressources[$position];
$projetPattern = '(?:(?:[A-Z0-9]+(\.(?:nn|nt)?):)?(?:[0-9]+,)*[0-9]+)';
$patternComplet = "/^$projetPattern(?:;$projetPattern)*$/i";
$ok = preg_match($patternComplet, $ids) ? true : false;
}
return $ok;
}
 
private function analyserIdentifiants($ids) {
if (preg_match('/^[0-9]+$/', $ids)) {
$this->taxonsDemandes[self::CODE_REFTAX_DEFAUT]['nn'][] = $ids;
} else {
// ceci contient potentiellement des formes ref_tax1.nn:1,2;ref_tax2.nt:3,4
$projetsListeEtNumNoms = explode(';', $ids);
if (count($projetsListeEtNumNoms) > 0) {
foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : self::CODE_REFTAX_DEFAUT.'.nn:'.$projetEtNumNoms;
list($projetEtType, $numNoms) = explode(':', $projetEtNumNoms);
list($projet, $type) = explode('.', $projetEtType);
$this->taxonsDemandes[$projet][$type] = explode(',', $numNoms);
}
}
} else {
throw new Exception("A implémenter : carte proportionnelle ensemble des infos");
}
}
 
private function etreRessourceLegende($position) {
$ok = true;
if (isset($this->ressources[$position])) {
$legende = $this->ressources[$position];
$ok = ($legende == 'legende') ? true : false;
}
return $ok;
}
 
private function chargerDonnees() {
$refTax = self::CODE_REFTAX_DEFAUT;
$numNom = $this->Bdd->proteger($this->taxonsDemandes[$refTax]['nn'][0]);
 
$conditions = $this->getConditions();
$requete = 'SELECT * '.
'FROM chorodep_v2012_01 '.
"WHERE num_nom IN ($numNom) ";
"FROM {$this->tableChorodep} ".
(isset($conditions) ? 'WHERE '.implode(' AND ', $conditions) : '');
$resultat = $this->Bdd->recupererTous($requete);
 
if (!is_array($resultat) || count($resultat) <= 0) {
202,6 → 267,47
$this->donnees = $resultat;
}
 
private function getConditions() {
$conditions = null;
if ($nnListe = $this->getListeNumNom()) {
$conditions[] = "num_nom IN ($nnListe) ";
}
if ($ntListe = $this->getListeNumTax()) {
$conditions[] = "num_tax IN ($ntListe) ";
}
return $conditions;
}
 
private function getListeNumNom() {
$nnListe = null;
$refTax = self::CODE_REFTAX_DEFAUT;
if (isset($this->taxonsDemandes[$refTax])) {
$nnProteges = array();
if (isset($this->taxonsDemandes[$refTax]['nn'])) {
foreach ($this->taxonsDemandes[$refTax]['nn'] as $nn) {
$nnProteges[] = $this->Bdd->proteger($nn);
}
$nnListe = implode(',', $nnProteges);
}
}
return $nnListe;
}
 
private function getListeNumTax() {
$ntListe = null;
$refTax = self::CODE_REFTAX_DEFAUT;
if (isset($this->taxonsDemandes[$refTax])) {
$ntProteges = array();
if (isset($this->taxonsDemandes[$refTax]['nt'])) {
foreach ($this->taxonsDemandes[$refTax]['nt'] as $nt) {
$ntProteges[] = $this->Bdd->proteger($nt);
}
$ntListe = implode(',', $ntProteges);
}
}
return $ntListe;
}
 
private function genererSVG() {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->validateOnParse = true;
213,8 → 319,11
$racineElement->setAttribute('width', $this->imgLargeur);
 
$css = $this->creerCssCarte();
$styleElement = $dom->getElementsByTagName('style')->item(0);
$css = $styleElement->nodeValue.$css;
$txtCss = $dom->createCDATASection($css);
$dom->getElementsByTagName('style')->item(0)->appendChild($txtCss);
$styleElement->nodeValue = '';
$styleElement->appendChild($txtCss);
 
$titre = $this->creerTitre();
$titreCdata = $dom->createCDATASection($titre);
227,6 → 336,7
$xpath = new DOMXPath($dom);
$taxonTitreEl = $xpath->query("//*[@id='titre-taxon']")->item(0);
$taxonTitreEl->nodeValue = '';
$taxonTitreEl->setAttribute('title', $taxonTitre);
$taxonTitreEl->appendChild($taxonCdata);
 
$svg = $dom->saveXML();
234,9 → 344,8
}
 
private function creerCssCarte() {
$fichierCssBase = $this->cheminCartesBase.self::CARTE_DEFAUT.'.css';
$css = file_get_contents($fichierCssBase);
 
$css = '';
$this->getZonesPriorites();
$zonesCouleurs = $this->getZonesCouleurs();
foreach ($zonesCouleurs as $couleur => $zonesClasses) {
$classes = implode(', ', $zonesClasses);
245,18 → 354,35
return $css;
}
 
private function getZonesCouleurs() {
$zones = array();
private function getZonesPriorites() {
$this->zones = array();
$zonesPrioritaires = array();
foreach ($this->donnees as $donnee) {
foreach ($donnee as $champ => $valeur) {
if (preg_match('/^[0-9][0-9ab]$/i', $champ)) {
if (array_key_exists($valeur, $this->legende)) {
$couleur = $this->legende[$valeur];
$zones[$couleur][] = strtolower(".departement$champ");
foreach ($donnee as $zoneId => $codeLegende) {
if (preg_match('/^[0-9][0-9ab]$/i', $zoneId)) {
if (array_key_exists($codeLegende, $this->priorites)) {
$priorite = $this->priorites[$codeLegende];
if (array_key_exists($zoneId, $zonesPrioritaires) == false) {
$zonesPrioritaires[$zoneId] = 0;
}
if ($priorite > $zonesPrioritaires[$zoneId]) {
$zonesPrioritaires[$zoneId] = $priorite;
$this->zones[$zoneId] = $codeLegende;
}
}
}
}
}
}
 
private function getZonesCouleurs() {
$zones = array();
foreach ($this->zones as $zoneId => $codeLegende) {
if (array_key_exists($codeLegende, $this->legende)) {
$couleur = $this->legende[$codeLegende];
$zones[$couleur][] = strtolower(".departement$zoneId");
}
}
return $zones;
}