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'])) $properties['strokeWidth'] = (int) $feature['dicke']; // e.g. 6 // TODO: Check
109 $json_feature = array(
112 'type' => 'LineString',
113 'coordinates' => geo_to_coordinates($feature)
115 'properties' => $properties
117 $json_features[] = $json_feature;
120 return $json_features;
126 /// Renders the <wrgmap> tag and the <wrmap> tag.
127 /// This class would be the only class needed but as the function render() toes not provide an argument
128 /// telling which tag name called the function, a trick with two inherited classes has to be used.
129 /// @param $content string - the content of the <wrgmap> tag
130 /// @param $args array - the array of attribute name/value pairs for the tag
131 /// @param $parser Parser - the MW Parser object for the current page
133 /// @return string - the html for rendering the map
134 public static function render($content, $args, $parser, $frame) {
135 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
136 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
137 assert(in_array($tagname, array('wrmap', 'wrgmap')));
139 $parserOutput = $parser->getOutput();
140 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
141 $parserOutput->addModules('ext.wrmap');
143 // append all sledruns as icon
144 $json_features = array();
145 $show_sledruns = ($tagname == 'wrgmap');
146 if ($show_sledruns) {
147 $json_features = array_merge($json_features, sledruns_to_json_features());
152 $properties = array();
153 if (isset($args['lat'])) $properties['lat'] = (float) $args['lat']; // latitude as float value
154 if (isset($args['lon'])) $properties['lon'] = (float) $args['lon']; // longitude as float value
155 if (isset($args['zoom'])) $properties['zoom'] = (int) $args['zoom']; // zoom as int value
156 if (isset($args['width'])) $properties['width'] = (int) $args['width']; // width as int value
157 if (isset($args['height'])) $properties['height'] = (int) $args['height']; // height as int value
159 // append all elements in the XML
160 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
161 } catch (Exception $e) {
162 return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
165 // create final geojson
167 'type' => 'FeatureCollection',
168 'features' => $json_features,
169 'properties' => $properties
171 $json_string = json_encode($json);
173 // Create <div/> element where the map is placed in
174 global $wgExtensionAssetsPath;
175 $width_s = (isset($properties['width'])) ? (string) $properties['width'] . 'px' : '100%';
176 $height_s = (isset($properties['height']) ? (string) $properties['height'] : 450) . 'px';
177 $output = "<div class=\"wrmap\" style=\"width: $width_s; height: $height_s; border-style:none;\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
178 $output .= "<script type=\"application/json\">";
179 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
180 $output .= "</script>";
181 $output .= "</div>\n";
189 class WrMap extends WrBaseMap {
194 class WrGMap extends WrBaseMap {