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'])) $properties['strokeColor'] = (string) $feature['farbe']; // e.g. #a200b7 // TODO: Check
103 if (isset($feature['dicke'])) $properties['strokeWidth'] = (int) $feature['dicke']; // e.g. 6 // TODO: Check
104 $json_feature = array(
107 'type' => 'LineString',
108 'coordinates' => geo_to_coordinates($feature)
110 'properties' => $properties
112 $json_features[] = $json_feature;
115 return $json_features;
121 /// Renders the <wrgmap> tag and the <wrmap> tag.
122 /// This class would be the only class needed but as the function render() toes not provide an argument
123 /// telling which tag name called the function, a trick with two inherited classes has to be used.
124 /// @param $content string - the content of the <wrgmap> tag
125 /// @param $args array - the array of attribute name/value pairs for the tag
126 /// @param $parser Parser - the MW Parser object for the current page
128 /// @return string - the html for rendering the map
129 public static function render($content, $args, $parser, $frame) {
130 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
131 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
132 assert(in_array($tagname, array('wrmap', 'wrgmap')));
134 $parserOutput = $parser->getOutput();
135 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
136 $parserOutput->addModules('ext.wrmap');
138 // append all sledruns as icon
139 $json_features = array();
140 $show_sledruns = ($tagname == 'wrgmap');
141 if ($show_sledruns) {
142 $json_features = array_merge($json_features, sledruns_to_json_features());
147 $properties = array();
148 if (isset($args['lat'])) $properties['lat'] = (float) $args['lat']; // latitude as float value
149 if (isset($args['lon'])) $properties['lon'] = (float) $args['lon']; // longitude as float value
150 if (isset($args['zoom'])) $properties['zoom'] = (int) $args['zoom']; // zoom as int value
151 if (isset($args['width'])) $properties['width'] = (int) $args['width']; // width as int value
152 if (isset($args['height'])) $properties['height'] = (int) $args['height']; // height as int value
154 // 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,
164 'properties' => $properties
166 $json_string = json_encode($json);
168 // Create <div/> element where the map is placed in
169 global $wgExtensionAssetsPath;
170 $width_s = (isset($properties['width'])) ? (string) $properties['width'] . 'px' : '100%';
171 $height_s = (isset($properties['height']) ? (string) $properties['height'] : 450) . 'px';
172 $output = "<div class=\"wrmap\" style=\"width: $width_s; height: $height_s; border-style:none;\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
173 $output .= "<script type=\"application/json\">";
174 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
175 $output .= "</script>";
176 $output .= "</div>\n";
184 class WrMap extends WrBaseMap {
189 class WrGMap extends WrBaseMap {