Fixed: Now name of points gets added to the GeoJSON properties.
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.body.php
index f6ed44545c53ea81e11eeb47334934994aa16f60..b6bd40b29b5c585b45b16f93057b484bbbe0dc6c 100644 (file)
@@ -1,12 +1,6 @@
 <?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 lon/lat coordinate pairs, e.g.
 // 47.12 N 11.87 E
 // 47.13 N 11.70 E
@@ -56,36 +50,34 @@ function sledruns_to_json_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
+       $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
-               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 (isset($feature['wiki'])) $properties['wiki'] = (string) $feature['wiki'];
+                       if (isset($feature['name'])) $properties['name'] = (string) $feature['name'];
                        $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
                if (in_array($feature->getName(), array_keys($line_type))) {
-                       $properties = array();
-                       if (array_key_exists('farbe', $feature)) $properties['strokeColor'] = $feature['farbe'];
-                       if (array_key_exists('dicke', $feature)) $properties['strokeWidth'] = $feature['dicke'];
-                       $properties['type'] = $line_type[$feature->getName()];
+                       $properties = array('type' => $line_type[$feature->getName()]);
+                       if (isset($feature['farbe'])) $properties['strokeColor'] = (string) $feature['farbe'];
+                       if (isset($feature['dicke'])) $properties['strokeWidth'] = (string) $feature['dicke'];
                        $json_feature = array(
                                'type' => 'feature',
                                'geometry' => array(