2012-04-01 10 views
0

Ich versuche, einen Punkt KML mit Zeitstempel zu machen. Die MySQL-Ausgabe sieht wie folgt aus:Erstellen Zeitstempel KML von PHP

KML_time,coordinates 
2012-3-26T04:36:39-01:00,"9.92016904,57.04809917,0" 
2012-3-26T04:36:54-01:00,"9.92017704,57.04809437,0" 
2012-3-26T04:37:08-01:00,"9.92011376,57.04819547,0" 

ich andere PHP-Skript gemacht haben KML zu schaffen, die funktioniert prima, aber aus irgendeinem Grund dieses hält nichts zu tun. Der PHP-Code sieht so aus:

<?php 
require('../db_conf2.php'); 

// Opens a connection to a MySQL server 
$connection = mysql_connect ($server, $username, $password); 

if (!$connection) 
{ 
    die('Not connected : ' . mysql_error()); 
} 

// Set the active MySQL database 
$db_selected = mysql_select_db($database, $connection); 

if (!$db_selected) 
{ 
    die ('Can\'t use db : ' . mysql_error()); 
} 

// Select all the rows in the markers table 

$query_trips = "SELECT KML_time, GROUP_CONCAT(lon, ',', lat, ',', '0' ORDER BY ts DESC SEPARATOR ' ') AS coordinates FROM demo_timespan GROUP BY id"; 

$result_trips = mysql_query($query_trips); 
if (!$result_trips) 
{ 
    die('Invalid query: ' . mysql_error()); 
} 

// Start KML file, create parent node 
$dom = new DOMDocument('1.0','UTF-8'); 

//Create the root KML element 
$node = $dom->createElementNS('http://www.opengis.net/kml/2.2','kml'); 
$dom->appendChild($node); 

//Create a Document element and append it to the KML element 
$documentNode = $dom->createElement('Document'); 
$node->appendChild($documentNode); 

//Create a hiker icon Style element and append it to the Document 
$styleNode = $dom->createElement('Style'); 
$documentNode->appendChild($styleNode); 
$styleNode->setAttribute('id','hiker-icon'); 

$iconStyleNode = $dom->createElement('IconStyle'); 
$styleNode->appendChild($iconStyleNode); 

$iconNode = $dom->createElement('Icon'); 
$iconStyleNode->appendChild($iconNode); 
$hrefNode = $dom->createElement('href', 'http://maps.google.com/mapfiles/ms/icons/hiker.png'); 
$iconNode->appendChild($hrefNode); 

//Create a check-hide-children Style element and append it to the KML element 
$styleNode = $dom->createElement('Style'); 
$documentNode->appendChild($styleNode); 
$styleNode->setAttribute('id','check-hide-children'); 

$ListStyleNode = $dom->createElement('ListStyle'); 
$styleNode->appendChild($ListStyleNode); 

$listItemTypeNode = $dom->createElement('listItemType', 'checkHideChildren'); 
$ListStyleNode->appendChild($listItemTypeNode); 


//Create a check-hide-children URL element and append it to the KML element 
$styleUrlNode = $dom->createElement('styleUrl', '#check-hide-children'); 
$documentNode->appendChild($styleUrlNode); 


//Iterate through the MySQL results 
while ($row_trip = mysql_fetch_assoc($result_trips)) 
{ 

//Create a Placemark and append it to the document 
$placenode = $dom->createElement('Placemark'); 
$documentNode->appendChild($placenode); 

//Create a Timestamp and append it to the PlaceMark 
$Timestampnode = $dom->createElement('Timestamp'); 
$placeNode->appendChild($Timestampnode); 

//Create a When and append it to the Timestamp 
$whenNode = $dom->createElement('when',$row_trip['KML_time']); 
$Timestampnode->appendChild($whenNode); 

//Create a StyleUrl and append it to the PlaceMark 
$styleURLNode= $dom->createElement('styleUrl', '#hiker-icon'); 
$placeNode->appendChild($styleURLNode); 

//Create a Point element 
$PointNode = $dom->createElement('Point'); 
$placeNode->appendChild($PointNode); 
//Create a coordinates element and give it the value of the lng and lat columns from the results 
$coorNode = $dom->createElement('coordinates',$row_trip['coordinates']); 
$PointNode->appendChild($coorNode); 

} 
$dom->saveXML(); 

//assign the KML headers. 
header('Content-type: application/vnd.google-earth.kml+xml'); 

$dom->save("../store_kml/Timespan.kml"); 
?> 

Bitte helfen, bevor dies mich zum Wahnsinnigen macht!


Dies ist das PHP-Skript, das ich verwende, um Polylinien zu machen, und es funktioniert gut. So kann ich nicht wirklich sehen, warum die anderen nicht funktionieren.

<?php 
require('../db_conf2.php'); 

// Opens a connection to a MySQL server 
$connection = mysql_connect ($server, $username, $password); 

if (!$connection) 
{ 
    die('Not connected : ' . mysql_error()); 
} 

// Set the active MySQL database 
$db_selected = mysql_select_db($database, $connection); 

if (!$db_selected) 
{ 
    die ('Can\'t use db : ' . mysql_error()); 
} 

// Select all the rows in the markers table 

$query_trips = "SELECT trip_id, type, mood, why, GROUP_CONCAT(lon, ',', lat, ',', '0' ORDER BY ts DESC SEPARATOR ' ') AS coordinates FROM data_demo WHERE lat>50 AND lon>8 GROUP BY trip_id"; 

$result_trips = mysql_query($query_trips); 
if (!$result_trips) 
{ 
    die('Invalid query: ' . mysql_error()); 
} 

// Start KML file, create parent node 
$dom = new DOMDocument('1.0','UTF-8'); 

//Create the root KML element and append it to the Document 
$node = $dom->createElementNS('http://www.opengis.net/kml/2.2','kml'); 
$dom->appendChild($node); 

//Create a Document element and append it to the KML element 
$dNode = $dom->createElement('Document'); 
$node->appendChild($dNode); 

//Create a gå Style element and append it to the KML element 
$styleNode = $dom->createElement('Style'); 
$dNode->appendChild($styleNode); 
$styleNode->setAttribute('id','gå'); 

$lineStyleNode = $dom->createElement('LineStyle'); 
$styleNode->appendChild($lineStyleNode); 

$colorNode = $dom->createElement('color', 'AABF3900'); 
$lineStyleNode->appendChild($colorNode); 
$widthNode =$dom->createElement('width','6'); 
$lineStyleNode->appendChild($widthNode); 

//Create a cykel Style element and append it to the KML element 
$styleNode = $dom->createElement('Style'); 
$dNode->appendChild($styleNode); 
$styleNode->setAttribute('id','cykel'); 

$lineStyleNode = $dom->createElement('LineStyle'); 
$styleNode->appendChild($lineStyleNode); 

$colorNode = $dom->createElement('color', 'AA16BF00'); 
$lineStyleNode->appendChild($colorNode); 
$widthNode =$dom->createElement('width','6'); 
$lineStyleNode->appendChild($widthNode); 

//Create a kollektiv Style element and append it to the KML element 
$styleNode = $dom->createElement('Style'); 
$dNode->appendChild($styleNode); 
$styleNode->setAttribute('id','kollektiv'); 

$lineStyleNode = $dom->createElement('LineStyle'); 
$styleNode->appendChild($lineStyleNode); 

$colorNode = $dom->createElement('color', 'AA00BFAC'); 
$lineStyleNode->appendChild($colorNode); 
$widthNode =$dom->createElement('width','6'); 
$lineStyleNode->appendChild($widthNode); 

//Create a bil Style element and append it to the KML element 
$styleNode = $dom->createElement('Style'); 
$dNode->appendChild($styleNode); 
$styleNode->setAttribute('id','bil'); 

$lineStyleNode = $dom->createElement('LineStyle'); 
$styleNode->appendChild($lineStyleNode); 

$colorNode = $dom->createElement('color', 'AA9600BF'); 
$lineStyleNode->appendChild($colorNode); 
$widthNode =$dom->createElement('width','6'); 
$lineStyleNode->appendChild($widthNode); 




//Iterate through the MySQL results 
while ($row_trip = mysql_fetch_assoc($result_trips)) 
{ 
//Create a Placemark and append it to the document 
$placeNode = $dom->createElement('Placemark'); 
$dNode->appendChild($placeNode); 

//Create an id attribute and assign it the value of id column 
$placeNode->setAttribute('id','linestring' . $row_trip['trip_id'] . ''); 

//Create name, description, and address elements and assign them the values of 
//the name, type, and address columns from the results 

$nameNode = $dom->createElement('name','Tur nummer: '. $row_trip['trip_id'] .''); 
$placeNode->appendChild($nameNode); 
$descNode= $dom->createElement('description', '' . $row_trip['mood'] . ' '. $row_trip['type'] .'tur fordi "'. $row_trip['why'] .'".'); 
$placeNode->appendChild($descNode); 
$styleURLNode= $dom->createElement('styleUrl', '#' . $row_trip['type'] . ''); 
$placeNode->appendChild($styleURLNode); 

//Create a LineString element 
$lineNode = $dom->createElement('LineString'); 
$placeNode->appendChild($lineNode); 
$exnode = $dom->createElement('tessellate', '1'); 
$lineNode->appendChild($exnode); 
$almodenode =$dom->createElement(altitudeMode,'relativeToGround'); 
$lineNode->appendChild($almodenode); 

//Create a coordinates element and give it the value of the lng and lat columns from the results 
$coorNode = $dom->createElement('coordinates',$row_trip['coordinates']); 
$lineNode->appendChild($coorNode); 


} 
$dom->saveXML(); 

//assign the KML headers. 
header('Content-type: application/vnd.google-earth.kml+xml'); 

$dom->save("../store_kml/PolylineType.kml"); 
?> 
+0

Erhalten Sie irgendwelche Fehler? Erste Gedanken sind, dass das Header-Tag nach der XML-Ausgabe gesendet wird. – ChrisK

+0

Die einzige Antwort, die ich bekomme, ist, nichts passiert. Was ist der Nutzen von Domodocument und nicht "echo"? –

Antwort

0
$query_trips = "SELECT KML_time, GROUP_CONCAT(lon, ',', lat, ',', '0' ORDER BY ts DESC SEPARATOR ' ') AS coordinates FROM demo_timespan GROUP BY id"; 

Wahrscheinlich das, wo in es sein sollte ..

$query_trips = "SELECT KML_time, GROUP_CONCAT(lon, ',', lat, ',', '0' ORDER BY ts DESC SEPARATOR ' ') AS coordinates FROM demo_timespan WHERE lat>50 AND lon>8 GROUP BY id"; 

oder dies nur

$query_trips = "SELECT KML_time, GROUP_CONCAT(coordinates) AS coordinates FROM demo_timespan ORDER BY id"; 

Leider habe ich falsch verstanden, was du versuchen, mit CONCAT zu erreichen Koordinaten cooooooompletely etwas anderes . Müde. Also, da andere Abfrage es tut, rate WHERE lat> 50 AND lon> 8 fehlt. Alles sieht gut aus.

+0

Wie kommt es "SELECT KML_time, GROUP_CONCAT (lon, ',', lat, ',', '0' ORDER BY ts DESC SEPARATOR '') als Koordinaten VON demo_timespan GROUP BY id"; funktioniert gut in Mysql Workbench und nicht in einem Skript? –

+0

Ich bin mir nicht sicher, ob es funktioniert. Habe es noch nie probiert, alles was ich weiß, kann bei komplexen Abfragen problematisch sein. Hast du versucht, deine Codes laufen zu lassen? – Xfile

+0

bearbeitet die Abfrage zu: "SELECT KML_time, Koordinaten FROM demo_timespan" nach dem Ändern der SQL-Ansicht. Noch keine ergebnisse –

0

Ich denke nicht, dass Sie den Zeitstempel richtig erstellen. Zunächst wird die korrekte Syntax ist Timestamp (nicht Zeitstempel) und man könnte es in einem TimePrimitive wickeln müssen

so sollte Code aussehen

 $Timestampnode = $dom->createElement('TimeStamp'); 

und haben vielleicht eine

 $TimePrimitivenode = $dom->createElement('TimePrimitive'); 

die enthält den TimeStamp

Edit: Sieht aus wie Sie Timestamp auch falsch sein könnte. Wenn man Sekunden einbezieht, muss man entweder ein 'Z' am Ende anhängen, um UTC anzuzeigen, oder man muss etwas wie '+03: 00' hinzufügen, um anzuzeigen, wie die Zeit in UTC umgerechnet wird. link

+0

Nun der Syntaxfehler ist, dass der Monat Teil 2-stellig haben sollte wie März ist 03 nicht 3. Allerdings ist das eigentliche Problem, dass mein Server nur 2 Millionen Punkt nicht behandeln kann. Die eigentliche Lösung ist die Verwendung von LIMIT im SQL-Code und einem automatischen Repeater. –