// 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);
+ $num_matches = preg_match_all('/\s*(\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[2][$i]), floatval($matches[1][$i]));
}
+ if (implode($matches[0]) != $input) throw new Exception('Falsches Koordinatenformat: ' . $input);
return $result;
}
// 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
$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) {
+ $given_properties = array();
+ foreach ($feature->attributes() as $key => $value) $given_properties[] = $key;
+
+ // determine feature type
+ $is_point = in_array($feature->getName(), array_keys($point_type));
+ $is_line = in_array($feature->getName(), array_keys($line_type));
+ if (!$is_point && !$is_line) {
+ throw new Exception('Unbekanntes Element <' . $feature->getName() . '>. Erlaubt sind: <' . implode('>, <', array_keys(array_merge($point_type, $line_type))) . '>.');
+ }
+
// point
- if (in_array($feature->getName(), array_keys($point_type))) {
+ if ($is_point) {
$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'];
+ $allowed_properties = array('name', 'wiki');
+ $wrong_properties = array_diff($given_properties, $allowed_properties);
+ if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
+ foreach ($given_properties as $property) {
+ $properties[$property] = (string) $feature[$property];
+ }
+ $coordinates = geo_to_coordinates($feature);
+ if (count($coordinates) != 1) throw new Exception('Das Element <' . $feature->getName() . '> muss genau ein Koordinatenpaar haben.');
$json_feature = array(
'type' => 'feature',
'geometry' => array(
'type' => 'Point',
- 'coordinates' => geo_to_coordinates($feature)[0]
+ 'coordinates' => reset($coordinates)
),
'properties' => $properties
);
$json_features[] = $json_feature;
}
// line
- if (in_array($feature->getName(), array_keys($line_type))) {
+ if ($is_line) {
$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'];
+ $allowed_properties = array('farbe', 'dicke');
+ $wrong_properties = array_diff($given_properties, $allowed_properties);
+ if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
+ if (isset($feature['farbe'])) $properties['strokeColor'] = (int) $feature['farbe']; // e.g. 6
+ if (isset($feature['dicke'])) $properties['strokeWidth'] = (string) $feature['dicke']; // e.g. #a200b7
$json_feature = array(
'type' => 'feature',
'geometry' => array(
try {
$json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
} catch (Exception $e) {
- return htmlspecialchars($e);
+ return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
}
// create final geojson