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('/\s*(\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]));
16 if (implode($matches[0]) != $input) throw new Exception('Falsches Koordinatenformat: ' . $input);
21 // convert sledruns to geojson (http://www.geojson.org/geojson-spec.html)
22 // Returns an array of features
23 function sledruns_to_json_features() {
24 $json_features = array(); // result
25 $dbr = wfGetDB(DB_SLAVE);
26 $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')));
27 while ($sledrun = $dbr->fetchRow($res)) {
28 $lat = $sledrun['position_latitude'];
29 $lon = $sledrun['position_longitude'];
30 if (is_null($lat) || is_null($lon)) continue;
31 $lat = floatval($lat);
32 $lon = floatval($lon);
33 $title = Title::newFromText($sledrun['page_title']);
34 $properties = array('type' => 'sledrun', 'name' => $title->getText(), 'wiki' => $title->getLocalUrl());
35 if (!is_null($sledrun['date_report'])) $properties[] = $sledrun['date_report'];
36 if (!is_null($sledrun['condition'])) $properties[] = intval($sledrun['condition']);
37 $json_feature = array(
41 'coordinates' => array($lon, $lat)
43 'properties' => $properties
45 $json_features[] = $json_feature;
47 $dbr->freeResult($res);
48 return $json_features;
52 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
53 // Returns an array of features
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) {
61 $given_properties = array();
62 foreach ($feature->attributes() as $key => $value) $given_properties[] = $key;
64 // determine feature type
65 $is_point = in_array($feature->getName(), array_keys($point_type));
66 $is_line = in_array($feature->getName(), array_keys($line_type));
67 if (!$is_point && !$is_line) {
68 throw new Exception('Unbekanntes Element <' . $feature->getName() . '>. Erlaubt sind: <' . implode('>, <', array_keys(array_merge($point_type, $line_type))) . '>.');
73 $properties = array('type' => $point_type[$feature->getName()]);
74 $allowed_properties = array('name', 'wiki');
75 $wrong_properties = array_diff($given_properties, $allowed_properties);
76 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
77 foreach ($given_properties as $property) {
78 $properties[$property] = (string) $feature[$property];
80 $coordinates = geo_to_coordinates($feature);
81 if (count($coordinates) != 1) throw new Exception('Das Element <' . $feature->getName() . '> muss genau ein Koordinatenpaar haben.');
82 $json_feature = array(
86 'coordinates' => reset($coordinates)
88 'properties' => $properties
90 $json_features[] = $json_feature;
94 $properties = array('type' => $line_type[$feature->getName()]);
95 $allowed_properties = array('farbe', 'dicke');
96 $wrong_properties = array_diff($given_properties, $allowed_properties);
97 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
98 if (isset($feature['farbe'])) $properties['strokeColor'] = (int) $feature['farbe']; // e.g. 6
99 if (isset($feature['dicke'])) $properties['strokeWidth'] = (string) $feature['dicke']; // e.g. #a200b7
100 $json_feature = array(
103 'type' => 'LineString',
104 'coordinates' => geo_to_coordinates($feature)
106 'properties' => $properties
108 $json_features[] = $json_feature;
111 return $json_features;
117 /// Renders the <wrgmap> tag and the <wrmap> tag.
118 /// This class would be the only class needed but as the function render() toes not provide an argument
119 /// telling which tag name called the function, a trick with two inherited classes has to be used.
120 /// @param $content string - the content of the <wrgmap> tag
121 /// @param $args array - the array of attribute name/value pairs for the tag
122 /// @param $parser Parser - the MW Parser object for the current page
124 /// @return string - the html for rendering the map
125 public static function render($content, $args, $parser, $frame) {
126 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
127 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
128 assert(in_array($tagname, array('wrmap', 'wrgmap')));
130 // Get center and zoom level from $args
131 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
132 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
133 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
134 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
135 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
136 $latitude_s = sprintf('%.6F', $latitude);
137 $longitude_s = sprintf('%.6F', $longitude);
138 $width_s = is_null($width) ? '100%' : $width . 'px';
139 $height_s = $height . 'px';
140 $show_sledruns = ($tagname == 'wrgmap');
142 $parserOutput = $parser->getOutput();
143 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
144 $parserOutput->addModules('ext.wrmap');
146 $json_features = array();
148 // append all sledruns as icon
149 if ($show_sledruns) {
150 $json_features = array_merge($json_features, sledruns_to_json_features());
153 // append all elements in the XML
155 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
156 } catch (Exception $e) {
157 return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
160 // create final geojson
162 'type' => 'FeatureCollection',
163 'features' => $json_features
165 $json_string = json_encode($json);
167 // Create <div/> element where the map is placed in
168 global $wgExtensionAssetsPath;
169 $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/\">";
170 $output .= "<script type=\"application/json\">";
171 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
172 $output .= "</script>";
173 $output .= "</div>\n";
181 class WrMap extends WrBaseMap {
186 class WrGMap extends WrBaseMap {