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 // TODO: name of points gets not added to properties
55 function xml_to_json_features($input) {
56 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
57 $xml = new SimpleXMLElement($input); // input
58 $json_features = array(); // output
59 $point_type = array('gasthaus' => 'inn', 'haltestelle' => 'busstop', 'parkplatz' => 'carpark', 'achtung' => 'attention', 'punkt' => 'point');
60 $line_type = array('rodelbahn' => 'sledrun', 'gehweg' => 'walk', 'alternative' => 'alternative', 'lift' => 'lift', 'linie' => 'line');
61 foreach ($xml as $feature) {
63 if (in_array($feature->getName(), array_keys($point_type))) {
64 $properties = array('type' => $point_type[$feature->getName()]);
65 if (array_key_exists('wiki', $feature)) $properties['wiki'] = $feature['wiki'];
66 if (array_key_exists('name', $feature)) $properties['name'] = $feature['name'];
67 $json_feature = array(
71 'coordinates' => geo_to_coordinates($feature)[0]
73 'properties' => $properties
75 $json_features[] = $json_feature;
78 if (in_array($feature->getName(), array_keys($line_type))) {
79 $properties = array('type' => $line_type[$feature->getName()]);
80 if (array_key_exists('farbe', $feature)) $properties['strokeColor'] = $feature['farbe'];
81 if (array_key_exists('dicke', $feature)) $properties['strokeWidth'] = $feature['dicke'];
82 $json_feature = array(
85 'type' => 'LineString',
86 'coordinates' => geo_to_coordinates($feature)
88 'properties' => $properties
90 $json_features[] = $json_feature;
93 return $json_features;
99 /// Renders the <wrgmap> tag and the <wrmap> tag.
100 /// This class would be the only class needed but as the function render() toes not provide an argument
101 /// telling which tag name called the function, a trick with two inherited classes has to be used.
102 /// @param $content string - the content of the <wrgmap> tag
103 /// @param $args array - the array of attribute name/value pairs for the tag
104 /// @param $parser Parser - the MW Parser object for the current page
106 /// @return string - the html for rendering the map
107 public static function render($content, $args, $parser, $frame) {
108 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
109 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
110 assert(in_array($tagname, array('wrmap', 'wrgmap')));
112 // Get center and zoom level from $args
113 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
114 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
115 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
116 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
117 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
118 $latitude_s = sprintf('%.6F', $latitude);
119 $longitude_s = sprintf('%.6F', $longitude);
120 $width_s = is_null($width) ? '100%' : $width . 'px';
121 $height_s = $height . 'px';
122 $show_sledruns = ($tagname == 'wrgmap');
124 $parserOutput = $parser->getOutput();
125 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
126 $parserOutput->addModules('ext.wrmap');
128 $json_features = array();
130 // append all sledruns as icon
131 if ($show_sledruns) {
132 $json_features = array_merge($json_features, sledruns_to_json_features());
135 // append all elements in the XML
137 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
138 } catch (Exception $e) {
139 return htmlspecialchars($e);
142 // create final geojson
144 'type' => 'FeatureCollection',
145 'features' => $json_features
147 $json_string = json_encode($json);
149 // Create <div/> element where the map is placed in
150 global $wgExtensionAssetsPath;
151 $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/\">";
152 $output .= "<script type=\"application/json\">";
153 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
154 $output .= "</script>";
155 $output .= "</div>\n";
163 class WrMap extends WrBaseMap {
168 class WrGMap extends WrBaseMap {