Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 alex 1
<?php
2
 
3
 
4
class Maillage {
5
 
34 alex 6
	private $bbox = array();
7
	private $zoom = '';
8
	private $source = '';
26 alex 9
 
34 alex 10
	private $indexLongitude = array();
11
	private $indexLatitude  = array();
12
	private $mailles = array();
26 alex 13
 
31 alex 14
	private $bdd = null;
26 alex 15
 
16
 
17
 
34 alex 18
	public function __construct($bbox, $zoom, $source) {
19
		$this->bbox   = $this->calculerLimiteBboxGlobale($bbox);
20
		$this->zoom   = $zoom;
21
		$this->source = $source;
26 alex 22
		$this->indexLongitude = array();
34 alex 23
		$this->indexLatitude  = array();
26 alex 24
		$this->mailles = array();
25
	}
26
 
34 alex 27
	private function calculerLimiteBboxGlobale($bbox) {
28
		$bboxGlobale = $bbox[0];
29
		for ($index = 1; $index < count($bbox); $index ++) {
30
			if ($bbox[$index]['ouest'] < $bboxGlobale['ouest']) {
31
				$bboxGlobale['ouest'] = $bbox[$index]['ouest'];
32
			}
33
			if ($bbox[$index]['sud'] < $bboxGlobale['sud']) {
34
				$bboxGlobale['sud'] = $bbox[$index]['sud'];
35
			}
36
			if ($bbox[$index]['est'] > $bboxGlobale['est']) {
37
				$bboxGlobale['est'] = $bbox[$index]['est'];
38
			}
39
			if ($bbox[$index]['nord'] > $bboxGlobale['nord']) {
40
				$bboxGlobale['nord'] = $bbox[$index]['nord'];
41
			}
26 alex 42
		}
34 alex 43
		return $bboxGlobale;
44
	}
26 alex 45
 
46
 
47
	public function genererMaillesVides() {
48
		$this->recupererIndexMaillesDansBbox();
49
		foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
50
			$ligne = array();
51
			foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
52
				$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
53
						$intervalleLng[1], $indexLat, $indexLng);
54
			}
55
			$this->mailles[] = $ligne;
56
		}
57
	}
58
 
59
 
60
	private function recupererIndexMaillesDansBbox() {
61
		$conditionsLongitude = "";
62
		if ($this->bbox['ouest'] >  $this->bbox['est']) {
63
			$conditionsLongitude = "NOT(debut>=".$this->bbox['ouest']." AND fin<=".$this->bbox['est'].")";
64
		} else {
65
			$conditionsLongitude = "fin>=".$this->bbox['ouest']." AND debut <=".$this->bbox['est'];
66
		}
67
		$requete =
68
			"SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=".$this->zoom." ".
69
			"AND (".
70
				"(axe='lat' AND fin>=".$this->bbox['sud']." AND debut<=".$this->bbox['nord'].") ".
71
				"OR (axe='lng' AND {$conditionsLongitude})".
72
			") ORDER BY axe, position";
73
		$indexMailles = $this->getBdd()->recupererTous($requete);
74
 
75
		foreach ($indexMailles as $index) {
76
			if ($index['axe'] == 'lng') {
77
				$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
78
			} else {
79
				$this->indexLatitude[$index['position']]  = array($index['debut'], $index['fin']);
80
			}
81
		}
34 alex 82
	}
26 alex 83
 
84
	private function getBdd() {
85
		if (is_null($this->bdd)) {
86
			$this->bdd = new Bdd();
87
		}
34 alex 88
		$nomBdd = Config::get('bdd_nom_eflore');
26 alex 89
		$this->bdd->requeter("USE ".$nomBdd);
90
		return $this->bdd;
91
	}
92
 
93
 
34 alex 94
	public function ajouterStations(& $stations) {
95
		foreach ($stations as $station) {
96
			$longitude = $station['longitude'];
97
			$latitude = $station['latitude'];
26 alex 98
			list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
34 alex 99
			$this->mailles[$indexLatitude][$indexLongitude]->ajouterStation($station, $this->source);
26 alex 100
		}
101
	}
102
 
31 alex 103
	public function ajouterMailles(& $mailles) {
104
		foreach ($mailles as $maille) {
34 alex 105
			$longitude = ($maille['ouest'] + $maille['est'])  / 2;
106
			$latitude  = ($maille['sud']   + $maille['nord']) / 2;
31 alex 107
			list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
34 alex 108
			$this->mailles[$indexLatitude][$indexLongitude]->combinerMailles($maille, $this->source);
31 alex 109
		}
110
	}
34 alex 111
 
26 alex 112
	private function rechercherIndexMaille($longitude, $latitude) {
113
		$indexLatitude = 0;
114
		$indexLongitude = 0;
115
		while ($indexLatitude < count($this->indexLatitude) - 1
116
			&& $this->mailles[$indexLatitude][0]->getLatitudeNord() < $latitude) {
117
			$indexLatitude ++;
118
		}
119
		while ($indexLongitude < count($this->indexLongitude) - 1
120
			&& $this->mailles[$indexLatitude][$indexLongitude]->getLongitudeEst() < $longitude) {
34 alex 121
				$indexLongitude ++;
26 alex 122
		}
123
		return array($indexLongitude, $indexLatitude);
124
	}
125
 
34 alex 126
	public function formaterSortie() {
26 alex 127
		$mailles_resume = array();
128
		foreach ($this->mailles as $ligne) {
129
			foreach ($ligne as $maille) {
34 alex 130
				$nombreStations = $maille->getNombrestations();
131
				if ($nombreStations > 0) {
26 alex 132
					$mailles_resume[] = array(
34 alex 133
						'type_site' => 'MAILLE',
31 alex 134
						'sud'       => $maille->getLatitudeSud(),
135
						'ouest'     => $maille->getLongitudeOuest(),
136
						'nord'      => $maille->getLatitudeNord(),
137
						'est'       => $maille->getLongitudeEst(),
34 alex 138
						'stations'     => $maille->getStations(),
139
						'observations' => $maille->getObservations()
26 alex 140
					);
141
				}
142
			}
143
		}
31 alex 144
		if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
26 alex 145
			return array();
146
		return $mailles_resume;
147
	}
148
 
149
}
150
 
151
?>