3 // get value of key or default value if key does not exist
4 function array_get($key, $array, $default) {
5 if (array_key_exists($key, $array)) return $array[$key];
10 // gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
14 // array(array(11.87, 47.12), array(11.70, 47.13))
15 function geo_to_coordinates($input) {
17 $num_matches = preg_match_all('/(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
19 for ($i=0; $i!=$num_matches; ++$i) {
20 $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
26 // convert sledruns to geojson (http://www.geojson.org/geojson-spec.html)
27 // Returns an array of features
28 function sledruns_to_json_features() {
29 $json_features = array(); // result
30 $dbr = wfGetDB(DB_SLAVE);
31 $res = $dbr->select(array('wrsledruncache', 'wrreportcache'), array('wrsledruncache.page_title', 'position_latitude', 'position_longitude', 'date_report', '`condition`'), array('show_in_overview', 'not under_construction'), __METHOD__, array(), array('wrreportcache' => array('left outer join', 'wrsledruncache.page_id=wrreportcache.page_id')));
32 while ($sledrun = $dbr->fetchRow($res)) {
33 $lat = $sledrun['position_latitude'];
34 $lon = $sledrun['position_longitude'];
35 if (is_null($lat) || is_null($lon)) continue;
36 $lat = floatval($lat);
37 $lon = floatval($lon);
38 $title = Title::newFromText($sledrun['page_title']);
39 $title_text = $title->getText();
40 $title_url = $title->getLocalUrl();
41 $condition = $sledrun['condition'];
42 if (!is_null($condition)) $condition = intval($condition);
43 $json_feature = array(
47 'coordinates' => array($lon, $lat)
49 'properties' => array(
51 'name' => $title_text,
53 'date_report' => $sledrun['date_report'],
54 'condition' => $condition
57 $json_features[] = $json_feature;
59 $dbr->freeResult($res);
60 return $json_features;
64 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
65 // Returns an array of features
66 function xml_to_json_features($input) {
67 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
68 $xml = new SimpleXMLElement($input); // input
69 $json_features = array(); // output
70 foreach ($xml as $feature) {
72 if (in_array($feature->getName(), array('gasthaus', 'haltestelle', 'parkplatz', 'punkt'))) {
73 $wiki = array_get('wiki', $feature, null);
74 $name = array_get('name', $feature, null);
75 $json_feature = array(
79 'coordinates' => geo_to_coordinates($feature)[0]
81 'properties' => array(
82 'type' => $feature->getName(),
87 $json_features[] = $json_feature;
90 if (in_array($feature->getName(), array('rodelbahn', 'gehweg', 'alternative', 'lift', 'linie'))) {
91 $color = array_get('farbe', $feature, null);
92 $thickness = array_get('dicke', $feature, null);
93 $json_feature = array(
96 'type' => 'LineString',
97 'coordinates' => geo_to_coordinates($feature)
99 'properties' => array(
100 'type' => $feature->getName(),
102 'thickness' => $thickness
105 $json_features[] = $json_feature;
108 return $json_features;
114 /// Renders the <wrgmap> tag and the <wrmap> tag.
115 /// This class would be the only class needed but as the function render() toes not provide an argument
116 /// telling which tag name called the function, a trick with two inherited classes has to be used.
117 /// @param $content string - the content of the <wrgmap> tag
118 /// @param $args array - the array of attribute name/value pairs for the tag
119 /// @param $parser Parser - the MW Parser object for the current page
121 /// @return string - the html for rendering the map
122 public static function render($content, $args, $parser, $frame) {
123 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
124 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
125 assert(in_array($tagname, array('wrmap', 'wrgmap')));
127 // Get center and zoom level from $args
128 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
129 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
130 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
131 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
132 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
133 $latitude_s = sprintf('%.6F', $latitude);
134 $longitude_s = sprintf('%.6F', $longitude);
135 $width_s = is_null($width) ? '100%' : $width . 'px';
136 $height_s = $height . 'px';
137 $show_sledruns = ($tagname == 'wrgmap');
139 $parserOutput = $parser->getOutput();
140 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
141 $parserOutput->addModules('ext.wrmap');
143 $json_features = array();
145 // append all sledruns as icon
146 if ($show_sledruns) {
147 $json_features = array_merge($json_features, sledruns_to_json_features());
150 // append all elements in the XML
152 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
153 } catch (Exception $e) {
154 return htmlspecialchars($e);
157 // create final geojson
159 'type' => 'FeatureCollection',
160 'features' => $json_features
162 $json_string = json_encode($json);
164 // Create <div/> element where the map is placed in
165 global $wgExtensionAssetsPath;
166 $output = "<div class=\"wrmap\" style=\"width: $width_s; height: $height_s; border-style:none;\" data-center-lon=\"$longitude_s\" data-center-lat=\"$latitude_s\" data-zoom=\"$zoom\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
167 $output .= "<script type=\"application/json\">";
168 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
169 $output .= "</script>";
170 $output .= "</div>\n";
178 class WrMap extends WrBaseMap {
183 class WrGMap extends WrBaseMap {