<?php
/*$username = "androidappp";
 	$password = "A!ms99AA";
 	$database = "androidappp";
 	$server = "androidappp.db.4594972.hostedresource.com";*/
include("database.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
extract($_GET);
if($_GET['id']!="")
{
	$query = " SELECT GROUP_CONCAT(lng, ',', lat, ',', '5' separator ' ') AS coordinates FROM markers WHERE aid= '$id';";
	//echo " SELECT GROUP_CONCAT(lng, ',', lat, ',', '100' separator ' ') AS coordinates FROM markers WHERE aid= '1';";
	$result = mysql_query($query);
		
	$sql = "SELECT tree_name,CONCAT(lng, ',', lat, ',') AS coord FROM info WHERE aid= '$id'";
	$res = mysql_query($sql);
	$num = mysql_num_rows($res);
			
	$query_ward="select * from input_form where id='$id'";
	//echo "select * from input_form where id='$id'";
	$result_ward=mysql_query($query_ward);
	$row_ward=mysql_fetch_array($result_ward);
	$ward = $row_ward['ward_office'];
	
	$query_master_ward = mysql_query("select * from master_ward where wid='$ward'");
	$row_master_ward = mysql_fetch_array($query_master_ward);
	$wardName = $row_master_ward['wvalue'];
	
	$query_master_ward_drop = mysql_query("select * from master_ward_drop where wid='$ward'");
	$row_master_ward_drop = mysql_fetch_array($query_master_ward_drop);
	$wardName_drop = $row_master_ward_drop['dvalue'];
	
	
		
	if (!$result) 
	{
		die('Invalid query: ' . mysql_error());
	}
	if (!$result_ward) 
	{
		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://earth.google.com/kml/2.1','kml');
$parNode = $dom->appendChild($node);

//Create a Folder element and append it to the KML element
$fnode = $dom->createElement('Folder');
$folderNode = $parNode->appendChild($fnode);
//Iterate through the MySQL results
$row = @mysql_fetch_assoc($result);
//Create a Placemark and append it to the document
$node = $dom->createElement('Placemark');
$placeNode = $folderNode->appendChild($node);

//Create an id attribute and assign it the value of id column
$placeNode->setAttribute('id','linestring1');

//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','My path');
$placeNode->appendChild($nameNode);
$descNode= $dom->createElement('description', 'Number of Tree Found in This Region : '.$num);
$placeNode->appendChild($descNode);

//Create a LineString element
$lineNode = $dom->createElement('LineString');
$placeNode->appendChild($lineNode);
$exnode = $dom->createElement('extrude', '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['coordinates']);
$lineNode->appendChild($coorNode);
//##############################################################################################################################3


while($row1 =  mysql_fetch_assoc($res))
{
		
		$tree_name = $row1['tree_name'];
		
		$tree_query = mysql_query("select * from tree_name where tid = '$tree_name'");
		$tree_result = mysql_fetch_array($tree_query);
	
		$node = $dom->createElement('Placemark');
		$placeNode = $folderNode->appendChild($node);
		
		//Create an id attribute and assign it the value of id column
		$placeNode->setAttribute('id','linestring1');
		
		//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',$tree_result['tree_name']);
		$placeNode->appendChild($nameNode);
		$descNode= $dom->createElement('description', 'Surveyed Tree'.$num,'HI');
		$placeNode->appendChild($descNode);
		
		//Create a LineString element
		$lineNode = $dom->createElement('Point');
		$placeNode->appendChild($lineNode);
		$exnode = $dom->createElement('extrude', '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',$row1['coord']);
		$lineNode->appendChild($coorNode);
}
$kmlOutput = $dom->saveXML();

//assign the KML headers. 

$fileName = "SmartSurvey_".$wardName."_".$wardName_drop;
//echo $fileName;
header('Content-type: application/vnd.google-earth.kml+xml');
header('Content-Disposition: attachment; filename="'.$fileName.'".kml"');
echo $kmlOutput;
?>