Location: PHPKode > projects > jjfmapper > jjfmapper/lib/geoformat_graphiclayer.php
<?php #-*-Mode: php; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
    jjfMapper, a cartography program for PHP 4.
    Copyright (C) 2004  John J Foerch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


include_once (JJFM_LIBDIR.'/geoformat.php');

class geoformat_graphiclayer extends GeoFormat {
    
    var $operations;
    
    function geoformat_graphiclayer ($args) {
        if (! is_array($args['operations']))
        {
            $this->ok=false;
            $this->error = 'no operations were passed';
            return;
        }
        $this->operations = $args['operations'];
    }
    
    function Draw (&$projection) {
        
        foreach ($this->operations as $o_k => $o_v) {
            if (! isset($this->operations[$o_k]['operation'])) continue;
            switch ($this->operations[$o_k]['operation']) {
                case 'FILL':
                    $color = isset($this->operations[$o_k]['color']) ?
                        $this->operations[$o_k]['color'] :
                        'FFFFFF';
                    $this->fill_operation ($projection, $color);
                    break;
                case 'TEXT':
                    $tag = isset($this->operations[$o_k]['text']) ?
                        $this->operations[$o_k]['text'] :
                        '';
                    $align = isset($this->operations[$o_k]['align']) ?
                        $this->operations[$o_k]['align'] :
                        'RIGHT';
                    $valign = isset($this->operations[$o_k]['valign']) ?
                        $this->operations[$o_k]['valign'] :
                        'BOTTOM';
                    $background = isset($this->operations[$o_k]['background'])?
                        $this->operations[$o_k]['background'] :
                        'TRANSPARENT';
                    $color = isset($this->operations[$o_k]['color']) ?
                        $this->operations[$o_k]['color'] :
                        '000000';
                    $font = isset($this->operations[$o_k]['font']) ?
                        $this->operations[$o_k]['font'] :
                        2;
                    $size = isset($this->operations[$o_k]['size']) ?
                        $this->operations[$o_k]['size'] :
                        12;
                    $location = isset($this->operations[$o_k]['location']) ?
                        $this->operations[$o_k]['location'] :
                        false;

                    $this->text_operation ($projection, $tag, $align, $valign,
                                           $background, $color, $font, $size,
                                           $location);
                    
                    break;
                case 'GRID':
                    $interval = isset($this->operations[$o_k]['interval']) ?
                        $this->operations[$o_k]['interval'] :
                        1;
                    $latoffset = isset($this->operations[$o_k]['latoffset']) ?
                        $this->operations[$o_k]['latoffset'] :
                        0;
                    $lonoffset = isset($this->operations[$o_k]['lonoffset']) ?
                        $this->operations[$o_k]['lonoffset'] :
                        0;
                    $weight = isset($this->operations[$o_k]['weight']) ?
                        $this->operations[$o_k]['weight'] :
                        1;
                    $color = isset($this->operations[$o_k]['color']) ?
                        $this->operations[$o_k]['color'] :
                        'FFFFFF';
                    
                    $this->grid_operation ($projection, $interval, $latoffset,
                                           $lonoffset, $weight, $color);

                    break;
            }
        }
    }
    
    function Bounds () {
        return false;//fitting not supported
    }
    
    function Validate () {
        return true;
    }
    
    function fill_operation (&$projection, $color) {
        $projection->palette['bgcolor'] =
            ColorStringToResource ($color, $projection->mapimage);
        imagefilledrectangle($projection->mapimage,0,0,
                             $projection->imwidth-1,
                             $projection->imheight-1,
                             $projection->palette['bgcolor']);
    }

    
    function text_operation (&$projection, $tag, $align,
                             $valign, $background, $color, $font, $size,
                             $location) {

        if (is_numeric ($font))
        {
            $font_type='gdf';
        } else {
            if (strtolower(substr($font,-4)) == '.gdf')
            {
                if (file_exists ($font))
                {
                    $font = imageloadfont ($font);
                    if ($font === false)
                    {
                        $font = 2;
                    }
                } else {
                    $font = 2;
                }
                $font_type='gdf';
            } else {
                if (defined ('JJFM_USE_FT'))
                {
                    $font_type='ft';
                } elseif (defined ('JJFM_USE_TTF')) {
                    $font_type='ttf';
                } else {
                    $font = 2;
                    $font_type = 'gdf';
                }
            }
        }


        if ($font_type == 'gdf')
        {
            $this->text_operation_gdf (&$projection, $tag, $align, $valign,
                                       $background, $color, $font, $location);
        } else {
            $this->text_operation_ft (&$projection, $tag, $align, $valign,
                                      $background, $color, $font, $size,
                                      $location);
        }
    }

    function text_operation_gdf (&$projection, $tag, $align,
                                 $valign, $background, $color, $font,
                                 $location) {
        $nl = strlen($tag);
        $letterwidth = imagefontwidth ($font);
        $width = $nl * $letterwidth;
        $height = imagefontheight ($font);
        if ($location != '')
        {
            $location = explode (',',$location);
            if ($align == 'LEFT')
            {
                $left = $location[0] - ($width + $letterwidth);
            } elseif ($align == 'RIGHT') {
                $left = $location[0] + $letterwidth;
            } else {//center
                $left = $location[0] - ($width / 2);
            }

            if ($valign == 'TOP')
            {
                $top = $location[1] - $height;
            } elseif ($valign == 'BOTTOM') {
                $top = $location[1];
            } else {//center
                $top = $location[1] - ($height / 2);
            }

        } else {
            if ($align == 'LEFT') 
            {
                $left = $letterwidth;
            } elseif ($align == 'RIGHT') {
                $left = ($projection->imwidth - $width) - $letterwidth;
            } elseif ($align == 'CENTER') {
                $left = ($projection->imwidth - $width) / 2;
            }
                
            if ($valign == 'TOP')
            {
                $top = 5;
            } elseif ($valign == 'BOTTOM') {
                $top = $projection->imheight - ($height + 5);
            } elseif ($valign == 'CENTER') {
                $top = ($projection->imheight - $height) / 2;
            }
        }                
        if ($background != 'TRANSPARENT')
        {
            if ($background == 'AUTO')
            {
                $background = $projection->palette['bgcolor'];
            } else {
                $background = ColorStringToResource ($background,
                                                     $projection->mapimage);
            }
            imagefilledrectangle($projection->mapimage,
                                 $left-3,$top,$left + $width + 3,
                                 $top + $height,$background);
        }
                
        $color = ColorStringToResource ($color, $projection->mapimage);
                
        imagestring($projection->mapimage,$font,
                    $left, $top,$tag,$color);

    }

    function text_operation_ft (&$projection, $tag, $align, $valign,
                                $background, $color, $font, $size, $location) {

        if (defined ('JJFM_USE_FT'))
        {
            $bbox = imageftbbox ($size, 0, $font, $tag, array());
        } else {
            $bbox = imagettfbbox ($size, 0, $font, $tag);
        }
        $width = abs($bbox[2] - $bbox[0]);
        $height = abs($bbox[7] - $bbox[1]);
        if ($location != '')
        {
            $location = explode (',',$location);
            if ($align == 'LEFT')
            {
                $left = $location[0] - ($width + 10);
            } elseif ($align == 'RIGHT') {
                $left = $location[0] + 10;
            } else {//center
                $left = $location[0] - ($width / 2);
            }

            if ($valign == 'TOP')
            {
                $baseline = $location[1];
            } elseif ($valign == 'BOTTOM') {
                $baseline = $location[1] + $height;
            } else {//center
                $baseline = $location[1] + ($height / 2);
            }

        } else {
            if ($align == 'LEFT') 
            {
                $left = 10;
            } elseif ($align == 'RIGHT') {
                $left = ($projection->imwidth - $width) - 10;
            } elseif ($align == 'CENTER') {
                $left = ($projection->imwidth - $width) / 2;
            }
                
            if ($valign == 'TOP')
            {
                $baseline = 5 + $height;
            } elseif ($valign == 'BOTTOM') {
                $baseline = $projection->imheight - 5;
            } elseif ($valign == 'CENTER') {
                $baseline = ($projection->imheight + $height) / 2;
            }
        }
        $top = $baseline - $height;

        if ($background != 'TRANSPARENT')
        {
            if ($background == 'AUTO')
            {
                $background = $projection->palette['bgcolor'];
            } else {
                $background = ColorStringToResource ($background,
                                                     $projection->mapimage);
            }
            imagefilledrectangle($projection->mapimage,
                                 $left-3,$top,$left + $width + 3,
                                 $top + $height,$background);
        }

        $color = ColorStringToResource ($color, $projection->mapimage);

        if (defined ('JJFM_USE_GD201'))
            imagealphablending($projection->mapimage, true);
        if (defined ('JJFM_USE_FT'))
        {
            imagefttext ($projection->mapimage,$size,0,$left,$baseline,
                          $color,$font,$tag,array());
        } else {
            imagettftext ($projection->mapimage,$size,0,$left,$baseline,
                          $color,$font,$tag);
        }
        if (defined ('JJFM_USE_GD201'))
            imagealphablending($projection->mapimage, false);

    }


    function grid_operation (&$projection, $interval, $latoffset,
                             $lonoffset, $weight, $color) {

        $gridcolor = ColorStringToResource ($color, $projection->mapimage);
        lineweight ($weight, $projection->mapimage);
                    
        $r=$lonoffset;
        if ($projection->xmin < $r)
        {
            while ($r > $projection->xmin) { $r -= $interval; }
        } else {
            while ($r < $projection->xmin) { $r += $interval; }
        }
        for ($a = $r; $a <= $projection->xmax; $a += $interval) {
            list ($x,$y) = $projection->TranslateXY($a,0);
            imageline($projection->mapimage,$x,0,$x,
                      $projection->imheight,$gridcolor);
        }
                
        $r = $latoffset;
        if ($projection->ymin < $r)
        {
            while ($r > $projection->ymin) { $r -= $interval; }
        } else {
            while ($r < $projection->ymin) { $r += $interval; }
        }
                
        for ($a = $r; $a <= $projection->ymax; $a += $interval) {
            list ($x,$y) = $projection->TranslateXY(0,$a);
            imageline($projection->mapimage,0,$y,
                      $projection->imwidth,$y,$gridcolor);
        }


    }

}

?>
Return current item: jjfmapper