Migrate to GeoJSON output, because it's just so much nicer than KML...

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-06 13:34:52 +02:00
parent dbdb9df725
commit 2e6fdd2d4c
2 changed files with 80 additions and 42 deletions

View file

@ -20,32 +20,51 @@ $sql = "SELECT ts, lat, lon FROM tracker WHERE device='" . mysqli_real_escape_st
$result = $conn->query($sql); $result = $conn->query($sql);
if ($result->num_rows > 0) { if ($result->num_rows > 0) {
header('Content-Type: application/vnd.google-earth.kml+xml'); header('Content-Type: application/vnd.geo+json');
header('Content-Disposition: attachment; filename="export.kml"'); header('Content-Disposition: attachment; filename="export.geojson"');
echo '<?xml version="1.0" encoding="UTF-8"?> $output = '{
<kml xmlns="http://www.opengis.net/kml/2.2"> "type": "FeatureCollection",
<Document> "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
<Placemark> "features": [
<ExtendedData> { "type": "Feature",
<Data name="styleUrl"><value>#style</value></Data> "properties": {
<Data name="styleHash"><value>1a1ac94e</value></Data> "Name": null,
<Data name="stroke"><value>#ffff00</value></Data> "description": null,
<Data name="stroke-opacity"><value>0.4980392156862745</value></Data> "timestamp": null,
<Data name="stroke-width"><value>4</value></Data> "begin": null,
<Data name="fill"><value>#00ff00</value></Data> "end": null,
<Data name="fill-opacity"><value>0.4980392156862745</value></Data> "altitudeMode": null,
</ExtendedData> "tessellate": -1,
<LineString> "extrude": 0,
<coordinates> "visibility": -1,
"drawOrder": null,
"icon": null,
"styleUrl": "#style",
"styleHash": "1a1ac94e",
"stroke": "#ffff00",
"stroke_opacity": "0.4980392156862745",
"stroke_width": "4",
"fill": "#00ff00",
"fill_opacity": "0.4980392156862745"
},
"geometry": {
"type": "LineString",
"coordinates": [
'; ';
while($row = $result->fetch_assoc()) { while($row = $result->fetch_assoc()) {
echo $row["lon"] . "," . $row["lat"] . "\n"; $output .= " [ " . $row["lon"] . ", " . $row["lat"] . " ],\n";
} }
echo '</coordinates> $output = substr_replace($output, "", strrpos($output, ","), 1);
</LineString></Placemark>
</Document></kml>'; $output .= ' ]
}
}
]
}';
echo $output;
} else { } else {
die("No records found."); die("No records found.");

61
day.php
View file

@ -20,32 +20,51 @@ $sql = "SELECT ts, lat, lon FROM tracker WHERE device='" . mysqli_real_escape_st
$result = $conn->query($sql); $result = $conn->query($sql);
if ($result->num_rows > 0) { if ($result->num_rows > 0) {
header('Content-Type: application/vnd.google-earth.kml+xml'); header('Content-Type: application/vnd.geo+json');
header('Content-Disposition: attachment; filename="export.kml"'); header('Content-Disposition: attachment; filename="export.geojson"');
echo '<?xml version="1.0" encoding="UTF-8"?> $output = '{
<kml xmlns="http://www.opengis.net/kml/2.2"> "type": "FeatureCollection",
<Document> "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
<Placemark> "features": [
<ExtendedData> { "type": "Feature",
<Data name="styleUrl"><value>#style</value></Data> "properties": {
<Data name="styleHash"><value>1a1ac94e</value></Data> "Name": null,
<Data name="stroke"><value>#ffff00</value></Data> "description": null,
<Data name="stroke-opacity"><value>0.4980392156862745</value></Data> "timestamp": null,
<Data name="stroke-width"><value>4</value></Data> "begin": null,
<Data name="fill"><value>#00ff00</value></Data> "end": null,
<Data name="fill-opacity"><value>0.4980392156862745</value></Data> "altitudeMode": null,
</ExtendedData> "tessellate": -1,
<LineString> "extrude": 0,
<coordinates> "visibility": -1,
"drawOrder": null,
"icon": null,
"styleUrl": "#style",
"styleHash": "1a1ac94e",
"stroke": "#ffff00",
"stroke_opacity": "0.4980392156862745",
"stroke_width": "4",
"fill": "#00ff00",
"fill_opacity": "0.4980392156862745"
},
"geometry": {
"type": "LineString",
"coordinates": [
'; ';
while($row = $result->fetch_assoc()) { while($row = $result->fetch_assoc()) {
echo $row["lon"] . "," . $row["lat"] . "\n"; $output .= " [ " . $row["lon"] . ", " . $row["lat"] . " ],\n";
} }
echo '</coordinates> $output = substr_replace($output, "", strrpos($output, ","), 1);
</LineString></Placemark>
</Document></kml>'; $output .= ' ]
}
}
]
}';
echo $output;
} else { } else {
die("No records found."); die("No records found.");