appendChild($dom->createElement('Style'));
$style->setAttribute("id",$id);
$style=$style->appendChild($dom->createElement('IconStyle'));
$style->appendChild($dom->createElement('scale',SCALE));
$style->appendChild($dom->createElement('color',$color));
$style->appendChild($dom->createElement('Icon'))->appendChild($dom->createElement('href',"http://".SERVER.ICONURL));
}
$ns=$dom->createElementNS('http://earth.google.com/kml/2.1','kml');
if(DEBUGCONNECTION){
$ns->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:debug', 'http://radioecology.info/ns/debug/1.0');
}
$node_h=$dom->appendChild($ns);
$node_h=$node_h->appendChild($dom->createElement('Document'));
$filinfo=fetchset("select df.* from ${prefix}datafiles df where df.id=?",$fileid,PDO::FETCH_ASSOC);
// Returns an row with meta information on the data set to be used in the decription
$node_h->appendChild($dom->createElement('name',"Mobildata - $fileid"));
// A general description for the data set:
$description="
- Filnavn: ${filinfo[name]}
- Prosjekt : ${filinfo[projectid]}
- Ansvarlig : ${filinfo[responsibleid]}
- System : ${filinfo[systemid]}
- Måletype : ${filinfo[measuretypeid]}
- Beskrivelse : ${filinfo[description]}
- Kalibrering : ${filinfo[calibrationid]}
- Snødekke : ${filinfo[snowcover]} cm
";
if(DEBUGCONNECTION){
foreach ($_REQUEST as $k=>$v){
$node_h->appendChild($dom->createElement("debug:$k",$v));
}}
$opacity='ff'; // Want the same opacity for all levels
$colors=array(
// kml color codes are bbggrr ...!!??! need the rrggbb for the legend - easiest to do it this way...
array('id'=>'rd','gbr'=>'0000ff','rgb'=>'ff0000'),
array('id'=>'or','gbr'=>'0099ff','rgb'=>'ff9900'),
array('id'=>'ye','gbr'=>'00ffff','rgb'=>'ffff00'),
array('id'=>'yg','gbr'=>'00ff99','rgb'=>'99ff00'),
array('id'=>'gr','gbr'=>'00ff00','rgb'=>'00ff00'));
foreach ($colors as $col){
createstyle($node_h,$col['id'],$opacity.$col['gbr']);
}
createstyle($node_h,'toolowcol',$opacity.'999999');
// Setting up a value -> color mapping TODO: Store in database
if($valcol=='tc'){
$max=6000;
$step=1000;
$min=0;
}
if($valcol=='svh'){
$max=600;
$step=100;
$min=0;
}
$legendheader="Total counts (cps)"; // this should be stored in the db together with the mapping...
$description.="$legendheader
";
$usedcol=0;
$description.="";
$gt='> ';
$oldmax='';
foreach ($colors as &$actualcol){ // &$ -> pointer to the element, can modify directly
$usedcol++;
$actualcol['level']=$max; // sets the level
$legtext=$usedcol==count($colors)?$min.$oldmax:$gt.$max.$oldmax;
$gt=''; // to avoid the gt before the following levels...
$oldmax=" - $max";
$description.="- $legtext
";
$max-=$step;
}
$description.="
";
$node_h->appendChild($dom->createElement('description'))->appendChild($dom->createCDATASection
($description)); // Since the description contains HTML, it must be added as CDATA
$actualcol['level']=$min; // Make sure that the last step covers all the way down to min
unset($actualcol); // just in case - a pointer into the array, may mess things up is misused
$sql="select lon,lat,${valcol},rd.id,ht.live from ${prefix}rawdata rd left join ${prefix}hit ht on(ht.rawdataid=rd.id) where rd.datafileid=? ";
// $dom->appendChild($dom->createElement('debug:sql',$sql));
$sqlh=$dbh->prepare($sql);
$sqlh->execute(array($fileid));
$error=$sqlh->errorInfo();
if($error[0]!='00000'){ // reports any database error
$dom->appendChild($dom->createElement('Err:Databaseerror',$error[2]));
}
while($row=$sqlh->fetch(PDO::FETCH_ASSOC)){
if ($row['lat'] && $row['lon']){
// pm is the base node for the new placemark
$pm=$dom->createElement('Placemark');
$val=$row[$valcol];
$actualcolor='';
if($val>$min){ // if $val is too small, set color to gray
reset($colors); // starts from the top again
do{ // Goes through each step and sets the color when the right level is found
$set=each($colors); // used this way it returns the next element of the array
$actualcolor=$val>=$set[1]['level']?"#".$set[1]['id']:'';
}while($actualcolor==''); // jumps out as soon as a value is found.
}else{
$actualcolor='#toolowcol';
}
$pm->appendChild($dom->createElement('description',sprintf('%s: %s
Spekter',$valcol,$val,SERVER,$row['id'])));
$pm->appendChild($dom->createElement('Point'))->appendChild($dom->createElement('coordinates',sprintf("%f,%f,0 ",$row['lon'],$row['lat'])));
if($row['id']==$elementid || $row['live']!=''){
// makes an extra copy of the point with a 'normal' marker for Hit points or point marked
// through the url: TODO: Distinguish between the types of markers
$node_h->appendChild($pm->cloneNode(true));
}
$pm->appendChild($dom->createElement('styleUrl',$actualcolor));
$node_h->appendChild($pm);
}
}
header("Content-Type: application/vnd-google-earth.kml+xml");
print($dom->saveXML());
?>