Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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