3 // get value of key or default value if key does not exist
4 // gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
8 // array(array(11.87, 47.12), array(11.70, 47.13))
9 function geo_to_coordinates($input) {
11 $num_matches = preg_match_all('/(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
13 for ($i=0; $i!=$num_matches; ++$i) {
14 $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
20 // convert sledruns to geojson (http://www.geojson.org/geojson-spec.html)
21 // Returns an array of features
22 function sledruns_to_json_features() {
23 $json_features = array(); // result
24 $dbr = wfGetDB(DB_SLAVE);
25 $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')));
26 while ($sledrun = $dbr->fetchRow($res)) {
27 $lat = $sledrun['position_latitude'];
28 $lon = $sledrun['position_longitude'];
29 if (is_null($lat) || is_null($lon)) continue;
30 $lat = floatval($lat);
31 $lon = floatval($lon);
32 $title = Title::newFromText($sledrun['page_title']);
33 $properties = array('type' => 'sledrun', 'name' => $title->getText(), 'wiki' => $title->getLocalUrl());
34 if (!is_null($sledrun['date_report'])) $properties[] = $sledrun['date_report'];
35 if (!is_null($sledrun['condition'])) $properties[] = intval($sledrun['condition']);
36 $json_feature = array(
40 'coordinates' => array($lon, $lat)
42 'properties' => $properties
44 $json_features[] = $json_feature;
46 $dbr->freeResult($res);
47 return $json_features;
51 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
52 // Returns an array of features
53 // TODO: Error reporting
54 function xml_to_json_features($input) {
55 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
56 $xml = new SimpleXMLElement($input); // input
57 $json_features = array(); // output
58 $point_type = array('gasthaus' => 'inn', 'haltestelle' => 'busstop', 'parkplatz' => 'carpark', 'achtung' => 'attention', 'punkt' => 'point');
59 $line_type = array('rodelbahn' => 'sledrun', 'gehweg' => 'walk', 'alternative' => 'alternative', 'lift' => 'lift', 'linie' => 'line');
60 foreach ($xml as $feature) {
62 if (in_array($feature->getName(), array_keys($point_type))) {
63 $properties = array('type' => $point_type[$feature->getName()]);
64 if (isset($feature['wiki'])) $properties['wiki'] = (string) $feature['wiki'];
65 if (isset($feature['name'])) $properties['name'] = (string) $feature['name'];
66 $json_feature = array(
70 'coordinates' => geo_to_coordinates($feature)[0]
72 'properties' => $properties
74 $json_features[] = $json_feature;
77 if (in_array($feature->getName(), array_keys($line_type))) {
78 $properties = array('type' => $line_type[$feature->getName()]);
79 if (isset($feature['farbe'])) $properties['strokeColor'] = (string) $feature['farbe'];
80 if (isset($feature['dicke'])) $properties['strokeWidth'] = (string) $feature['dicke'];
81 $json_feature = array(
84 'type' => 'LineString',
85 'coordinates' => geo_to_coordinates($feature)
87 'properties' => $properties
89 $json_features[] = $json_feature;
92 return $json_features;
98 /// Renders the <wrgmap> tag and the <wrmap> tag.
99 /// This class would be the only class needed but as the function render() toes not provide an argument
100 /// telling which tag name called the function, a trick with two inherited classes has to be used.
101 /// @param $content string - the content of the <wrgmap> tag
102 /// @param $args array - the array of attribute name/value pairs for the tag
103 /// @param $parser Parser - the MW Parser object for the current page
105 /// @return string - the html for rendering the map
106 public static function render($content, $args, $parser, $frame) {
107 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
108 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
109 assert(in_array($tagname, array('wrmap', 'wrgmap')));
111 // Get center and zoom level from $args
112 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
113 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
114 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
115 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
116 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
117 $latitude_s = sprintf('%.6F', $latitude);
118 $longitude_s = sprintf('%.6F', $longitude);
119 $width_s = is_null($width) ? '100%' : $width . 'px';
120 $height_s = $height . 'px';
121 $show_sledruns = ($tagname == 'wrgmap');
123 $parserOutput = $parser->getOutput();
124 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
125 $parserOutput->addModules('ext.wrmap');
127 $json_features = array();
129 // append all sledruns as icon
130 if ($show_sledruns) {
131 $json_features = array_merge($json_features, sledruns_to_json_features());
134 // append all elements in the XML
136 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
137 } catch (Exception $e) {
138 return htmlspecialchars($e);
141 // create final geojson
143 'type' => 'FeatureCollection',
144 'features' => $json_features
146 $json_string = json_encode($json);
148 // Create <div/> element where the map is placed in
149 global $wgExtensionAssetsPath;
150 $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/\">";
151 $output .= "<script type=\"application/json\">";
152 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
153 $output .= "</script>";
154 $output .= "</div>\n";
162 class WrMap extends WrBaseMap {
167 class WrGMap extends WrBaseMap {