# HG changeset patch # User Matti Hamalainen # Date 1394637522 -7200 # Node ID 3adc7a98ef0ae7b65ec2a5caff6b1a55060ae901 # Parent 29643b59072277d2225ac25d57d62c9925bf57e0 Improve bootstrapping by generating htaccess files automagically. diff -r 29643b590722 -r 3adc7a98ef0a tools/makegmaps.php --- a/tools/makegmaps.php Wed Mar 12 16:48:57 2014 +0200 +++ b/tools/makegmaps.php Wed Mar 12 17:18:42 2014 +0200 @@ -18,6 +18,14 @@ $fontFile = "./lucon.ttf"; $tileDim = 256; +$minZoom = 1; +$maxZoom = 10; + +// Font sizes +$fontSize = array(); +$fontSize[ 8] = 7; +$fontSize[16] = 13; +$fontSize[32] = 26; /* Internal data and settings @@ -85,6 +93,69 @@ } +function outputToFile($sfilename, $sdata) +{ + if (file_put_contents($sfilename, $sdata, LOCK_EX) === FALSE) + die("Error writing to '".$sfilename."'.\n"); +} + + +/* + * Create htaccess files + */ +function makeHtAccessFile($surlprefix, $sprefix, $spath) +{ + $sfile = $sprefix.$spath.".htaccess"; + if (!file_exists($sfile) && file_exists($sprefix.$spath."sea.png")) + { + if (!isset($surlprefix)) + { + $sdone = FALSE; + while (!$sdone) + { + echo "Enter URL prefix for tiles:\n"; + $surlprefix = trim(fgets(STDIN)); + if (substr($surlprefix, 0, 7) != "http://" && + substr($surlprefix, 0, 8) != "https://") + { + echo "Prefix must start with http:// or https://\n"; + } + else + if (substr($surlprefix, -1) != "/") + { + echo "Prefix must end with /\n"; + } + else + if (preg_match("/^https?:\/\/[a-zA-Z0-9]+[a-zA-Z0-9\/\.\:\-]*?\//", $surlprefix) === FALSE) + { + echo "Malformed URL (or atleast this silly regexp does not want it.\n"; + } + else + { + echo "The URL prefix to be used is \"".$surlprefix."\", e.g.\n". + "htaccess to be created would be: \"".$surlprefix."tiles/".$spath."sea.png\"\n". + "Is this correct? [y/N]? "; + + $sprompt = strtolower(trim(fgets(STDIN))); + if ($sprompt == "y") + $sdone = TRUE; + + echo "\n"; + } + } + } + + + echo "Creating ".$sfile."\n"; + outputToFile($sfile, "ErrorDocument 404 ".$surlprefix."tiles/".$spath."sea.png\n"); + } +} + +makeHtAccessFile(&$urlTilePrefix, $pathTileData, ""); +for ($i = $minZoom; $i <= $maxZoom; $i++) + makeHtAccessFile(&$urlTilePrefix, $pathTileData, $i."/"); + + /* * Build maputils and fetch latest map data */ @@ -153,8 +224,7 @@ } $str .= "];\n"; -if (file_put_contents($continentJS, $str) === FALSE) - die("Error writing continent data to ".$continentJS."\n"); +outputToFile($continentJS, $str); @@ -213,10 +283,9 @@ } -function outputJSON($qfilename, $qdata) +function outputToJSONFile($qfilename, $qdata) { - if (file_put_contents($qfilename, json_encode($qdata), LOCK_EX) === FALSE) - die("Could not write to file '".$qfilename."'.\n"); + outputToFile($qfilename, json_encode($qdata)); } @@ -247,7 +316,7 @@ ); } -outputJSON($tradelaneOut, $qdata); +outputToJSONFile($tradelaneOut, $qdata); /* Export tradelane polyline data @@ -276,14 +345,14 @@ } -outputJSON($tradelaneOverlay, $qdata); +outputToJSONFile($tradelaneOverlay, $qdata); /* * Generate PNG maps */ function makeMap($inFilename, $outFilename, $zlevel, $data) { - global $mapPalette, $fontFile; + global $mapPalette, $fontFile, $fontSize; // Try to open input file $file = @fopen($inFilename, "r"); @@ -312,11 +381,6 @@ imagefilledrectangle($im, 0, 0, $width*$zoom, $height*$zoom, $black); - // Font sizes - $fontsize[ 8] = 7; - $fontsize[16] = 13; - $fontsize[32] = 26; - // Read input raw $y = 0; while ($y < $height && ($data = fgets($file, 4096)) !== FALSE) @@ -530,7 +594,7 @@ $mapData = array(); -for ($zoom = 1; $zoom <= 10; $zoom++) +for ($zoom = $minZoom; $zoom <= $maxZoom; $zoom++) { $zoom2 = $zoom - 1; $scale = pow(2, $zoom2 - 5);