= 100000) {
echo "Splitting $tileid with nodes=$nodes, starting at $mapid. Max id was $maxid, level = $level\n";
$command = "java -Xmx1792M -jar ~/garmin/utils/splitter/splitter.jar --no-trim --mapid=".$mapid." --max-nodes=".$nodes." --write-kml=".$tileid.".kml --geonames-file=$cities $tileid.osm.gz";
passthru($command);
$newtileid = $maxid;
// Determine the maximum id produced by the split
$newmaxid = getMaxSourceId();
// require at least two sub files
if (($newmaxid - $maxid) < 2) {
echo "The split did not result in at least two subtiles, try again with less nodes per tile\n";
$level++;
$newmaxid = subsplit($tileid, $maxid, $level);
} else {
// Render the new source files
$maxid++;
$tiles = $newmaxid - $maxid;
for ($i = 0; $i <= $tiles; $i++) {
echo "Start a new recursive render with $maxid, $newmaxid, $level, $nodes\n";
$newmaxid = render($maxid, $newmaxid, $level);
$maxid++;
}
echo "Done with subsplit at level $level\n";
}
} else {
echo "Subsplitting passed the minimum node count. Giving up....\n";
$newmaxid = $maxid;
}
return $newmaxid;
}
// Return the number of the file with the highest filenumber
function getMaxSourceId() {
$maxid = 0;
$ids = getSourceIds();
foreach ($ids as $id) {
if ($id > $maxid)
$maxid = $id;
}
return $maxid;
}
// Return the available filenumbers
function getSourceIds() {
$ids = array();
foreach (glob("*.osm.gz") as $filename) {
$parts = explode(".", $filename);
$ids[] = $parts[0];
}
return $ids;
}
// Delete a directory tree. Please ensure $dir ends with a slash
function delTree($dir) {
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
delTree( $file );
else
unlink( $file );
}
rmdir( $dir );
}
/*
* Below are some functions to update the list of available tiles from the initial split with the subsplit results
*/
function simplexml_append(SimpleXMLElement $parent, SimpleXMLElement $new_child){
$node1 = dom_import_simplexml($parent);
$dom_sxe = dom_import_simplexml($new_child);
$node2 = $node1->ownerDocument->importNode($dom_sxe, true);
$node1->appendChild($node2);
}
function simplexml_replace(SimpleXMLElement $parent, SimpleXMLElement $new_child){
$node1 = dom_import_simplexml($parent);
$dom_sxe = dom_import_simplexml($new_child);
$node2 = $node1->ownerDocument->importNode($dom_sxe, true);
$node1->parentNode->replaceChild($node2,$node1);
}
function updateKml($name) {
$kmlRed = "
";
$kmlBlue = "
";
// Load the original KML file
if (file_exists("$name.kml")) {
$xml = simplexml_load_file("$name.kml");
} else {
exit("Failed to open $name.kml.");
}
// loop through all KML files to update the original KML file
foreach (glob("*.kml") as $replacement) {
if (strpos($replacement, $name) === false) {
$id = explode(".", $replacement);
// Load each new KML file
$newxml = simplexml_load_file($replacement);
foreach ($xml->Document->Placemark as $oldPlacemark) {
if (strpos($oldPlacemark->name, $id[0]) !== false) {
// This KML file contains subtiles -> delete the tile they replace
$oNode = dom_import_simplexml($oldPlacemark);
$oNode->parentNode->removeChild($oNode);
break;
}
}
// Add the subtiles
foreach ($newxml->Document->Placemark as $newPlacemark) {
simplexml_append($xml->Document, $newPlacemark);
}
}
}
// Determine which tiles actually exist (update KML rendering)
$red = simplexml_load_string($kmlRed);
$blue = simplexml_load_string($kmlBlue);
simplexml_replace($xml->Document->Style, $red);
simplexml_append($xml->Document, $blue);
foreach ($xml->Document->Placemark as $tile) {
$id = $tile->name;
if (file_exists($id.".img") == true) {
$tile->styleUrl = "#Blue";
} else {
$tile->styleUrl = "#Red";
}
}
// Write out the new version of the KML file
$fh = fopen("$name.kml.1", "w+");
if ($fh) {
fwrite($fh, $xml->asXML());
fclose($fh);
}
// rename original KML file to old
rename("$name.kml", "$name.kml.old");
// rename new KML file to original
rename("$name.kml.1", "$name.kml");
}
/* The original Mkgmap commandline from the render() function:
* $output = passthru("ulimit -t 600 && java -Xmx1792M -ea -jar ~/garmin/utils/mkgmap/mkgmap.jar --adjust-turn-headings --latin1 --name-tag-list=name:en,int_name,name:zh_py,name:engels,name --remove-short-arcs --add-pois-to-areas --make-opposite-cycleways --link-pois-to-ways --route --description='OSM World Routable' --series-name='OSM World Routable' --input-file=$file", $retval);
*/
?>