Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Go to most recent revision | 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
 
6
	private $bbox;
7
	private $zoom;
8
 
9
	private $indexLongitude;
10
	private $indexLatitude;
11
	private $mailles;
12
 
31 alex 13
	private $bdd = null;
26 alex 14
 
15
 
16
 
17
	public function __construct($bbox, $zoom) {
18
		$this->bbox = $bbox;
19
		$this->zoom = $zoom;
20
		$this->indexLongitude = array();
21
		$this->indexLatitude = array();
22
		$this->mailles = array();
23
	}
24
 
25
	public function __destruct() {
26
		while (count($this->indexLatitude) > 0) {
27
			array_pop($this->indexLatitude);
28
		}
29
		while (count($this->indexLongitude) > 0) {
30
			array_pop($this->indexLongitude);
31
		}
32
 
33
		while (count($this->mailles) > 0) {
34
			array_pop($this->mailles);
35
		}
36
		unset($this);
37
	}
38
 
39
 
40
	public function genererMaillesVides() {
41
		$this->recupererIndexMaillesDansBbox();
42
		foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
43
			$ligne = array();
44
			foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
45
				$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
46
						$intervalleLng[1], $indexLat, $indexLng);
47
			}
48
			$this->mailles[] = $ligne;
49
		}
50
	}
51
 
52
 
53
	private function recupererIndexMaillesDansBbox() {
54
		// recuperer toutes les mailles qui couvrent l'espace a requeter
55
		$conditionsLongitude = "";
56
		if ($this->bbox['ouest'] >  $this->bbox['est']) {
57
			$conditionsLongitude = "NOT(debut>=".$this->bbox['ouest']." AND fin<=".$this->bbox['est'].")";
58
		} else {
59
			$conditionsLongitude = "fin>=".$this->bbox['ouest']." AND debut <=".$this->bbox['est'];
60
		}
61
		$requete =
62
			"SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=".$this->zoom." ".
63
			"AND (".
64
				"(axe='lat' AND fin>=".$this->bbox['sud']." AND debut<=".$this->bbox['nord'].") ".
65
				"OR (axe='lng' AND {$conditionsLongitude})".
66
			") ORDER BY axe, position";
67
		$indexMailles = $this->getBdd()->recupererTous($requete);
68
 
31 alex 69
 
26 alex 70
		foreach ($indexMailles as $index) {
71
			if ($index['axe'] == 'lng') {
72
				$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
73
			} else {
74
				$this->indexLatitude[$index['position']]  = array($index['debut'], $index['fin']);
75
			}
76
		}
77
	}
78
 
79
	private function getBdd() {
80
		if (is_null($this->bdd)) {
81
			$this->bdd = new Bdd();
82
		}
83
		$nomBdd = Config::get('bdd_eflore');
84
		if (is_null($nomBdd)) {
85
			$nomBdd = Config::get('bdd_nom');
86
		}
87
		$this->bdd->requeter("USE ".$nomBdd);
88
		return $this->bdd;
89
	}
90
 
91
 
92
	public function ajouterPoints(& $points) {
31 alex 93
		foreach ($points as $point) {
26 alex 94
			list($longitude, $latitude) = $this->obtenirCoordonneesPoint($point);
95
			list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
96
			$this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
97
		}
98
	}
99
 
31 alex 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
	}
108
 
26 alex 109
	private function obtenirCoordonneesPoint($point) {
110
		$longitude = 0;
111
		$latitude = 0;
112
		if (!isset($point['type_site']) || $point['type_site'] == 'STATION') {
113
			$longitude = $point['longitude'];
114
			$latitude = $point['latitude'];
115
		} else {
116
			$longitude = $point['lng_commune'];
117
			$latitude = $point['lat_commune'];
118
		}
119
		return array($longitude, $latitude);
120
	}
121
 
122
	private function rechercherIndexMaille($longitude, $latitude) {
123
		$indexLatitude = 0;
124
		$indexLongitude = 0;
125
		while ($indexLatitude < count($this->indexLatitude) - 1
126
			&& $this->mailles[$indexLatitude][0]->getLatitudeNord() < $latitude) {
127
			$indexLatitude ++;
128
		}
129
		while ($indexLongitude < count($this->indexLongitude) - 1
130
			&& $this->mailles[$indexLatitude][$indexLongitude]->getLongitudeEst() < $longitude) {
131
			$indexLongitude ++;
132
		}
133
		return array($indexLongitude, $indexLatitude);
134
	}
135
 
31 alex 136
	public function formaterSortie($toutesLesMailles = false) {
26 alex 137
		$mailles_resume = array();
138
		foreach ($this->mailles as $ligne) {
139
			foreach ($ligne as $maille) {
140
				$nombrePoints = $maille->getNombrePoints();
31 alex 141
				if ($nombrePoints > 0 || $toutesLesMailles) {
26 alex 142
					$mailles_resume[] = array(
31 alex 143
						'zoom'      => $this->zoom,
144
						'sud'       => $maille->getLatitudeSud(),
145
						'ouest'     => $maille->getLongitudeOuest(),
146
						'nord'      => $maille->getLatitudeNord(),
147
						'est'       => $maille->getLongitudeEst(),
148
						'points'    => $nombrePoints,
149
						'observations' => $maille->getNombreObservations(),
150
						'type_site' => 'MAILLE'
26 alex 151
					);
152
				}
153
			}
154
		}
31 alex 155
		if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
26 alex 156
			return array();
157
		return $mailles_resume;
158
	}
159
 
160
 
161
}
162
 
163
?>