changeset 30:04dd1cfdd4c3 gmap2

Cleanups, fixes.
author ccr@tnsp.org
date Sat, 08 Jan 2011 17:10:42 +0200
parents 2aa6069aeb7e
children 4a0cf87a9c9c
files tools/makegmaps.php
diffstat 1 files changed, 88 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/tools/makegmaps.php	Sat Jan 08 16:48:24 2011 +0200
+++ b/tools/makegmaps.php	Sat Jan 08 17:10:42 2011 +0200
@@ -1,18 +1,27 @@
 #!/usr/bin/php
 <?php
 
+$rawPath = "raw/";
+$cachePath = "cache/";
 $fontFile = "lucon.ttf";
+
 $locPath = "maputils/maps/";
-$outPath = "../";
+$markerPath = "../";
+
 $mkloc = "maputils/mkloc";
 $map2ppm = "maputils/map2ppm";
 
+$tradelaneInCSV = "tradelane.txt";
+$tradelaneOutXML = $markerPath."tradelane.xml";
+
 $modes = array(
   "xml"     => "markers.xml",
   "overlay" => "overlay.txt",
   "labels"  => "overlaylabels.txt",
 );
 
+require "config.php";
+
 $mapPalette = array();
 $mapPalette["!"]  = array(204,255,255);
 $mapPalette["%"]  = array(  0,170,170);
@@ -54,9 +63,11 @@
 $mapPalette["~"]  = array( 51, 51,170);
 $mapPalette["1"]  = array(255,102, 16);
 
-require "world.php";
 
-
+/*
+ * Generate marker files from LOC data
+ */
+echo "Converting location data from LOC files to GMaps markers...\n";
 $args = "";
 foreach ($continentList as $name => $t) {
   if ($t[4]) { // has a map
@@ -69,28 +80,45 @@
 }
 
 foreach ($modes as $mode => $file) {
-  $tmp = escapeshellcmd($mkloc)." -v -o ".escapeshellarg($outPath.$file)." -G ".$mode." ".$args;
+  echo "* ".$mode.": ".$markerPath.$file."\n";
+  $tmp = escapeshellcmd($mkloc)." -v -o ".escapeshellarg($markerPath.$file)." -G ".$mode." ".$args;
   passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 }
 
 
-function makeMap($input, $name, $map, $zoom, $data)
+/*
+ * Generate PNG maps
+ */
+function makeMap($inFilename, $outFilename, $zlevel, $data)
 {
-  global $palette;
-  
-  $zoom = pow(2,$zoom-1);
+  global $mapPalette, $fontFile;
+
+  // Try to open input file
+  $file = @fopen($inFilename, "r");
+  if ($file === FALSE) {
+    echo "Could not open input '".$inFilename."'\n";
+    return FALSE;
+  }
+
+  //   
+  $zoom = pow(2, $zlevel - 1);
+  $width = $data[5];
+  $height = $data[6];
+
+  // Create image and assign colors
   $im = @imagecreate($width*$zoom, $height*$zoom);
   $black = imagecolorallocate($im, 0, 0, 0);
-  foreach ($palette as $id => $val)
+  foreach ($mapPalette as $id => $val)
     $colors[$id] = imagecolorallocate($im, $val[0], $val[1], $val[2]);
 
   imagefilledrectangle($im, 0, 0, $width*$zoom, $height*$zoom, $black);
-  
+
+  // Font sizes
   $fontsize[8]  = 7;
   $fontsize[16] = 13;
   $fontsize[32] = 26;
 
-  $file = fopen("raw/".$filename, "r");
+  // Read input raw
   $y = 0;
   while ($y < $height && ($data = fgets($file, 4096)) !== FALSE) 
   {
@@ -106,29 +134,65 @@
           $x*$zoom + $fontsize[$zoom]/4,
           $y*$zoom + $fontsize[$zoom],
           $colors[$data[$x]],
-          "./lucon.ttf",
+          $fontFile,
           $data[$x]);
       }
     }
     $y++;
-    echo ".";
+//    echo ".";
+  }
+
+//  echo $outfile;
+
+  if (imagepng($im, $outFilename) === FALSE) {
+    echo "Error creating '".$outFilename."'\n";
+    imagedestroy($im);
+    return FALSE;
   }
 
-  echo $name;
-  if (imagepng($im, $name) === FALSE)
-    echo "Error creating '".$map."' -> '".$name."'\n";
+  imagedestroy($im);
+  return TRUE;
+}
+
 
-  imagedestroy($im);
+echo "Generating basic map data...\n";
+foreach($continentList as $name => $data)
+if ($data[4])
+{
+  for ($zoom = 1; $zoom <= 5; $zoom++)
+  {
+    makeMap($rawPath.$name.".txt", $cachePath.$name."_".($zoom + 4).".png", $zoom, $data);
+  }
 }
 
 
-foreach($continentList as $name => $data)
-{
-  for ($zoom = 1; $zoom < 6; $zoom++)
-  {
-    echo "\nGenerating: ".$map.", zoom level: ".$zoom." ";
-    makeMap("cache/".$name."_".($zoom + 4).".png", $name, $zoom, $data);
-  }
+/*
+ * Convert tradelane data to XML
+ */
+echo "\nConverting tradelane data '".$tradelaneInCSV."' -> '".$tradelaneOutXML."'.\n";
+
+$doc = new DOMDocument("1.0", "UTF-8");
+$node = $doc->createElement("markers");
+$parnode = $doc->appendChild($node);
+  
+$file = fopen($tradelaneInCSV, "r");
+while (($data = fgetcsv($file, 512, "\t")) !== FALSE) {
+  if ($data[0][0] == "#") continue;
+
+  list ($x, $y, $name) = $data;
+
+  $html = "<b>TRADELANE WPT</b><br>".$name;
+
+  $node = $doc->createElement("marker");
+  $newnode = $parnode->appendChild($node);
+  $newnode->setAttribute("x", $x + 4096);
+  $newnode->setAttribute("y", $y + 4096);
+  $newnode->setAttribute("name", $name);
+  $newnode->setAttribute("html", $html);
+  $newnode->setAttribute("continent", "");
+  $newnode->setAttribute("type", "tradelane");
 }
 
+$doc->save($tradelaneOutXML);
+
 ?>
\ No newline at end of file