2012-04-01 62 views
0

我正在嘗試使用時間戳創建一個KML點。 MySQL的輸出是這樣的:從php創建時間戳KML

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" 

我做了其他的PHP腳本創建KML這是工作的罰款,但由於某種原因,這一塊一直無所事事。 PHP代碼如下所示:

<?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"); 
?> 

在這讓我變成瘋子之前請幫忙!


這是我用來製作折線的php腳本,它工作的很好。所以我不能確定爲什麼其他人不工作。

<?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

您是否收到任何錯誤?首先想到的是標題標籤正在xml輸出後發送。 – ChrisK 2012-04-01 12:32:07

+0

我得到的唯一回應是,沒有任何反應。使用Domdocument而不是「回聲」的收益是多少? – 2012-04-01 13:43:50

回答

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

這大概應該在哪裏是它..

$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"; 

或者即將

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

對不起,我誤會了youre設法實現與CONCAT座標cooooooompletely別的東西。累。因此,因爲其他查詢做它,猜測WHERE lat> 50 AND lon> 8丟失。一切看起來不錯。

+0

「SELECT KML_time,GROUP_CONCAT(lon,',',lat,',','ORDER BY ts DESC SEPARATOR'')AS座標FROM demo_timespan GROUP BY id」;在Mysql工作臺中工作正常,而不是在腳本中? – 2012-04-01 13:46:01

+0

我不確定它是否有效。從來沒有嘗試過,我知道這可能是複雜的查詢問題。你是否試圖讓你的代碼運行? – Xfile 2012-04-01 13:56:13

+0

在更改SQL視圖後將查詢編輯爲:「SELECT KML_time,coordinates FROM demo_timespan」。仍然沒有結果 – 2012-04-01 14:07:24

0

我不認爲你正在創建時間戳正確。 首先,正確的語法是時間戳(不是時間戳),你可能需要將其包裝在一個TimePrimitive

這樣的代碼看起來應該像

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

,也許有

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

這包含時間戳

編輯:看起來你的時間戳可能也是錯誤的。如果包含秒數,我認爲您需要在末尾附加'Z'來表示UTC,或者您需要添加類似'+03:00'的內容來指示如何將時間轉換爲UTC。 link

+0

那麼語法錯誤的是,月份部分應該像2月3日是03不是3. 但是,真正的問題是,我的服務器無法處理200萬點。所以真正的解決辦法是在SQL代碼中使用LIMIT和一個自動中繼器。 – 2012-04-02 19:21:54