readPointsFromCSVFile($input_file_path);
$tel1 = 0; //$coord_array starts at 0
$color = 1111;
// TODO : resolve buf that vertical lines are not drawn all the way down
// TODO : determine height from horizontal spreading of points
// (points spread across 100s or 1000s of km need a high framework to
// be visible).
//var_dump ($coord_array);
foreach ($coord_array["id_array"] as $brol) {
// trim avoids nasty troubles with carriage returns in input file, that
// mess up the kml structure
$lat[$coord_array["id_array"][$tel1]] = trim($coord_array["latitude_array"][$tel1]);
$lon[$coord_array["id_array"][$tel1]] = trim($coord_array["longitude_array"][$tel1]);
$tel1++;
}
//echo('
');
//var_dump ($lat);
//$inputstring = "(1,((2,3), ((((4,10),(5,8)),(6,9)),7)))";
$inputstring = "(41,((33,((((12,((6,((3,(1,2)),(4,5))),((7,8),(9,(10,11))))),(13,14)),((18,(17,(15,16))),((21,(19,20)),(22,((23,24),(25,(26,27))))))),((28,29),(30,(31,32))))),(34,35)),(36,((37,38),(39,40))))";
//$inputstring = "(((1,2),3),4)";
// get rid of nasty whitespaces
$inputstring = str_replace(" ","",$inputstring);
//$lat = array(
//"1"=>22,
//"2"=>16,
//"3"=>16.2,
//"4"=>17,
//"5"=>18.2,
//"6"=>21,
//"7"=>21,
//"8"=>20,
//"9"=>21,
//"10"=>17.2);
//$lon = array(
//"1"=>10,
//"2"=>10,
//"3"=>10,
//"4"=>10,
//"5"=>10,
//"6"=>10,
//"7"=>10,
//"8"=>10,
//"9"=>10,
//"10"=>10);
$levels = array();
//$inputstring = "(1,(A, (((B,C),D),7)))";
$tempstring = $inputstring;
$cnt = 0;
$tl=0;
$id = "my_id".$cnt;
$i=1;
$output_file = fopen('/home/bmeganck/Desktop/out.kml','w');
//$comma_count = substr_count($inputstring,",");
//$fwpos = 0;
// while ($inputstring <> "") {
$outstring = '
Paths
Examples of paths. Note that the tessellate tag is by default
set to 0. If you want to create tessellated lines, they must be authored
(or edited) directly in KML.
';
while (substr_count($inputstring,",")>1) {
//$comma_count = substr_count($inputstring,",");
//echo $comma_count;
//$fwpos = 0;
// for ($i=1;$i<=$comma_count;$i++) {
//for ($i=1;$i<20;$i++) {
// we search for a comma, then we check both sides of it
// if we find an opening bracket on the left and a closing bracket to the right,
// we replace that part with a single node
$pos = strpos($inputstring,",",$fwpos);
$fwpos = $pos + 1;
$rwpos = $pos - 1;
$ffwd=substr($inputstring,$fwpos,1);
$frwd=substr($inputstring,$rwpos,1);
while ($ffwd <> ")" && $ffwd <> "(") {
$ffwd=substr($inputstring,$fwpos,1);
$fwpos++;
} // while $ffwd
if ($ffwd == "(") { }
elseif ($ffwd == ")") {
// this could be something
while ($frwd <> ")" && $frwd <> "(") {
$frwd=substr($inputstring,$rwpos,1);
$rwpos--;
} // while $frwd
if ($frwd == "(") {
// yes! we have a hit !
//echo ("level0");
//echo ("
");
$cutstring = substr($inputstring,$rwpos+1,($fwpos-$rwpos-1));
$no_brack1 = str_replace(")","",$cutstring);
$no_brackets = str_replace("(","",$no_brack1);
//$no_brackets = trim($no_brack2," ");
// echo ($no_brackets);
$pieces = explode(",",$no_brackets);
//echo $pieces[0];
//echo ($cutstring);
//$cutstring = str_replace(" ","",$cutstring);
//echo ($cutstring);
//echo ($id);
//echo ("
");
$tempstring = str_replace($cutstring,$id,$inputstring);
$cnt +=1;
// #yellowLineGreenPoly
$outstring.= '
Absolute Extruded
1
Transparent green wall with yellow outlines
0
0
relativeToGround
';
if (array_key_exists ($pieces[0],$levels)) {
$lvl1 = $levels ["$pieces[0]"]["level"];
}
else {$levels ["$pieces[0]"]["level"] = 1;
$lvl1=1;}
if (array_key_exists ($pieces[1],$levels)) {
$lvl2 = $levels ["$pieces[1]"]["level"];
}
else {$levels ["$pieces[1]"]["level"] = 1;
$lvl2=1;}
$lvl = max($lvl1,$lvl2)+1;
$levels["$id"]["level"] = $lvl;
$lat["$id"]= min($lat[$pieces[0]],$lat[$pieces[1]]) + (( max($lat[$pieces[0]],$lat[$pieces[1]])
- min($lat[$pieces[0]],$lat[$pieces[1]]) ) /2);
$lon["$id"]= min($lon[$pieces[0]],$lon[$pieces[1]]) + (( max($lon[$pieces[0]],$lon[$pieces[1]])
- min($lon[$pieces[0]],$lon[$pieces[1]]) ) /2);
if (array_key_exists($pieces[0],$lat)) {
//echo ($lvl);
$outstring .= $lat[$pieces[0]].",".$lon[$pieces[0]].",";
$outstring .= ($levels ["$pieces[0]"]["level"])*12000;
$outstring .= "\n";
$outstring .= $lat[$pieces[0]].",".$lon[$pieces[0]].",";
$outstring .= ($lvl)*12000;
$outstring .= "\n";
}
if (array_key_exists($pieces[1],$lat)) {
$outstring .= $lat[$pieces[1]].",".$lon[$pieces[1]].",";
$outstring .= ($lvl)*12000;
$outstring .= "\n";
// echo("
");
$outstring .= $lat[$pieces[1]].",".$lon[$pieces[1]].",";
$outstring .= (($levels ["$pieces[1]"]["level"])*12000);
$outstring .= "\n";
// echo("
");
}//-------------------------------------------------------------------------------
// CSV_Module.php
// Handle checking, parsing, and writing of comma separated value (csv) files
//
// written by Bart Meganck, Royal Museum for Central Africa
// bart.meganck@africamuseum.be
//
//
// This program is free & open source, under the GNU General Public License, v.2
// See : http://www.gnu.org/copyleft/gpl.html for more details.
//
//-------------------------------------------------------------------------------
// $color += 150;
$outstring .= '
';
// $outstring .= '
// id1
//
// ';
// $outstring .= $lat[$pieces[0]].",".$lon[$pieces[0]].",";
// $outstring .= $lvl*12000;
// $outstring .= '
//
// ';
// echo $lvl;
$id = "my_id".$cnt;
//echo($fwpos);
$fwpos = $fwpos - ($fwpos-$rwpos-1);
// echo ("
");
//echo($fwpos);
// echo ("
");
// echo($tempstring);
} // if $frwd
} // elseif
else {};
//$worksub = substr($inputstring,$pos,$len);
//$single = substr($worksub,0,1);
//while ($single<>")" && $single<>"(" ) {
//$worksub = substr($inputstring,$i,$count);
//$count++;
//$single = substr($worksub,$count,1);
//echo ($worksub);
//echo('
');
//echo("$i");
//echo($single);
//echo('
');
// } // while
// if ($single == ")") {}
// else {}
$inputstring = $tempstring;
// } // for $i
//echo ($tempstring);
$i++;
} // while $inputstring <> ""
$outstring .='
';
fwrite ($output_file,$outstring);
fclose($output_file);
//var_dump ($levels);
echo ('OK. Done.');
?>