| 1 | This is the code used in the printing function for the MapServer application running at http://maps.macnoise.com/interactive/ |
| 2 | |
| 3 | Much of this code is hard coded specifically for this application, but hopefully bits and pieces can serve as a useful example. |
| 4 | |
| 5 | {{{ |
| 6 | <?php |
| 7 | set_time_limit(60000); |
| 8 | ini_set('max_execution_time',0); |
| 9 | dl("php_mapscript.so"); |
| 10 | $mapfile=$_REQUEST['map']; |
| 11 | $map = ms_newMapObj($mapfile); |
| 12 | $map->selectOutputFormat('jpeg'); //fpdf seems to play better with jpeg |
| 13 | |
| 14 | //get map extent |
| 15 | if ($_REQUEST['mapext']) { |
| 16 | $mapext = $_REQUEST['mapext']; |
| 17 | $extent = explode(" ", $mapext); |
| 18 | $map->setExtent($extent[0], $extent[1],$extent[2], $extent[3]); |
| 19 | } |
| 20 | |
| 21 | //get list of layers and cycle through to turn them on |
| 22 | if (isset($_REQUEST['layers'])) { |
| 23 | $layerlist = rtrim($_REQUEST['layers']); |
| 24 | $layers = explode(" ", $layerlist); |
| 25 | $numLayers = $map->numlayers; |
| 26 | for ($i = 0; $i < $numLayers; $i++) { |
| 27 | $activeLayer = $map->getlayer($i); |
| 28 | if ($activeLayer->status != 2) $activeLayer->set("status", 0); |
| 29 | } |
| 30 | foreach ($layers as $l) { |
| 31 | $activeLayer = $map->getlayerbyname($l); |
| 32 | $activeLayer->set("status", 1); |
| 33 | } |
| 34 | } |
| 35 | $ft=$map->getlayerbyname('FlightTracks'); |
| 36 | $ftstat=$ft->status; |
| 37 | // this is used to allow cgi substitutions in data strings |
| 38 | for ($i = 0; $i < $map->numlayers; $i++) { |
| 39 | $activeLayer = $map->getlayer($i); |
| 40 | $datastring=$activeLayer->data; |
| 41 | if (preg_match_all("/%([\w].+?)%/",$datastring,$matches,PREG_SET_ORDER)>0){ |
| 42 | foreach ($matches as $match){ |
| 43 | $search=$match[0]; |
| 44 | $replace=$match[1]; |
| 45 | if (isset($_REQUEST[$replace])){ |
| 46 | $replaceval=$_REQUEST[$replace]; |
| 47 | $datastring=str_replace($search,$replaceval,$datastring); |
| 48 | $activeLayer->set("data",$datastring); |
| 49 | } else { |
| 50 | if (!($activeLayer->name == 'FlightTracks' && isset($_REQUEST['sDate']) && isset($_REQUEST['eDate']))) $activeLayer->set("status", 0); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | } |
| 56 | $headerstring=''; |
| 57 | |
| 58 | //conditionals used to only display certain messages when certain layers are turned on |
| 59 | $ft=$map->getlayerbyname('FlightTracks'); |
| 60 | $ftstat=$ft->status; |
| 61 | $add=$map->getlayerbyname('house'); |
| 62 | $addstat=$add->status; |
| 63 | if ($ftstat==1) $headerstring=$headerstring.'Time Period: '.$_REQUEST['sDate'].'-'.$_REQUEST['eDate']; |
| 64 | if ($addstat==1) $headerstring=$headerstring.' Address Shown: '.$_REQUEST['add']; |
| 65 | |
| 66 | |
| 67 | $resolution=isset($_REQUEST['resolution'])?$_REQUEST['resolution']:150; //allows to set resolution or defaults to 150dpi |
| 68 | $mapfile_resolution=$map->resolution; |
| 69 | $scalefactor=$resolution/$mapfile_resolution; //how much everything needs to be multiplied by |
| 70 | $map->set('resolution',$resolution); // set new resolution for min/max scale renderings |
| 71 | |
| 72 | |
| 73 | // loop through layers to extract class images and names and to resize styles and truetype symbols |
| 74 | $numLayers = $map->numlayers; |
| 75 | $classimages=array(); |
| 76 | $classnames=array(); |
| 77 | for ($i=0; $i < $numLayers; $i++) { |
| 78 | $layer = $map->getlayer($i); |
| 79 | if ($layer->status == 1 ){ |
| 80 | for ($j=0; $j < $layer->numclasses; $j++) { |
| 81 | $class = $layer->getClass($j); |
| 82 | $label = $class->label; |
| 83 | if ($layer->type !=4){ |
| 84 | $classimageobj=$class->createLegendIcon(60,60); |
| 85 | $classimages[]=$classimageobj->saveWebImage(); |
| 86 | $classnames[]=$class->name; |
| 87 | } |
| 88 | if ($label->mindistance>1){ |
| 89 | $md=$label->mindistance; |
| 90 | $newmd=$md*$scalefactor; |
| 91 | $label->set('mindistance',$newmd); |
| 92 | } |
| 93 | if ($label->type == 'TRUETYPE') { |
| 94 | $labelsize=$label->size; |
| 95 | $newlabelsize=$labelsize*$scalefactor; |
| 96 | $label->set('size',$newlabelsize); |
| 97 | } |
| 98 | for ($k=0; $k < $class->numstyles; $k++) { |
| 99 | $style = $class->getStyle($k); |
| 100 | $style->set("size", $style->size * $scalefactor); |
| 101 | $style->set("offsetx", $style->offsetx * $scalefactor); |
| 102 | $style->set("offsety", $style->offsety * $scalefactor); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | $nrows=ceil(count($classnames)/4); // make 4 columns to display legend |
| 108 | $legheight=$nrows*.25; // determine height of legend |
| 109 | $mapheight=9.3-$legheight; // determine area left to draw map |
| 110 | $legstart=$mapheight+1.05; // determine where to start the legend |
| 111 | |
| 112 | $map->set("width", 7.6*$resolution); |
| 113 | $map->set("height", $resolution*$mapheight); // make map rectangle fit page |
| 114 | //echo $mapheight*$resolution; |
| 115 | //draw map |
| 116 | $imgMap = $map->draw(); |
| 117 | $urlMap = $imgMap->saveWebImage(); |
| 118 | |
| 119 | require('fpdf.php'); |
| 120 | class PDF extends FPDF |
| 121 | { |
| 122 | //Page header |
| 123 | function Header() |
| 124 | { |
| 125 | |
| 126 | } |
| 127 | |
| 128 | //Page footer |
| 129 | function Footer() |
| 130 | { |
| 131 | $this->SetY(-1); |
| 132 | $this->SetFont('Arial','I',7); |
| 133 | } |
| 134 | function draw_legend($textarray,$imgarray,$startx,$starty,$nr){ // function draws the 4 column table |
| 135 | $this->SetFont('Times','',12); |
| 136 | $y=$starty-.25; |
| 137 | $x=$startx; |
| 138 | $first=true; |
| 139 | |
| 140 | for ($i=0;$i<count($textarray);$i++){ |
| 141 | if ($i==$nr or $i==2*$nr or $i==3*$nr) { |
| 142 | $y=$starty-.25; |
| 143 | $x=$x+1.9; |
| 144 | $first=false; |
| 145 | } |
| 146 | $y=$y+.25; |
| 147 | $imgurl=$imgarray[$i]; |
| 148 | $text=$textarray[$i]; |
| 149 | $this->Image($imgurl,$x,$y,.20,.20); |
| 150 | $this->Text($x+.25,$y+.15,$text); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | //Instanciation of inherited class |
| 156 | $pdf=new PDF('P','in','letter'); |
| 157 | |
| 158 | $pdf->SetDisplayMode('fullpage'); |
| 159 | $pdf->SetMargins(.25,.25); |
| 160 | |
| 161 | $pdf->AddPage(); |
| 162 | |
| 163 | |
| 164 | $pdf->SetFont('Arial','B',14); |
| 165 | $pdf->Cell(0,.50,'MACNOISE Interactive Map',0,1,'C'); |
| 166 | $pdf->SetFont('Arial','',10); |
| 167 | $pdf->Cell(0,.02,$headerstring,0,1,'C'); |
| 168 | |
| 169 | |
| 170 | $pdf->SetLineWidth(.1); |
| 171 | $pdf->SetDrawColor(0,0,150); |
| 172 | $pdf->Rect(.25,.25,8,10.5); |
| 173 | |
| 174 | $pdf->Image($urlMap,.45,.9,7.6,$mapheight); |
| 175 | $pdf->Image("/var/apache/htdocs/graphics/MACnorth1.png",.45,.9,1,1); |
| 176 | |
| 177 | $pdf->SetLineWidth(.01); |
| 178 | $pdf->SetDrawColor(0,0,200); |
| 179 | $pdf->Rect(.40,.85,7.7,$mapheight+.1); |
| 180 | $pdf->Rect(.40,$legstart,7.7,$legheight+.1); |
| 181 | |
| 182 | $date=date('M j, Y g:i'); |
| 183 | $datemessage="Map created by http://maps.macnoise.com/interactive/ on $date. This information is to be used for reference purposes only."; |
| 184 | $disclaimer="The Metropolitan Airports Commission does not guarantee accuracy of the material contained herein and is not responsible for misuse or misinterpretation."; |
| 185 | $pdf->SetFont('Arial','I',7); |
| 186 | $pdf->Text(.45,10.55,$datemessage); |
| 187 | $pdf->Text(.45,10.65,$disclaimer); |
| 188 | $pdf->SetTitle('MACNoise Interactive Map'); |
| 189 | $pdf->draw_legend($classnames,$classimages,.45,$legstart+.05,$nrows); |
| 190 | |
| 191 | $pdf->Output(); |
| 192 | //$pdf->Output('macnoise.pdf','D'); |
| 193 | ?> |
| 194 | }}} |