<?
/**
 * Draw maps using mapserver <http://mapserver.gis.umn.edu> and custom map file <blank.map>
 *
 * @author James Jefferson Jarvis <jj@aprsworld.net>, Copyright 2004
 * Released under the GNU GPL license.
 *
 * @param $lat    Latitude of point (and usually center of map)
 * @param $lon Longitude of point (and usually center of map)
 * @param $icon Name of mapserver icon to label point above
 * @param $label label for point

 * @param $scale set the scale of the map to draw
 * @param $lat_scale used when scale=='box' to set the up/down area of the bounding box
 * @param $lon_scale used when scale=='box' to set the left/right area of the bounding box
 * @param $info_text text to display in the lower left hand corner of the map

 * @param $nocache if true then don't used the cached image
 * @param $track_shapefile data to use to show a series of unlabeled points
 * @param $icon_size size in pixels the icons should be
 * @param $layers layers seperated by spaces to draw, overrides the defaults in blank.map
 * if $scale is extent then bounding information comes from
 * @param $minx Minimum x coordinate (longitude) of bounding box
 * @param $maxx Maximum x coordinate (longitude) of bounding box
 * @param $miny Minimum y coordinate (latitude) of bounding box
 * @param $maxy Maximum y coordinate (latitude) of bounding box
*/
 

$track_shapefile $_REQUEST["track_shapefile"];
$map $_REQUEST["map"];
$icon $_REQUEST["icon"];
$icon_size $_REQUEST["icon_size"];
$scale $_REQUEST["scale"];
$minx $_REQUEST["minx"];
$maxx $_REQUEST["maxx"];
$minx $_REQUEST["minx"];
$miny $_REQUEST["miny"];
$lat $_REQUEST["lat"];
$lon $_REQUEST["lon"];
$lat_scale $_REQUEST["lat_scale"];
$lon_scale $_REQUEST["lon_scale"];
$info_text $_REQUEST["info_text"];
$nocache $_REQUEST["nocache"];
$label $_REQUEST["label"];

$s_old_code substr($icon,9);
$s_code sprintf("%03d",$s_old_code);
if ( 
strtolower(substr($icon,0,9)) == "aprs_pri_" ) {
    
$icon "047_" $s_code;
} else if ( 
strtolower(substr($icon,0,9)) == "aprs_alt_" ) {
    
$icon "092_" $s_code;
}
 

/* Our simple caching ... if everything is the same then we don't need to redraw */
$maphash md5($lat $lon $label $icon $scale $lat_scale $lon_scale $info_text $map);
$mapfile "/var/www/html/tmp/" $maphash ".map";        // output file to render

if ( "radar" == $map ) {
    
$nocache=1;
    
$mapfile_template "/var/www/html/mapserver/radar.map";
} else {
    
$mapfile_template "/var/www/html/mapserver/blank.map";    // map to customize
}
$mapimage "/var/www/html/tmp/" $maphash ".png";        // rendered image that we are caching

/* simple caching: if mapimage exists and nocache is false then we use already rendered map */
if ( ! $nocache  && file_exists($mapimage) ) {
    
header("Content-type: image/x-png");
    
passthru("cat " $mapimage);
    return;
}
    
if ( 
"" != $track_shapefile  ) {
    
$new_layer[] = sprintf("
    LAYER
        NAME track
        TYPE POINT
        STATUS DEFAULT
        DATA \"%s\"
    
        CLASS
            SYMBOL 'circle'
            SIZE 6
            COLOR 0 255 0
        END
    END
    "
,$track_shapefile);

}
                                                

/* set defaults */
if ( $icon == "" ) {
    
$icon "circle";
    
$icon_size=10;
}

if ( ! 
$icon_size )
    
$icon_size=19;

/* Set scale */
$scale strtolower($scale);
$skip_set=0;
switch ( 
$scale ) {
    case 
"box":
        
/* use supplied $lon_scale and $lat_scale */
        
break;
    case 
"extent":
        
/* do nothing. minx,miny,maxx,maxy should already be set */
        
$skip_set=1;
        break;
    case 
"street":
        
$lon_scale=0.002;
        
$lat_scale=0.002;
        break;
    case 
"town":
        
$lon_scale=0.005;
        
$lat_scale=0.005;
        break;
    case 
"county":
        
$lon_scale=0.01;
        
$lat_scale=0.01;
        break;
    case 
"europe":
        
$lon_scale=$lat_scale=4;
        break;
//    case "usa":
//        /* ( -178.218, 18.925 ) ( -66.969, 71.406 ) */
//        $minx = -178.218; $miny=18.925;
//        $maxx = -66.969; $maxy=71.406;
//        $skip_set=1;    
//        break;
    
case "regional":
    default:
        
/* default to a regional scale */
        
$lon_scale=2.5;
        
$lat_scale=1.5;
}

if ( ! 
$skip_set ) {
    
$minx $lon-$lon_scale;
    
$maxx $lon+$lon_scale;
    
$miny $lat-$lat_scale;
    
$maxy $lat+$lat_scale;
}


$tp file($mapfile_template);

$map implode("",$tp);

$map str_replace("##LAT##",$lat,$map);
$map str_replace("##LON##",$lon,$map);
$map str_replace("##LABEL##",$label,$map);
$map str_replace("##ICON##",$icon,$map);
$map str_replace("##ICON_SIZE##","SIZE " $icon_size,$map);
$map str_replace("##INFO_TEXT##",$info_text,$map);

for ( 
$i=$i<count($new_layer) ; $i++ ) {
    
$placeholder sprintf("##LAYER_%02d##",$i);
    
$map str_replace($placeholder,$new_layer[$i],$map);

}


// $layers="aprs us_ilabels us_rivers us_water us_interstates us_cities us_streets us_cities";

$command sprintf("/usr/local/bin/shp2img -m %s -o %s -e %f %f %f %f ",$mapfile,$mapimage,$minx,$miny,$maxx,$maxy);

/* write map file */
$fp fopen($mapfile,"w");
fputs($fp,$map);
fclose($fp);

if ( 
$layers != "" )
    
$command .= "-l " $layers;

if ( 
$debug ) {
    
header("Content-type: text/plain");
    
printf("<PRE>\n");
    
printf("layer[0] = %s\n",$new_layer[0]);
    
printf("mapfile name: %s\n",$mapfile);
    
printf("command to render map: %s\n"$command);
    
printf("map:\n\n%s\n\n",$map);
    
printf("</PRE>\n");
    

    exit();

}


if ( 
abs($lat) > 90 || abs($lon) > 180 ) {
    
header("Content-type: text/plain");

    
printf("Invalid Coordinates.");
} else {
    
header("Content-type: image/x-png");

     
    
exec$command );
    
passthru("cat " $mapimage);

}

unlink($mapfile);
?>