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 $properties[$property] = (string) $feature[$property];
79 $coordinates = geo_to_coordinates($feature);
80 if (count($coordinates) != 1) throw new Exception('Das Element <' . $feature->getName() . '> muss genau ein Koordinatenpaar haben.');
81 $json_feature = array(
85 'coordinates' => reset($coordinates)
87 'properties' => $properties
89 $json_features[] = $json_feature;
93 $properties = array('type' => $feature->getName());
94 $allowed_properties = array('farbe', 'dicke');
95 $wrong_properties = array_diff($given_properties, $allowed_properties);
96 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
97 if (isset($feature['farbe'])) $properties['strokeColor'] = (string) $feature['farbe']; // e.g. #a200b7 // TODO: Check
98 if (isset($feature['dicke'])) $properties['strokeWidth'] = (int) $feature['dicke']; // e.g. 6 // TODO: Check
99 $json_feature = array(
102 'type' => 'LineString',
103 'coordinates' => geo_to_coordinates($feature)
105 'properties' => $properties
107 $json_features[] = $json_feature;
110 return $json_features;
116 /// Renders the <wrgmap> tag and the <wrmap> tag.
117 /// This class would be the only class needed but as the function render() toes not provide an argument
118 /// telling which tag name called the function, a trick with two inherited classes has to be used.
119 /// @param $content string - the content of the <wrgmap> tag
120 /// @param $args array - the array of attribute name/value pairs for the tag
121 /// @param $parser Parser - the MW Parser object for the current page
123 /// @return string - the html for rendering the map
124 public static function render($content, $args, $parser, $frame) {
125 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
126 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
127 assert(in_array($tagname, array('wrmap', 'wrgmap')));
129 $parserOutput = $parser->getOutput();
130 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
131 $parserOutput->addModules('ext.wrmap');
133 // append all sledruns as icon
134 $json_features = array();
135 $show_sledruns = ($tagname == 'wrgmap');
136 if ($show_sledruns) {
137 $json_features = array_merge($json_features, sledruns_to_json_features());
142 $properties = array();
143 if (isset($args['lat'])) $properties['lat'] = (float) $args['lat']; // latitude as float value
144 if (isset($args['lon'])) $properties['lon'] = (float) $args['lon']; // longitude as float value
145 if (isset($args['zoom'])) $properties['zoom'] = (int) $args['zoom']; // zoom as int value
146 if (isset($args['width'])) $properties['width'] = (int) $args['width']; // width as int value
147 if (isset($args['height'])) $properties['height'] = (int) $args['height']; // height as int value
149 // append all elements in the XML
150 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
151 } catch (Exception $e) {
152 return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
155 // create final geojson
157 'type' => 'FeatureCollection',
158 'features' => $json_features,
159 'properties' => $properties
161 $json_string = json_encode($json);
163 // Create <div/> element where the map is placed in
164 global $wgExtensionAssetsPath;
165 $width_s = (isset($properties['width'])) ? (string) $properties['width'] . 'px' : '100%';
166 $height_s = (isset($properties['height']) ? (string) $properties['height'] : 450) . 'px';
167 $output = "<div class=\"wrmap\" style=\"width: $width_s; height: $height_s; border-style:none;\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
168 $output .= "<script type=\"application/json\">";
169 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
170 $output .= "</script>";
171 $output .= "</div>\n";
179 class WrMap extends WrBaseMap {
184 class WrGMap extends WrBaseMap {