Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | die( 'Not an entry point.' ); |
15 | 15 | } |
16 | 16 | |
17 | | -class MapsOpenLayersDispMap extends MapsBasePointMap { |
| 17 | +class MapsOpenLayersDispMap extends MapsBaseMap { |
18 | 18 | |
19 | 19 | public $serviceName = MapsOpenLayersUtils::SERVICE_NAME; |
20 | 20 | |
Index: trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_BasePointMap.php |
— | — | @@ -112,7 +112,6 @@ |
113 | 113 | /** |
114 | 114 | * Sets the $centre_lat and $centre_lon fields. |
115 | 115 | * Note: this needs to be done AFTRE the maker coordinates are set. |
116 | | - * |
117 | 116 | */ |
118 | 117 | private function setCentre() { |
119 | 118 | if (empty($this->centre)) { |
— | — | @@ -129,20 +128,34 @@ |
130 | 129 | } |
131 | 130 | else { |
132 | 131 | // If centre is not set and there are no markers, use the default latitude and longitutde. |
133 | | - global $egMapsMapLat, $egMapsMapLon; |
134 | | - $this->centre_lat = $egMapsMapLat; |
135 | | - $this->centre_lon = $egMapsMapLon; |
| 132 | + $this->setCentreDefaults(); |
136 | 133 | } |
137 | 134 | } |
138 | | - else { |
139 | | - // If a centre value is set, use it. |
140 | | - $centre = MapsUtils::getLatLon($this->centre); |
141 | | - $this->centre_lat = $centre['lat']; |
142 | | - $this->centre_lon = $centre['lon']; |
143 | | - } |
144 | | - } |
| 135 | + else { // If a centre value is set, geocode when needed and use it. |
| 136 | + $this->centre = MapsParserGeocoder::attemptToGeocode($this->centre, $this->geoservice, $this->serviceName); |
| 137 | + |
| 138 | + // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde. |
| 139 | + if ($this->centre) { |
| 140 | + $this->centre = MapsUtils::getLatLon($this->centre); |
| 141 | + $this->centre_lat = $this->centre['lat']; |
| 142 | + $this->centre_lon = $this->centre['lon']; |
| 143 | + } |
| 144 | + else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used. |
| 145 | + $this->setCentreDefaults(); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
145 | 149 | |
146 | 150 | /** |
| 151 | + * Sets the centre latitude and longitutde to the defaults. |
| 152 | + */ |
| 153 | + private function setCentreDefaults() { |
| 154 | + global $egMapsMapLat, $egMapsMapLon; |
| 155 | + $this->centre_lat = $egMapsMapLat; |
| 156 | + $this->centre_lon = $egMapsMapLon; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
147 | 160 | * Parse the wiki text in the title and label values. |
148 | 161 | * |
149 | 162 | * @param unknown_type $parser |
Index: trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_BaseMap.php |
— | — | @@ -27,8 +27,6 @@ |
28 | 28 | * @return html |
29 | 29 | */ |
30 | 30 | public final function displayMap(&$parser, array $params) { |
31 | | -die('disp map'); |
32 | | - |
33 | 31 | $this->setMapSettings(); |
34 | 32 | |
35 | 33 | $coords = $this->manageMapProperties($params); |
— | — | @@ -61,11 +59,31 @@ |
62 | 60 | * |
63 | 61 | */ |
64 | 62 | private function setZoom() { |
65 | | - if (strlen($this->zoom) < 1) $this->zoom = $this->defaultZoom; |
| 63 | + if (empty($this->zoom)) $this->zoom = $this->defaultZoom; |
66 | 64 | } |
67 | 65 | |
| 66 | + /** |
| 67 | + * Sets the $centre_lat and $centre_lon fields. |
| 68 | + */ |
68 | 69 | private function setCentre() { |
69 | | - if (strlen($this->centre) < 1) $this->centre = $this->defaultZoom; |
| 70 | + if (empty($this->coordinates)) { // If centre is not set, use the default latitude and longitutde. |
| 71 | + global $egMapsMapLat, $egMapsMapLon; |
| 72 | + $this->centre_lat = $egMapsMapLat; |
| 73 | + $this->centre_lon = $egMapsMapLon; |
| 74 | + } |
| 75 | + else { // If a centre value is set, geocode when needed and use it. |
| 76 | + $this->coordinates = MapsParserGeocoder::attemptToGeocode($this->coordinates, $this->geoservice, $this->serviceName); |
| 77 | + |
| 78 | + // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde. |
| 79 | + if ($this->coordinates) { |
| 80 | + $this->coordinates = MapsUtils::getLatLon($this->coordinates); |
| 81 | + $this->centre_lat = $this->coordinates['lat']; |
| 82 | + $this->centre_lon = $this->coordinates['lon']; |
| 83 | + } |
| 84 | + else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used. |
| 85 | + $this->setCentreDefaults(); |
| 86 | + } |
| 87 | + } |
70 | 88 | } |
71 | 89 | |
72 | 90 | } |
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php |
— | — | @@ -79,31 +79,57 @@ |
80 | 80 | $params[$i] = 'coordinates=' . implode(';', $coordinates); |
81 | 81 | |
82 | 82 | } |
| 83 | + |
83 | 84 | } |
84 | 85 | |
85 | 86 | return $fails; |
86 | 87 | } |
87 | 88 | |
88 | 89 | /** |
| 90 | + * This function first determines wether the provided string is a pair or coordinates |
| 91 | + * or an address. If it's the later, an attempt to geocode will be made. The function will |
| 92 | + * return the coordinates or false, in case a geocoding attempt was made but failed. |
89 | 93 | * |
90 | | - * @return unknown_type |
| 94 | + * @param $coordsOrAddress |
| 95 | + * @param $geoservice |
| 96 | + * @param $service |
| 97 | + * |
| 98 | + * @return string or boolean |
91 | 99 | */ |
92 | | - private static function attemptToGeocode($coordsOrAddress, $geoservice, $service) { |
93 | | - // TODO: add check for DM and DD notations |
94 | | - $floatRegex = "/^\d{1,3}(|\.\d{1,7}),(|\s)\d{1,3}(|\.\d{1,7})$/"; |
95 | | - $dmsRegex = "/^(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(N|S)(| )(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(E|W)$/"; |
96 | | - |
97 | | - $needsGeocoding = !preg_match($floatRegex, $coordsOrAddress); |
98 | | - if ($needsGeocoding) $needsGeocoding = !preg_match($dmsRegex, $coordsOrAddress); |
99 | | - |
100 | | - if ($needsGeocoding) { |
101 | | - $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service); |
| 100 | + public static function attemptToGeocode($coordsOrAddress, $geoservice, $service) { |
| 101 | + if (MapsParserGeocoder::isCoordinate($coordsOrAddress)) { |
| 102 | + $coords = $coordsOrAddress; |
102 | 103 | } |
103 | 104 | else { |
104 | | - $coords = $coordsOrAddress; |
| 105 | + $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service); |
105 | 106 | } |
106 | 107 | |
107 | 108 | return $coords; |
108 | 109 | } |
109 | 110 | |
| 111 | + /** |
| 112 | + * |
| 113 | + * @param $coordsOrAddress |
| 114 | + * @return unknown_type |
| 115 | + */ |
| 116 | + private static function isCoordinate($coordsOrAddress) { |
| 117 | + $coordRegexes = array( // TODO: change . to �, this won't work for some reason |
| 118 | + '/^\d{1,3}(\.\d{1,7})?,(\s)?\d{1,3}(\.\d{1,7})?$/', // Floats |
| 119 | + '/^(\d{1,2}.)(\d{2}\')?((\d{2}")?|(\d{2}\.\d{2}")?)(N|S)(\s)?(\d{1,2}.)(\d{2}\')?((\d{2}")?|(\d{2}\.\d{2}")?)(E|W)$/', // DMS // TODO: compress logic |
| 120 | + '/^(-)?\d{1,3}(|\.\d{1,7}).,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,7}).$/', // DD |
| 121 | + '/(-)?\d{1,3}.\d{1,3}(\.\d{1,7}\')?,(\s)?(-)?\d{1,3}.\d{1,3}(\.\d{1,7}\')?$/', // DM |
| 122 | + ); |
| 123 | + |
| 124 | + $isCoordinate = false; |
| 125 | + |
| 126 | + foreach ($coordRegexes as $coordRegex) { |
| 127 | + if (preg_match($coordRegex, $coordsOrAddress)) { |
| 128 | + $isCoordinate = true; |
| 129 | + continue; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return $isCoordinate; |
| 134 | + } |
| 135 | + |
110 | 136 | } |
\ No newline at end of file |
Index: trunk/extensions/Maps/Maps.php |
— | — | @@ -23,7 +23,7 @@ |
24 | 24 | die( 'Not an entry point.' ); |
25 | 25 | } |
26 | 26 | |
27 | | -define('Maps_VERSION', '0.4 a7'); |
| 27 | +define('Maps_VERSION', '0.4 a8'); |
28 | 28 | |
29 | 29 | $egMapsScriptPath = $wgScriptPath . '/extensions/Maps'; |
30 | 30 | $egMapsIP = $IP . '/extensions/Maps'; |
Index: trunk/extensions/Maps/Maps_Mapper.php |
— | — | @@ -24,6 +24,7 @@ |
25 | 25 | private static $mainParams = array |
26 | 26 | ( |
27 | 27 | 'service' => array(), |
| 28 | + 'geoservice' => array(), |
28 | 29 | 'coordinates' => array('coords', 'location', 'locations'), |
29 | 30 | 'zoom' => array(), |
30 | 31 | 'centre' => array('center'), |
— | — | @@ -115,6 +116,7 @@ |
116 | 117 | |
117 | 118 | $mapDefaults = array( |
118 | 119 | 'service' => $egMapsDefaultService, |
| 120 | + 'geoservice' => '', |
119 | 121 | 'coordinates' => "$egMapsMapLat, $egMapsMapLon", |
120 | 122 | 'zoom' => '', |
121 | 123 | 'centre' => '', |
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsDispMap.php |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | die( 'Not an entry point.' ); |
15 | 15 | } |
16 | 16 | |
17 | | -final class MapsGoogleMapsDispMap extends MapsBasePointMap { |
| 17 | +final class MapsGoogleMapsDispMap extends MapsBaseMap { |
18 | 18 | |
19 | 19 | public $serviceName = MapsGoogleMapsUtils::SERVICE_NAME; |
20 | 20 | |
Index: trunk/extensions/Maps/YahooMaps/Maps_YahooMapsDispMap.php |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | die( 'Not an entry point.' ); |
15 | 15 | } |
16 | 16 | |
17 | | -class MapsYahooMapsDispMap extends MapsBasePointMap { |
| 17 | +class MapsYahooMapsDispMap extends MapsBaseMap { |
18 | 18 | |
19 | 19 | public $serviceName = MapsYahooMapsUtils::SERVICE_NAME; |
20 | 20 | |
Index: trunk/extensions/Maps/Maps_Utils.php |
— | — | @@ -35,6 +35,17 @@ |
36 | 36 | } |
37 | 37 | } |
38 | 38 | |
| 39 | + private static function convertCoord($deg_coord = "") { |
| 40 | + if (preg_match ( '/°/', $deg_coord )) { |
| 41 | + if (preg_match ( '/"/', $deg_coord )) { |
| 42 | + return MapsUtils::degree2Decimal ( $deg_coord ); |
| 43 | + } else { |
| 44 | + return MapsUtils::decDegree2Decimal ( $deg_coord ); |
| 45 | + } |
| 46 | + } |
| 47 | + return $deg_coord; |
| 48 | + } |
| 49 | + |
39 | 50 | private static function degree2Decimal($deg_coord = "") { |
40 | 51 | $dpos = strpos ( $deg_coord, '°' ); |
41 | 52 | $mpos = strpos ( $deg_coord, '.' ); |
— | — | @@ -65,17 +76,6 @@ |
66 | 77 | return $decimal; |
67 | 78 | } |
68 | 79 | |
69 | | - private static function convertCoord($deg_coord = "") { |
70 | | - if (preg_match ( '/°/', $deg_coord )) { |
71 | | - if (preg_match ( '/"/', $deg_coord )) { |
72 | | - return MapsUtils::degree2Decimal ( $deg_coord ); |
73 | | - } else { |
74 | | - return MapsUtils::decDegree2Decimal ( $deg_coord ); |
75 | | - } |
76 | | - } |
77 | | - return $deg_coord; |
78 | | - } |
79 | | - |
80 | 80 | public static function latDecimal2Degree($decimal) { |
81 | 81 | if ($decimal < 0) { |
82 | 82 | return abs ( $decimal ) . "° S"; |