从php创建时间戳KML


Creating Timestamp KML from php

我正在尝试制作一个带有时间戳的点KML。MySQL输出如下:

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"

我制作了另一个创建KML的php脚本,它运行良好,但由于某种原因,这个脚本一直无所事事。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");
?>
$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";

很抱歉,我误解了你试图通过CONCAT坐标来实现的目标——完全是其他事情。疲倦的因此,由于其他查询执行了此操作,所以猜测WHERE lat>50 AND lon>8丢失。一切看起来都很好。

我认为您没有正确创建时间戳。首先,正确的语法是TimeStamp(而不是TimeStamp),您可能需要将其封装在TimePrimitive 中

所以代码应该看起来像

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

也许有

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

其中包含TimeStamp

编辑:看起来你的时间戳可能也错了。当包含秒时,我认为您需要在末尾添加一个"Z"来表示UTC,或者需要添加类似"+03:00"的内容来表示如何将时间转换为UTC。链接