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 $properties = array('type' => 'sledrun', 'name' => $title->getText(), 'wiki' => $title->getLocalUrl());
40 if (!is_null($sledrun['date_report'])) $properties[] = $sledrun['date_report'];
41 if (!is_null($sledrun['condition'])) $properties[] = intval($sledrun['condition']);
42 $json_feature = array(
46 'coordinates' => array($lon, $lat)
48 'properties' => $properties
50 $json_features[] = $json_feature;
52 $dbr->freeResult($res);
53 return $json_features;
57 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
58 // Returns an array of features
59 function xml_to_json_features($input) {
60 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
61 $xml = new SimpleXMLElement($input); // input
62 $json_features = array(); // output
63 $line_type = array('rodelbahn' => 'sledrun', 'gehweg' => 'walk', 'alternative' => 'alternative', 'lift' => 'lift', 'linie' => 'line');
64 foreach ($xml as $feature) {
66 if (in_array($feature->getName(), array('gasthaus', 'haltestelle', 'parkplatz', 'punkt'))) {
67 $wiki = array_get('wiki', $feature, null);
68 $name = array_get('name', $feature, null);
69 $json_feature = array(
73 'coordinates' => geo_to_coordinates($feature)[0]
75 'properties' => array(
76 'type' => $feature->getName(),
81 $json_features[] = $json_feature;
84 if (in_array($feature->getName(), array_keys($line_type))) {
85 $properties = array();
86 if (array_key_exists('farbe', $feature)) $properties['strokeColor'] = $feature['farbe'];
87 if (array_key_exists('dicke', $feature)) $properties['strokeWidth'] = $feature['dicke'];
88 $properties['type'] = $line_type[$feature->getName()];
89 $json_feature = array(
92 'type' => 'LineString',
93 'coordinates' => geo_to_coordinates($feature)
95 'properties' => $properties
97 $json_features[] = $json_feature;
100 return $json_features;
106 /// Renders the <wrgmap> tag and the <wrmap> tag.
107 /// This class would be the only class needed but as the function render() toes not provide an argument
108 /// telling which tag name called the function, a trick with two inherited classes has to be used.
109 /// @param $content string - the content of the <wrgmap> tag
110 /// @param $args array - the array of attribute name/value pairs for the tag
111 /// @param $parser Parser - the MW Parser object for the current page
113 /// @return string - the html for rendering the map
114 public static function render($content, $args, $parser, $frame) {
115 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
116 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
117 assert(in_array($tagname, array('wrmap', 'wrgmap')));
119 // Get center and zoom level from $args
120 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
121 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
122 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
123 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
124 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
125 $latitude_s = sprintf('%.6F', $latitude);
126 $longitude_s = sprintf('%.6F', $longitude);
127 $width_s = is_null($width) ? '100%' : $width . 'px';
128 $height_s = $height . 'px';
129 $show_sledruns = ($tagname == 'wrgmap');
131 $parserOutput = $parser->getOutput();
132 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
133 $parserOutput->addModules('ext.wrmap');
135 $json_features = array();
137 // append all sledruns as icon
138 if ($show_sledruns) {
139 $json_features = array_merge($json_features, sledruns_to_json_features());
142 // append all elements in the XML
144 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
145 } catch (Exception $e) {
146 return htmlspecialchars($e);
149 // create final geojson
151 'type' => 'FeatureCollection',
152 'features' => $json_features
154 $json_string = json_encode($json);
156 // Create <div/> element where the map is placed in
157 global $wgExtensionAssetsPath;
158 $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/\">";
159 $output .= "<script type=\"application/json\">";
160 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
161 $output .= "</script>";
162 $output .= "</div>\n";
170 class WrMap extends WrBaseMap {
175 class WrGMap extends WrBaseMap {