1 | <?php |
---|
2 | |
---|
3 | include_spip('inc/config'); |
---|
4 | |
---|
5 | /** |
---|
6 | * Filtre dec_to_dms, http://www.statemaster.com/encyclopedia/Geographic-coordinate-conversion |
---|
7 | * |
---|
8 | * @param decimal $coord |
---|
9 | * @return string |
---|
10 | */ |
---|
11 | function dec_to_dms($coord) { |
---|
12 | return sprintf( |
---|
13 | "%0.0f° %2.3f", |
---|
14 | floor(abs($coord)), |
---|
15 | 60*(abs($coord)-floor(abs($coord))) |
---|
16 | ); |
---|
17 | } |
---|
18 | |
---|
19 | /** |
---|
20 | * Filtre dms_to_dec, http://www.statemaster.com/encyclopedia/Geographic-coordinate-conversion |
---|
21 | * |
---|
22 | * @param string $ref N, E, S, W |
---|
23 | * @param int $deg |
---|
24 | * @param int $min |
---|
25 | * @param int $sec |
---|
26 | * @return decimal |
---|
27 | */ |
---|
28 | function dms_to_dec($ref,$deg,$min,$sec) { |
---|
29 | |
---|
30 | $arrLatLong = array(); |
---|
31 | $arrLatLong["N"] = 1; |
---|
32 | $arrLatLong["E"] = 1; |
---|
33 | $arrLatLong["S"] = -1; |
---|
34 | $arrLatLong["W"] = -1; |
---|
35 | |
---|
36 | return ($deg+((($min*60)+($sec))/3600)) * $arrLatLong[$ref]; |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * Filtre distance pour renvoyer la distance entre deux points |
---|
41 | * http://snipplr.com/view/2531/calculate-the-distance-between-two-coordinates-latitude-longitude/ |
---|
42 | * sinon voir ici : http://zone.spip.org/trac/spip-zone/browser/_plugins_/forms/geoforms/inc/gPoint.php |
---|
43 | * |
---|
44 | * @param int $from id_gis du point de référence |
---|
45 | * @param int $ti id_gis du point distant |
---|
46 | * @param bool $miles renvoyer le résultat en miles (kilomètres par défaut) |
---|
47 | * @return |
---|
48 | */ |
---|
49 | function distance($from,$to,$miles=false) { |
---|
50 | $from = sql_fetsel('lat,lon','spip_gis',"id_gis=$from"); |
---|
51 | $to = sql_fetsel('lat,lon','spip_gis',"id_gis=$to"); |
---|
52 | |
---|
53 | $pi80 = M_PI / 180; |
---|
54 | $from['lat'] *= $pi80; |
---|
55 | $from['lon'] *= $pi80; |
---|
56 | $to['lat'] *= $pi80; |
---|
57 | $to['lon'] *= $pi80; |
---|
58 | |
---|
59 | $r = 6372.797; // mean radius of Earth in km |
---|
60 | $dlat = $to['lat'] - $from['lat']; |
---|
61 | $dlng = $to['lon'] - $from['lon']; |
---|
62 | $a = sin($dlat / 2) * sin($dlat / 2) + cos($from['lat']) * cos($to['lat']) * sin($dlng / 2) * sin($dlng / 2); |
---|
63 | $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); |
---|
64 | $km = $r * $c; |
---|
65 | |
---|
66 | return ($miles ? ($km * 0.621371192) : $km); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Critere {gis distance<XX} pour filtrer une liste de points par rapport à la distance du point de l'env |
---|
71 | * |
---|
72 | * @param unknown_type $idb |
---|
73 | * @param unknown_type $boucles |
---|
74 | * @param unknown_type $crit |
---|
75 | */ |
---|
76 | function critere_gis_dist($idb, &$boucles, $crit) { |
---|
77 | |
---|
78 | $boucle = &$boucles[$idb]; |
---|
79 | $id_table = $boucle->id_table; // articles |
---|
80 | $primary = $boucle->primary; // id_article |
---|
81 | $objet = objet_type($id_table); // article |
---|
82 | |
---|
83 | if ($id_table == 'gis') { |
---|
84 | // exclure l'élément en cours des résultats |
---|
85 | $id_gis = calculer_argument_precedent($idb,$primary, $boucles); |
---|
86 | $boucle->where[]= array("'!='", "'$boucle->id_table." . "$primary'", $id_gis); |
---|
87 | |
---|
88 | // récupérer les paramètres du critère |
---|
89 | $op=''; |
---|
90 | $params = $crit->param; |
---|
91 | $type = array_shift($params); |
---|
92 | $type = $type[0]->texte; |
---|
93 | if(preg_match(',^(\w+)([<>=]+)([0-9]+)$,',$type,$r)){ |
---|
94 | $type=$r[1]; |
---|
95 | $op=$r[2]; |
---|
96 | $op_val=$r[3]; |
---|
97 | } |
---|
98 | if ($op) |
---|
99 | $boucle->having[]= array("'".$op."'", "'".$type."'",$op_val); |
---|
100 | |
---|
101 | // récupérer lat/lon du point de la boucle englobante |
---|
102 | $lat = calculer_argument_precedent($idb,'lat', $boucles); |
---|
103 | $lon = calculer_argument_precedent($idb,'lon', $boucles); |
---|
104 | |
---|
105 | // http://www.awelty.fr/developpement-web/php/ |
---|
106 | // http://www.movable-type.co.uk/scripts/latlong-db.html |
---|
107 | // http://code.google.com/intl/fr/apis/maps/articles/geospatial.html#geospatial |
---|
108 | $select = "(6371 * acos( cos( radians(\".$lat.\") ) * cos( radians( gis.lat ) ) * cos( radians( gis.lon ) - radians(\".$lon.\") ) + sin( radians(\".$lat.\") ) * sin( radians( gis.lat ) ) ) ) AS distance"; |
---|
109 | $order = "'distance'"; |
---|
110 | |
---|
111 | $boucle->select[]= $select; |
---|
112 | $boucle->order[]= $order; |
---|
113 | } else { |
---|
114 | // ajouter tous les champs du point au select |
---|
115 | // et les suffixer pour lever toute ambiguite avec des champs homonymes |
---|
116 | $boucle->select[]= 'gis.titre AS titre_gis'; |
---|
117 | $boucle->select[]= 'gis.descriptif AS descriptif_gis'; |
---|
118 | $boucle->select[]= 'gis.adresse AS adresse_gis'; |
---|
119 | $boucle->select[]= 'gis.pays AS pays_gis'; |
---|
120 | $boucle->select[]= 'gis.code_pays AS code_pays_gis'; |
---|
121 | $boucle->select[]= 'gis.region AS region_gis'; |
---|
122 | $boucle->select[]= 'gis.ville AS ville_gis'; |
---|
123 | $boucle->select[]= 'gis.code_postal AS code_postal_gis'; |
---|
124 | // jointure sur spip_gis_liens/spip_gis |
---|
125 | // cf plugin notation |
---|
126 | // $boucle->join["surnom (as) table de liaison"] = array("surnom de la table a lier", "cle primaire de la table de liaison", "identifiant a lier", "type d'objet de l'identifiant"); |
---|
127 | $boucle->from['gis_liens'] = 'spip_gis_liens'; |
---|
128 | $boucle->join['gis_liens']= array("'$id_table'","'id_objet'","'$primary'","'gis_liens.objet='.sql_quote('$objet')"); |
---|
129 | $boucle->from['gis'] = 'spip_gis'; |
---|
130 | $boucle->join['gis']= array("'gis_liens'","'id_gis'"); |
---|
131 | // bien renvoyer tous les points qui son attachés à l'objet |
---|
132 | $boucle->group[] = 'gis_liens.id_gis'; |
---|
133 | // ajouter gis aux jointures et spécifier les jointures explicites pour pouvoir utiliser les balises de la table de jointure |
---|
134 | // permet de passer dans trouver_champ_exterieur() depuis index_tables_en_pile() |
---|
135 | // cf http://article.gmane.org/gmane.comp.web.spip.zone/6628 |
---|
136 | $boucle->jointures[] = 'gis'; |
---|
137 | $boucle->jointures_explicites = 'gis_liens gis'; |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Balise #DISTANCE issue du critère {gis distance<XX} |
---|
144 | * merci marcimant : http://formation.magraine.net/spip.php?article61 |
---|
145 | * |
---|
146 | * @param unknown_type $p |
---|
147 | */ |
---|
148 | function balise_distance_dist($p) { |
---|
149 | return rindex_pile($p, 'distance', 'gis'); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Balise #TITRE_GIS : retourne le titre du point |
---|
154 | * Necessite le critere {gis} sur la boucle |
---|
155 | * |
---|
156 | * @param unknown_type $p |
---|
157 | */ |
---|
158 | function balise_titre_gis_dist($p) { |
---|
159 | return rindex_pile($p, 'titre_gis', 'gis'); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Balise #DESCRIPTIF_GIS : retourne le descriptif du point |
---|
164 | * Necessite le critere {gis} sur la boucle |
---|
165 | * |
---|
166 | * @param unknown_type $p |
---|
167 | */ |
---|
168 | function balise_descriptif_gis_dist($p) { |
---|
169 | return rindex_pile($p, 'descriptif_gis', 'gis'); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Définition de l'API à utiliser en prenant compte les defines |
---|
174 | */ |
---|
175 | function gis_api_utilisee(){ |
---|
176 | $defaut = 'openlayers'; |
---|
177 | if(defined('_GIS_APIS_FORCEE')){ |
---|
178 | return _GIS_APIS_FORCEE; |
---|
179 | }else{ |
---|
180 | if(defined('_GIS_APIS_DEFAUT')){ |
---|
181 | $defaut = _GIS_APIS_DEFAUT; |
---|
182 | } |
---|
183 | $config = lire_config('gis/api'); |
---|
184 | return $config ? $config : $defaut; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Définition du fond cartographique à utiliser en prenant compte les defines |
---|
190 | */ |
---|
191 | function gis_maptype_utilise(){ |
---|
192 | $defaut = 'ROAD'; |
---|
193 | if(defined('_GIS_MAPTYPES_FORCE')){ |
---|
194 | return _GIS_MAPTYPES_FORCE; |
---|
195 | }else{ |
---|
196 | if(defined('_GIS_MAPTYPES_DEFAUT')){ |
---|
197 | $defaut = _GIS_MAPTYPES_DEFAUT; |
---|
198 | } |
---|
199 | $config = lire_config('gis/maptype'); |
---|
200 | return $config ? $config : $defaut; |
---|
201 | } |
---|
202 | } |
---|
203 | ?> |
---|