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 = $boucle->primary; |
---|
80 | |
---|
81 | // exclure l'élément en cours des résultats |
---|
82 | $id_gis = calculer_argument_precedent($idb,$id, $boucles); |
---|
83 | $boucle->where[]= array("'!='", "'$boucle->id_table." . "$id'", $id_gis); |
---|
84 | |
---|
85 | // récupérer les paramètres du critère |
---|
86 | $op=''; |
---|
87 | $params = $crit->param; |
---|
88 | $type = array_shift($params); |
---|
89 | $type = $type[0]->texte; |
---|
90 | if(preg_match(',^(\w+)([<>=]+)([0-9]+)$,',$type,$r)){ |
---|
91 | $type=$r[1]; |
---|
92 | $op=$r[2]; |
---|
93 | $op_val=$r[3]; |
---|
94 | } |
---|
95 | if ($op) |
---|
96 | $boucle->having[]= array("'".$op."'", "'".$type."'",$op_val); |
---|
97 | |
---|
98 | // récupérer lat/lon du point de la boucle englobante |
---|
99 | $lat = calculer_argument_precedent($idb,'lat', $boucles); |
---|
100 | $lon = calculer_argument_precedent($idb,'lon', $boucles); |
---|
101 | |
---|
102 | // http://www.awelty.fr/developpement-web/php/ |
---|
103 | // http://www.movable-type.co.uk/scripts/latlong-db.html |
---|
104 | // http://code.google.com/intl/fr/apis/maps/articles/geospatial.html#geospatial |
---|
105 | $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"; |
---|
106 | $order = "'distance'"; |
---|
107 | |
---|
108 | $boucle->select[]= $select; |
---|
109 | $boucle->order[]= $order; |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Balise #DISTANCE issue du critère {gis distance<XX} |
---|
115 | * merci marcimant : http://formation.magraine.net/spip.php?article61 |
---|
116 | * |
---|
117 | * @param unknown_type $p |
---|
118 | */ |
---|
119 | function balise_distance_dist($p) { |
---|
120 | return rindex_pile($p, 'distance', 'gis'); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Définition de l'API à utiliser en prenant compte les defines |
---|
125 | */ |
---|
126 | function gis_api_utilisee(){ |
---|
127 | $defaut = 'openlayers'; |
---|
128 | if(defined('_GIS_APIS_FORCEE')){ |
---|
129 | return _GIS_APIS_FORCEE; |
---|
130 | }else{ |
---|
131 | if(defined('_GIS_APIS_DEFAUT')){ |
---|
132 | $defaut = _GIS_APIS_DEFAUT; |
---|
133 | } |
---|
134 | $config = lire_config('gis/api'); |
---|
135 | return $config ? $config : $defaut; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Définition du fond cartographique à utiliser en prenant compte les defines |
---|
141 | */ |
---|
142 | function gis_maptype_utilise(){ |
---|
143 | $defaut = 'ROAD'; |
---|
144 | if(defined('_GIS_MAPTYPES_FORCE')){ |
---|
145 | return _GIS_MAPTYPES_FORCE; |
---|
146 | }else{ |
---|
147 | if(defined('_GIS_MAPTYPES_DEFAUT')){ |
---|
148 | $defaut = _GIS_MAPTYPES_DEFAUT; |
---|
149 | } |
---|
150 | $config = lire_config('gis/maptype'); |
---|
151 | return $config ? $config : $defaut; |
---|
152 | } |
---|
153 | } |
---|
154 | ?> |
---|