#
# Area drawing functions
#

use warnings;
use strict;

sub CreateAreaFillRules
  {
   my($global,$data)=(@_);

   my($n,$m);

   # Check the data

   foreach $n (0..$#{$data})
     {
      if(overlapping_zooms($data->[$n]{'MIN-ZOOM-FILL'},$data->[$n]{'MAX-ZOOM-FILL'},$data->[$n]{'MIN-ZOOM-SYMBOL'},$data->[$n]{'MAX-ZOOM-SYMBOL'}))
        {
         print STDERR "Warning: Area layer '$data->[$n]{LAYER}' style '$data->[$n]{STYLE}' has overlapping zoom ranges for fill (*-ZOOM-FILL) and symbol (*-ZOOM-SYMBOL).\n";
        }

      warn_missing_information("Area","FILL","FILL-COLOUR",$data->[$n]);

      warn_missing_information("Area","SYMBOL","FILL-SYMBOL",$data->[$n]);

      warn_missing_information("Area","LINE","LINE-COLOUR",$data->[$n]);

      warn_missing_information("Area","LABEL","LABEL-COLOUR",$data->[$n]);
      warn_missing_information("Area","LABEL","LABEL-SIZE"  ,$data->[$n]);
      warn_missing_information("Area","LABEL","LABEL"       ,$data->[$n]);

      foreach $m ($n+1..$#{$data})
        {
         next if($data->[$n]{LAYER} ne $data->[$m]{LAYER});
         next if($data->[$n]{STYLE} ne $data->[$m]{STYLE});
         next if($data->[$n]{FILTER} ne $data->[$m]{FILTER} || $data->[$n]{'SUB-FILTER'} ne $data->[$m]{'SUB-FILTER'});

         warn_overlapping_lines("Area","FILL"  ,$data->[$n],$data->[$m]);
         warn_overlapping_lines("Area","SYMBOL",$data->[$n],$data->[$m]);
         warn_overlapping_lines("Area","LINE"  ,$data->[$n],$data->[$m]);
        }
     }

   # Create the layers, styles and rules

   my($zoom,$datum);

   foreach $zoom ($global->{MINZOOM}..$global->{MAXZOOM})
     {
      foreach $datum (@{$data})
        {
         # Create the symbolisers

         my(@symbolisers)=();

         if($zoom >= $datum->{'MIN-ZOOM-FILL'} && $zoom <= $datum->{'MAX-ZOOM-FILL'} &&
            $datum->{'FILL-COLOUR'} ne "")
           {
            push(@symbolisers,polygon_symboliser($datum->{'FILL-STYLE'},
                                                 'fill'=>$datum->{'FILL-COLOUR'}));
           }

         if($zoom >= $datum->{'MIN-ZOOM-SYMBOL'} && $zoom <= $datum->{'MAX-ZOOM-SYMBOL'} &&
            $datum->{'FILL-SYMBOL'} ne "")
           {
            push(@symbolisers,polygon_pattern_symboliser("",
                                                         'base'=>'symbols',
                                                         'file'=>$datum->{'FILL-SYMBOL'}));
           }

         if($zoom >= $datum->{'MIN-ZOOM-LINE'} && $zoom <= $datum->{'MAX-ZOOM-LINE'} &&
            $datum->{'LINE-COLOUR'} ne "")
           {
            push(@symbolisers,line_symboliser($datum->{'LINE-STYLE'},
                                              'stroke'=>$datum->{'LINE-COLOUR'}));
           }

         if($#symbolisers > -1)
           {
            my($sql)="way_area >= ".sprintf("%.0f",100/exp(log(4.0)*($zoom-14)));

            # Create the layer

            if($zoom>15)
              {
               add_layer($global,
                         name=>"areas--$datum->{LAYER}--z$zoom",
                         description=>"areas $datum->{LAYER}",
                         zoom=>$zoom,
                         'datasource-base'=>'postgis',
                         'sql-table'=>'planet_osm_polygon',
                         'extra-sql-order-clause'=>'way_area desc'
                        );
              }
            else
              {
               add_layer($global,
                         name=>"areas--$datum->{LAYER}--z$zoom",
                         description=>"areas $datum->{LAYER}",
                         zoom=>$zoom,
                         'datasource-base'=>'postgis',
                         'sql-table'=>'planet_osm_polygon',
                         'extra-sql-where-clause'=>$sql,
                         'extra-sql-order-clause'=>'way_area desc'
                        );
              }

            # Create the style (may re-use previous one if names match)

            add_style($global,
                      name=>"areas--$datum->{LAYER}--$datum->{STYLE}--z$zoom",
                      zoom=>$zoom);

            # Create the rule

            my($filter)="($datum->{FILTER})";

            if($datum->{'SUB-FILTER'} ne "")
              {
               $filter.=" and ($datum->{'SUB-FILTER'})";
              }

            add_rule($global,
                     zoom=>$zoom,
                     filter=>$filter,
                     symbolisers=>[@symbolisers]);
           }
        }
     }
  }

sub CreateAreaLabelRules
  {
   my($global,$data)=(@_);

   my($n,$m);

   # Check the data

   foreach $n (0..$#{$data})
     {
      foreach $m ($n+1..$#{$data})
        {
         next if($data->[$n]{LAYER} ne $data->[$m]{LAYER});
         next if($data->[$n]{STYLE} ne $data->[$m]{STYLE});
         next if($data->[$n]{FILTER} ne $data->[$m]{FILTER} || $data->[$n]{'SUB-FILTER'} ne $data->[$m]{'SUB-FILTER'});

         if(overlapping_zooms($data->[$n]{'MIN-ZOOM-LABEL'},$data->[$n]{'MAX-ZOOM-LABEL'},$data->[$m]{'MIN-ZOOM-LABEL'},$data->[$m]{'MAX-ZOOM-LABEL'}))
           {
            print STDERR "Warning: Area layer '$data->[$m]{LAYER}' style '$data->[$m]{STYLE}' has two entries with overlapping zoom ranges (MIN-ZOOM-LABEL/MAX-ZOOM-LABEL).\n";
           }
        }
     }

   # Create the layers, styles and rules

   my($zoom,$datum);

   foreach $zoom ($global->{MINZOOM}..$global->{MAXZOOM})
     {
      foreach $datum (@{$data})
        {
         if($zoom >= $datum->{'MIN-ZOOM-LABEL'} && $zoom <= $datum->{'MAX-ZOOM-LABEL'})
           {
            my($sql)="way_area >= ".sprintf("%.0f",160000/exp(log(4.0)*($zoom-14)));

            # Create the layer

            add_layer($global,
                      name=>"areas--labels--$datum->{LAYER}--z$zoom",
                      description=>"areas $datum->{LAYER}",
                      zoom=>$zoom,
                      'datasource-base'=>'postgis',
                      'sql-table'=>'planet_osm_polygon',
                      'extra-sql-where-clause'=>$sql,
                      'extra-sql-order-clause'=>'way_area desc'
                     );

            # Create the style (may re-use previous one if names match)

            add_style($global,
                      name=>"areas-labels--$datum->{LAYER}--$datum->{STYLE}--z$zoom",
                      zoom=>$zoom);

            # Create the rule

            my($filter)="($datum->{FILTER})";

            if($datum->{'SUB-FILTER'} ne "")
              {
               $filter.=" and ($datum->{'SUB-FILTER'})";
              }

            add_rule($global,
                     zoom=>$zoom,
                     filter=>$filter,
                     symbolisers=>[text_symboliser($datum->{'LABEL-STYLE'},
                                                   $datum->{'LABEL'},
                                                   'fill'=>$datum->{'LABEL-COLOUR'},
                                                   'size'=>$datum->{'LABEL-SIZE'},
                                                   'halo-radius'=>1,
                                                   'wrap-width'=>20,
                                                   'text-ratio'=>0.8,
                                                   'placement'=>'interior',
                                                   'fontset-name'=>'normal')]);
           }
        }
     }
  }

#
1;
