<?php
/*
OpenDataBag - Data Web Interface
Copyright (C) 2004 Nawara
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.
*/
//USE
// http://127.0.0.1/test-b/odb_index.php?odb_run=map&odb_map=ch-postal&odb_search=abc&odb_region_field_name=Region&odb_value_field_name=Income&odb_colorset=redgreen
$value_field_name=get('value_field_name');
$region_field_name=get('region_field_name');
$image=get('map');
$colorset=get('colorset');
$agregation='avg';
if($image=='generate')
{
// Color map - distribute ------------------------------------------
$ii=0;
$point_array=array();
$value_array=array();
$result_array=search($search.' ['.$value_field_name.']');
foreach($result_array as $line)
{
$longitude=round(read($line,'Longitude'),6);
$latitude=round(read($line,'Latitude'),6);
if($longitude and $latitude)
{
$value=read($line,$value_field_name);
$value=str_replace(' ','',$value);
$value=str_replace('.','',$value);
$value=str_replace(',','',$value);
//$value=0+$value;
$point_array[$ii]['longitude']=$longitude;
$point_array[$ii]['latitude']=$latitude;
$point_array[$ii]['value']=$value;
if($longitude<$region_longitude_min or $ii==0) $region_longitude_min=$longitude;
if($longitude>$region_longitude_max or $ii==0) $region_longitude_max=$longitude;
if($latitude<$region_latitude_min or $ii==0) $region_latitude_min=$latitude;
if($latitude>$region_latitude_max or $ii==0) $region_latitude_max=$latitude;
$value_array[$value]=1;
$ii++;
}
}
if(($region_latitude_max-$region_latitude_min)!=0 and ($region_longitude_max-$region_longitude_min)!=0)
{
$acpect=abs(($region_longitude_max-$region_longitude_min)/($region_latitude_max-$region_latitude_min));
$width=80;
$range=10;
$long_lat_aspect=0.625;
$height=$width*$acpect*$long_lat_aspect;
$square_array=array();
$im = imagecreatetruecolor($width,$height);
imagetruecolortopalette($im, false, 256 );
$bg = imagecolorallocate($im, 0, 0, 0);
$trans = imagecolorallocate($im, 255, 255, 255);
if(file_exists('./maps/col_'.$colorset.'.php'))
{
include('./maps/col_'.$colorset.'.php');
}
else
{
$colorset_array['yellow'][] = imagecolorresolve($im,255,255,0);
$colorset_array['yellow'][] = imagecolorresolve($im,255,255,0);
$colorset_array['yellow'][] = imagecolorresolve($im,220,220,0);
$colorset_array['yellow'][] = imagecolorresolve($im,200,200,0);
$colorset_array['yellow'][] = imagecolorresolve($im,150,150,0);
$colorset='yellow';
}
$tmp_col=0;
$tmp_col_aspect=(count($value_array)-1)/(count($colorset_array[$colorset])-1);
ksort($value_array);
reset($value_array);
foreach($value_array as $tmp_value => $tmp)
{
$value_array[$tmp_value]=round($tmp_col/$tmp_col_aspect,0);
$tmp_col++;
}
//print_r($value_array); exit;
imagefill($im, 0, 0, $trans);
imagecolortransparent($im, $trans);
//range array
for($xx_shift=-$range;$xx_shift<$range;$xx_shift++)
$range_y_array[$xx_shift]=round(sqrt(($range*$range)-($xx_shift*$xx_shift)),0);
/*
$triangle=array();
reset($point_array);
foreach($point_array as $point)
{
$longitude=$point['longitude'];
$latitude=$point['latitude'];
$value=$point['value'];
//$col=(count($colorset_array[$colorset])-1)*($sort_place/count($point_array));
$col=$value_array[$value];
$xx=round(($longitude-$region_longitude_min)/($region_longitude_max-$region_longitude_min)*$width);
$yy=round($height-round(($latitude-$region_latitude_min)/($region_latitude_max-$region_latitude_min)*$height));
imagesetpixel($im, $xx, $yy, $colorset_array[$colorset][$col]);
$trianglex[] = $xx;
$triangley[] = $yy;
}
sort($trianglex);
sort($triangley);
reset($trianglex);
foreach($trianglex as $kk => $xx)
{
$yy = $triangley[$kk];
$triangle[] = $xx;
$triangle[] = $yy;
if(count($triangle)==6)
{
imagepolygon($im, $triangle, 3, $colorset_array[$colorset][$col]);
$triangle=array();
}
}
//print_r($myPoints); exit;
//*/
//*
$sort_place=0;
reset($point_array);
foreach($point_array as $point)
{
$longitude=$point['longitude'];
$latitude=$point['latitude'];
$value=$point['value'];
//$col=(count($colorset_array[$colorset])-1)*($sort_place/count($point_array));
$col=$value_array[$value];
$xx=round(($longitude-$region_longitude_min)/($region_longitude_max-$region_longitude_min)*$width,0);
$yy=$height-round(($latitude-$region_latitude_min)/($region_latitude_max-$region_latitude_min)*$height,0);
$max_distance=sqrt(($range*$range)+($range*$range));
//$max_distance=2/$range;
for($xx_shift=-$range;$xx_shift<$range;$xx_shift++)
{
$range_y=$range_y_array[$xx_shift];
for($yy_shift=-$range_y;$yy_shift<$range_y;$yy_shift++)
{
$tmp_distance=sqrt(($xx_shift*$xx_shift)+($yy_shift*$yy_shift));
//$tmp_distance=abs($xx_shift)+abs($yy_shift);
$tmp_col_power=(1-($tmp_distance/$max_distance));
if($square_array[$xx+$xx_shift][$yy+$yy_shift]['power']<$tmp_col_power)
{
if(!isset($square_array[$xx+$xx_shift][$yy+$yy_shift]['col']) or $square_array[$xx+$xx_shift][$yy+$yy_shift]['col']!=$col)
imagesetpixel($im, $xx+$xx_shift, $yy+$yy_shift, $colorset_array[$colorset][$col]);
$square_array[$xx+$xx_shift][$yy+$yy_shift]['col']=$col;
$square_array[$xx+$xx_shift][$yy+$yy_shift]['power']=$tmp_col_power;
}
}
}
$square_array[$xx][$yy]['col']=$col;
}
//*/
imagesavealpha($im, true);
}
}
else
{
if(file_exists('./maps/'.$image.'.php'))
{
include('./maps/'.$image.'.php');
}
else
{
$region_array=array();
$region_alias_array=array();
//region check function
if(!function_exists('region_value_check'))
{
function region_value_check($tmp_region)
{
return $tmp_region;
}
}
}
$im = imagecreatefrompng('./maps/'.$image.'.png');
//$transparent=imagecolorresolve($im,255,0,255);
// imagefill($im, 0, 0, $transparent);
$colorset_array=array();
if(file_exists('./maps/col_'.$colorset.'.php'))
{
include('./maps/col_'.$colorset.'.php');
}
else
{
$colorset_array['yellow'][] = imagecolorresolve($im,255,255,0);
$colorset_array['yellow'][] = imagecolorresolve($im,255,255,0);
$colorset_array['yellow'][] = imagecolorresolve($im,220,220,0);
$colorset_array['yellow'][] = imagecolorresolve($im,200,200,0);
$colorset_array['yellow'][] = imagecolorresolve($im,150,150,0);
$colorset='yellow';
}
$result_array=search($search.' ['.$region_field_name.']');
$agregation_array=array();
reset($result_array);
foreach($result_array as $line)
{
$tmp_region=read($line,$region_field_name);
if($tmp_region!='')
{
if(isset($region_array[$tmp_region]))
$tmp_region=$tmp_region;
elseif(isset($region_array[$region_alias_array[$tmp_region]]))
$tmp_region=$region_alias_array[$tmp_region];
else
$tmp_region=region_value_check($tmp_region);
if(read($line,$value_field_name))
$agregation_array[$tmp_region] += read($line,$value_field_name);
}
}
//print_r($bagaccess_array);
//print_r($agregation_array); echo $search.' ['.$region_field_name.']'; exit;
arsort($agregation_array);
$tmp_color_kk=0;
if(count($agregation_array))
{
$max_colors=count($colorset_array[$colorset])-1;
$tmp_color_max=count($agregation_array)-1;
reset($agregation_array);
foreach($agregation_array as $tmp_region => $tmp_pos)
{
if(isset($region_array[$tmp_region]))
{
//echo $tmp_region.'<br>';
imagefill($im, $region_array[$tmp_region][0], $region_array[$tmp_region][1], $colorset_array[$colorset][floor($max_colors*$tmp_color_kk/$tmp_color_max)]);
$tmp_color_kk++;
}
}
}
//exit;
/*
if(!$tmp_color_kk)
{
reset($region_array);
foreach($region_array as $tmp_region => $tmp_pos)
{
imagefill($im, $tmp_pos[0], $tmp_pos[1], $colorset_array[$colorset][$tmp_pos[2]]);
}
}
*/
//exit;
imagesavealpha($im, true);
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
exit;
?>