Combined layer_sledmap and layer_point to just one layer (layer_point).
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.body.php
index 5f43adc683c115a162573ecb882f066c0ef26154..77d3f93e27c9fd3dc1d129f52ce3a6a863d18582 100644 (file)
@@ -1,23 +1,17 @@
 <?php
 
 // get value of key or default value if key does not exist
 <?php
 
 // get value of key or default value if key does not exist
-function array_get($key, $array, $default) {
-       if (array_key_exists($key, $array)) return $array[$key];
-       return $default;
-}
-
-
-// gets coordinates and returns an array of coordinate pairs, e.g.
+// gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
 // 47.12 N 11.87 E
 // 47.13 N 11.70 E
 // ->
 // 47.12 N 11.87 E
 // 47.13 N 11.70 E
 // ->
-// array(array(47.12, 11.87), array(47.13, 11.70))
+// array(array(11.87, 47.12), array(11.70, 47.13))
 function geo_to_coordinates($input) {
        $matches = array();
        $num_matches = preg_match_all('/(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
        $result = array();
        for ($i=0; $i!=$num_matches; ++$i) {
 function geo_to_coordinates($input) {
        $matches = array();
        $num_matches = preg_match_all('/(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
        $result = array();
        for ($i=0; $i!=$num_matches; ++$i) {
-               $result[] = array(floatval($matches[1][$i]), floatval($matches[2][$i]));
+               $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
        }
        return $result;
 }
        }
        return $result;
 }
@@ -36,23 +30,16 @@ function sledruns_to_json_features() {
                $lat = floatval($lat);
                $lon = floatval($lon);
                $title = Title::newFromText($sledrun['page_title']);
                $lat = floatval($lat);
                $lon = floatval($lon);
                $title = Title::newFromText($sledrun['page_title']);
-               $title_text = $title->getText();
-               $title_url = $title->getLocalUrl();
-               $condition = $sledrun['condition'];
-               if (!is_null($condition)) $condition = intval($condition);
+               $properties = array('type' => 'sledrun', 'name' => $title->getText(), 'wiki' => $title->getLocalUrl());
+               if (!is_null($sledrun['date_report'])) $properties[] = $sledrun['date_report'];
+               if (!is_null($sledrun['condition'])) $properties[] = intval($sledrun['condition']);
                $json_feature = array(
                        'type' => 'feature',
                        'geometry' => array(
                                'type' => 'Point',
                                'coordinates' => array($lon, $lat)
                        ),
                $json_feature = array(
                        'type' => 'feature',
                        'geometry' => array(
                                'type' => 'Point',
                                'coordinates' => array($lon, $lat)
                        ),
-                       'properties' => array(
-                               'type' => 'sledrun',
-                               'name' => $title_text,
-                               'wiki' => $title_url,
-                               'date_report' => $sledrun['date_report'],
-                               'condition' => $condition
-                       )
+                       'properties' => $properties
                );
                $json_features[] = $json_feature;
        }
                );
                $json_features[] = $json_feature;
        }
@@ -63,44 +50,41 @@ function sledruns_to_json_features() {
 
 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
 // Returns an array of features
 
 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
 // Returns an array of features
+// TODO: Error reporting
 function xml_to_json_features($input) {
        libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
        $xml = new SimpleXMLElement($input); // input
        $json_features = array(); // output
 function xml_to_json_features($input) {
        libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
        $xml = new SimpleXMLElement($input); // input
        $json_features = array(); // output
+       $point_type = array('gasthaus' => 'inn', 'haltestelle' => 'busstop', 'parkplatz' => 'carpark', 'achtung' => 'attention', 'punkt' => 'point');
+       $line_type = array('rodelbahn' => 'sledrun', 'gehweg' => 'walk', 'alternative' => 'alternative', 'lift' => 'lift', 'linie' => 'line');
        foreach ($xml as $feature) {
                // point
        foreach ($xml as $feature) {
                // point
-               if (in_array($feature->getName(), array('gasthaus', 'haltestelle', 'parkplatz', 'punkt'))) {
-                       $wiki = array_get('wiki', $feature, null);
-                       $name = array_get('name', $feature, null);
+               if (in_array($feature->getName(), array_keys($point_type))) {
+                       $properties = array('type' => $point_type[$feature->getName()]);
+                       if (array_key_exists('wiki', $feature)) $properties['wiki'] = $feature['wiki'];
+                       if (array_key_exists('name', $feature)) $properties['name'] = $feature['name'];
                        $json_feature = array(
                                'type' => 'feature',
                                'geometry' => array(
                                        'type' => 'Point',
                                        'coordinates' => geo_to_coordinates($feature)[0]
                                ),
                        $json_feature = array(
                                'type' => 'feature',
                                'geometry' => array(
                                        'type' => 'Point',
                                        'coordinates' => geo_to_coordinates($feature)[0]
                                ),
-                               'properties' => array(
-                                       'type' => $feature->getName(),
-                                       'name' => $name,
-                                       'wiki' => $wiki
-                               )
+                               'properties' => $properties
                        );
                        $json_features[] = $json_feature;
                }
                // line
                        );
                        $json_features[] = $json_feature;
                }
                // line
-               if (in_array($feature->getName(), array('rodelbahn', 'gehweg', 'alternative', 'lift', 'linie'))) {
-                       $color = array_get('farbe', $feature, null);
-                       $thickness = array_get('dicke', $feature, null);
+               if (in_array($feature->getName(), array_keys($line_type))) {
+                       $properties = array('type' => $line_type[$feature->getName()]);
+                       if (array_key_exists('farbe', $feature)) $properties['strokeColor'] = $feature['farbe'];
+                       if (array_key_exists('dicke', $feature)) $properties['strokeWidth'] = $feature['dicke'];
                        $json_feature = array(
                                'type' => 'feature',
                                'geometry' => array(
                                        'type' => 'LineString',
                                        'coordinates' => geo_to_coordinates($feature)
                                ),
                        $json_feature = array(
                                'type' => 'feature',
                                'geometry' => array(
                                        'type' => 'LineString',
                                        'coordinates' => geo_to_coordinates($feature)
                                ),
-                               'properties' => array(
-                                       'type' => $feature->getName(),
-                                       'color' => $color,
-                                       'thickness' => $thickness
-                               )
+                               'properties' => $properties
                        );
                        $json_features[] = $json_feature;
                }
                        );
                        $json_features[] = $json_feature;
                }
@@ -171,9 +155,9 @@ class WrBaseMap {
                
                return $output;
        }
                
                return $output;
        }
-
 }
 
 }
 
+
 // <wrmap> tag
 class WrMap extends WrBaseMap {
 }
 // <wrmap> tag
 class WrMap extends WrBaseMap {
 }