3 // gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
7 // array(array(11.87, 47.12), array(11.70, 47.13))
8 function geo_to_coordinates($input) {
10 $num_matches = preg_match_all('/\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
12 for ($i=0; $i!=$num_matches; ++$i) {
13 $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
15 if (implode($matches[0]) != $input) throw new Exception('Falsches Koordinatenformat: ' . $input);
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 function xml_to_json_features($input) {
54 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
55 $xml = new SimpleXMLElement($input); // input
56 $json_features = array(); // output
57 $point_types = array('gasthaus', 'haltestelle', 'parkplatz', 'achtung', 'punkt');
58 $line_types = array('rodelbahn', 'gehweg', 'alternative', 'lift', 'anfahrt', 'linie');
59 foreach ($xml as $feature) {
60 $given_properties = array();
61 foreach ($feature->attributes() as $key => $value) $given_properties[] = $key;
63 // determine feature type
64 $is_point = in_array($feature->getName(), $point_types);
65 $is_line = in_array($feature->getName(), $line_types);
66 if (!$is_point && !$is_line) {
67 throw new Exception('Unbekanntes Element <' . $feature->getName() . '>. Erlaubt sind: <' . implode('>, <', array_keys(array_merge($point_type, $line_type))) . '>.');
72 $properties = array('type' => $feature->getName());
73 $allowed_properties = array('name', 'wiki');
74 $wrong_properties = array_diff($given_properties, $allowed_properties);
75 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
76 foreach ($given_properties as $property) {
77 $propval = (string) $feature[$property];
78 if ($property == 'wiki') {
79 $title = Title::newFromText($propval);
80 $propval = $title->getLocalUrl();
82 $properties[$property] = $propval;
84 $coordinates = geo_to_coordinates($feature);
85 if (count($coordinates) != 1) throw new Exception('Das Element <' . $feature->getName() . '> muss genau ein Koordinatenpaar haben.');
86 $json_feature = array(
90 'coordinates' => reset($coordinates)
92 'properties' => $properties
94 $json_features[] = $json_feature;
98 $properties = array('type' => $feature->getName());
99 $allowed_properties = array('farbe', 'dicke');
100 $wrong_properties = array_diff($given_properties, $allowed_properties);
101 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
102 if (isset($feature['farbe'])) {
103 $color = (string) $feature['farbe']; // e.g. #a200b7
104 if (preg_match('/^#[0-9a-f]{6}$/i', $color) != 1)
105 throw new Exception('Die Farbangabe der Linie hat ein falsches Format. Sie muss z.B. so aussehen: #a200b7.');
106 $properties['strokeColor'] = $color;
108 if (isset($feature['dicke'])) {
109 $stroke_width = (int) $feature['dicke']; // e.g. 6
110 if (((string) $stroke_width) !== (string) $feature['dicke'])
111 throw new Exception('Die Angabe der Liniendicke hat ein falsches Format. Sie muss eine ganze Zahl wie z.B. 6 sein.');
112 $properties['strokeWidth'] = $stroke_width;
114 $json_feature = array(
117 'type' => 'LineString',
118 'coordinates' => geo_to_coordinates($feature)
120 'properties' => $properties
122 $json_features[] = $json_feature;
125 return $json_features;
131 /// Renders the <wrgmap> tag and the <wrmap> tag.
132 /// This class would be the only class needed but as the function render() toes not provide an argument
133 /// telling which tag name called the function, a trick with two inherited classes has to be used.
134 /// @param $content string - the content of the <wrgmap> tag
135 /// @param $args array - the array of attribute name/value pairs for the tag
136 /// @param $parser Parser - the MW Parser object for the current page
138 /// @return string - the html for rendering the map
139 public static function render($content, $args, $parser, $frame) {
140 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
141 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
142 assert(in_array($tagname, array('wrmap', 'wrgmap')));
144 $parserOutput = $parser->getOutput();
145 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
146 $parserOutput->addModules('ext.wrmap');
148 // append all sledruns as icon
149 $json_features = array();
150 $show_sledruns = ($tagname == 'wrgmap');
151 if ($show_sledruns) {
152 $json_features = array_merge($json_features, sledruns_to_json_features());
157 $properties = array();
158 if (isset($args['lat'])) $properties['lat'] = (float) $args['lat']; // latitude as float value
159 if (isset($args['lon'])) $properties['lon'] = (float) $args['lon']; // longitude as float value
160 if (isset($args['zoom'])) $properties['zoom'] = (int) $args['zoom']; // zoom as int value
161 if (isset($args['width'])) $properties['width'] = (int) $args['width']; // width as int value
162 if (isset($args['height'])) $properties['height'] = (int) $args['height']; // height as int value
164 // append all elements in the XML
165 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
166 } catch (Exception $e) {
167 return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
170 // create final geojson
172 'type' => 'FeatureCollection',
173 'features' => $json_features,
174 'properties' => $properties
176 $json_string = json_encode($json);
178 // Create <div/> element where the map is placed in
179 global $wgExtensionAssetsPath;
180 $output = "<div class=\"wrmap\" style=\"border-style:none;\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
181 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
182 $output .= "</div>\n";
190 class WrMap extends WrBaseMap {
195 class WrGMap extends WrBaseMap {