Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 delphine 1
<?php
2
 
3
 
4
class Maillage {
5
 
6
	/**
7
	 * @var array les coordonnees des limites de l'espace de recherche
8
	 */
9
	private $bbox;
10
 
11
	/**
12
	 * @var int le niveau actuel de zoom sur la carte (cote navigateur client)
13
	 */
14
	private $zoom;
15
 
16
	/**
17
	 * @var array le tableau indiquant la coordonnee de debut de maille sur la longitude
18
	 */
19
	private $indexLongitude;
20
 
21
	/**
22
	 * @var array le tableau indiquant la coordonnee de debut de maille sur la latitude
23
	 */
24
	private $indexLatitude;
25
 
26
	/**
27
	 * @var array le tableau a deux dimensions recouvrant l'espace de la bbox de mailles
28
	 */
29
	private $mailles;
30
 
31
 
32
 
33
 
34
	public function __construct($bbox, $zoom) {
35
		$this->bbox = $bbox;
36
		$this->zoom = $zoom;
37
		$this->indexLongitude = array();
38
		$this->indexLatitude = array();
39
		$this->mailles = array();
40
	}
41
 
42
	public function __destruct() {
43
		while (count($this->indexLatitude) > 0) {
44
			array_pop($this->indexLatitude);
45
		}
46
		while (count($this->indexLongitude) > 0) {
47
			array_pop($this->indexLongitude);
48
		}
49
 
50
		while (count($this->mailles) > 0) {
51
			array_pop($this->mailles);
52
		}
53
		unset($this);
54
	}
55
 
56
 
57
	public function genererMaillesVides() {
58
		$this->getIndexDansBbox();
59
		foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
60
			$ligne = array();
61
			foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
62
				$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
63
						$intervalleLng[1], $indexLat, $indexLng);
64
			}
65
			$this->mailles[] = $ligne;
66
		}
67
	}
68
 
69
 
70
	private function getIndexDansBbox() {
71
		// recuperer toutes les mailles qui couvrent l'espace a requeter
72
		$bdd = new Bdd();
73
		$requete = "SELECT  axe, position, debut, fin FROM mailles_index WHERE zoom=" . $this->zoom
74
			. " AND ((axe='lat' AND fin >= " . $this->bbox['sud'] . " AND debut <= "
75
			. $this->bbox['nord']. ")";
76
		if ($this->bbox['ouest'] >  $this->bbox['est']) {
77
			$requete .= " OR (axe='lng' AND NOT(debut >= " . $this->bbox['ouest'] . " AND fin <= "
78
				.  $this->bbox['est'] . ")))";
79
		} else {
80
			$requete .= " OR (axe='lng' AND fin >= " . $this->bbox['ouest'] . " AND debut <= "
81
				.  $this->bbox['est'] . "))";
82
		}
83
		$requete .= " ORDER BY axe, position";
84
		$bdd->requeter("USE tb_eflore");
85
		$indexMailles = $bdd->recupererTous($requete);
86
 
87
		foreach ($indexMailles as $index) {
88
			if ($index['axe'] == 'lng') {
89
				$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
90
			} else {
91
				$this->indexLatitude[$index['position']]  = array($index['debut'], $index['fin']);
92
			}
93
		}
94
	}
95
 
96
 
97
	public function ajouterPoints($points) {
98
		foreach ($points as $index =>$point) {
99
			if (!isset($point['type_site']) || $point['type_site'] == 'STATION') {
100
				$longitude = $point['longitude'];
101
				$latitude = $point['latitude'];
102
			} else {
103
				$longitude = $point['lng_commune'];
104
				$latitude = $point['lat_commune'];
105
			}
106
 
107
			// recherche de l'index de la maille dans lequel placer le point
108
			$indexLat = 0;
109
			$indexLng = 0;
110
			while ($indexLat < count($this->indexLatitude) - 1
111
				&& $this->mailles[$indexLat][0]->getLatitudeNord() < $latitude) {
112
				$indexLat++;
113
			}
114
			while ($indexLng < count($this->indexLongitude) - 1
115
				&& $this->mailles[$indexLat][$indexLng]->getLongitudeEst() < $longitude) {
116
				$indexLng++;
117
			}
118
			// ajout du point dans la maille correspondante
119
			$this->mailles[$indexLat][$indexLng]->ajouterPoint($point);
120
		}
121
	}
122
 
123
 
124
 
125
	public function resumePourReponseAJAX() {
126
		$mailles_resume = array();
127
		foreach ($this->mailles as $ligne) {
128
			foreach ($ligne as $maille) {
129
				$nombrePoints = $maille->getNombrePoints();
130
				if ($nombrePoints == 0)
131
					continue;
132
				$mailles_resume[] = array(
133
					'zoom'      => $this->zoom,
134
					'sud'       => $maille->getLatitudeSud(),
135
					'ouest'     => $maille->getLongitudeOuest(),
136
					'nord'      => $maille->getLatitudeNord(),
137
					'est'       => $maille->getLongitudeEst(),
138
					'points'    => $nombrePoints,
139
					'type_site' => 'MAILLE'
140
				);
141
			}
142
		}
143
		if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
144
			return array();
145
		return $mailles_resume;
146
	}
147
 
148
	public function resumePourInsertionBdd() {
149
		$mailles_resume = array();
150
		foreach ($this->mailles as $ligne) {
151
			foreach ($ligne as $maille) {
152
				if ($maille->getNombrePoints() > 0) {
153
					$mailles_resume[] = array(
154
						'zoom'     => $this->zoom,
155
						'sud'      => $maille->getLatitudeSud(),
156
						'ouest'    => $maille->getLongitudeOuest(),
157
						'nord'     => $maille->getLatitudeNord(),
158
						'est'      => $maille->getLongitudeEst(),
159
						'indexLat' => $maille->getIndexLatitude(),
160
						'indexLng' => $maille->getIndexLongitude(),
161
						'points'   => $maille->getNombrePoints()
162
					);
163
				}
164
			}
165
		}
166
		if (count($mailles_resume[0]) == 0)
167
			return array();
168
		return $mailles_resume;
169
	}
170
 
171
 
172
	public function zoomer() {
173
		$this->zoom += 1;
174
	}
175
 
176
	// redecoupage des mailles en 4 (fonction quadtree)
177
	public function redecouperMailles() {
178
		$bdd = new Bdd();
179
		while (count($this->indexLatitude) > 0) {
180
			array_pop($this->indexLatitude);
181
		}
182
		while (count($this->indexLongitude) > 0) {
183
			array_pop($this->indexLongitude);
184
		}
185
 
186
		$mailles = $this->resumePourInsertionBdd();
187
		while (count($this->mailles) > 0) {
188
			array_pop($this->mailles);
189
		}
190
 
191
		foreach ($mailles as $maille) {
192
			$indexLat = 2 * $maille['indexLat'];
193
			$indexLng = 2 * $maille['indexLng'];
194
			// rechercher les nouvelles coordonnees des mailles au niveau de zoom inferieur
195
			$requete = "SELECT axe,position,debut,fin FROM mailles_index WHERE zoom=". $this->zoom ." AND ((axe='lat'"
196
				. " AND (position={$indexLat} OR position=" . ($indexLat+1) . ")) OR (axe='lng' AND"
197
				. " (position={$indexLng}  OR position=" . ($indexLng+1) . "))) ORDER BY If(axe='lat',0,1)";
198
			$resultats = $bdd->recupererTous($requete);
199
 
200
			$this->indexLatitude[$indexLat]    = array($resultats[0]['debut'], $resultats[0]['fin']);
201
			$this->indexLatitude[$indexLat+1]  = array($resultats[1]['debut'], $resultats[1]['fin']);
202
			$this->indexLongitude[$indexLng]   = array($resultats[2]['debut'], $resultats[2]['fin']);
203
			$this->indexLongitude[$indexLng+1] = array($resultats[3]['debut'], $resultats[3]['fin']);
204
		}
205
		ksort($this->indexLatitude);
206
		ksort($this->indexLongitude);
207
 
208
		// creer et ajouter les nouvelles mailles a partir des nouveaux index
209
		foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
210
			$ligne = array();
211
			foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
212
				$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
213
					$intervalleLng[1], $indexLat, $indexLng);
214
			}
215
			$this->mailles[] = $ligne;
216
		}
217
 
218
	}
219
 
220
	public function getNombreMailles() {
221
		return count($this->mailles);
222
	}
223
 
224
 
225
}
226
 
227
?>