going crazy on setting the colors of my zip polygons via perl mapscript
Joseph Norris
sirronj at PACBELL.NET
Mon Nov 29 06:57:20 PST 2004
Sean and everyone who has responded - thank you all but still no color!
Here is what I have:
#!/usr/bin/perl
use URI::Escape;
use mapscript;
use POSIX qw(tmpnam);
use CGI;
use Data::Dumper;
use lib q(/usr/local/cs/lib/);
use CS;
my $cs = new CS( domain => 'mco.communityservers.com' );
my $cfg = $cs->cfg;
my $dbh = $cfg->dbhandle;
my $cgi = new CGI;
print "Content-type: text/html\n\n";
my $action = $cgi->param('action');
my $type = $cgi->param('type');
my $z = $cgi->param('zdir');
my $layers;
if ($action eq 'submit'){
if ($type ne 'none'){
my $sql = qq! select cs_rid, firstname,lastname,zip,lat,lon from u
where type = '$type'!;
my %f = ( cs_rid=>1,firstname=>1,lastname=>1,zip=>1,lat=>1,lon=>1);
my $cmd = $dbh->prepare( $sql );
$cmd->execute;
my $i;
my $rec = $cmd->fetchall_arrayref( {%f} );
foreach my $r (@{$rec}){
my $id = join '~', $r->{cs_rid}, $r->{zip};
$layers->{$id}->{firstname} = $r->{firstname};
$layers->{$id}->{lastname} = $r->{lastname};
$layers->{$id}->{lat} = $r->{lat};
$layers->{$id}->{lon} = $r->{lon};
}
my ($map_file, $gif_file_location, $template_location) =
&build_map($layers);
&run_mapserver($map_file, $gif_file_location, $template_location,
$z);
}
} else {
print <<EOD;
<html>
<head>
<title>MapServer - CS CGI Application</title>
</head>
<frameset cols="20%,80%" frameborder="0" border="0">
<frame src="/maps/teacher/templates/menu2.html" name="query_frame"
marginwidth="5" marginheight="5" scrolling="YES">
<frame src="/maps/teacher/templates/blank.html" name="map_frame"
marginwidth="5" marginheight="5" scrolling="YES">
</frameset>
</html>
EOD
}
exit(0);
sub run_mapserver{
my ($map_file, $gif_file_location, $template_location, $zdir) = @_;
my $url = qq!http://mco.communityservers.com/cgi-bin/mapserv!;
$url .= "?map_web_template=$template_location/map_template.html";
$url .= "&zoomsize=2";
$url .= "&zoomdir=1";
$url .= "&zoom=2";
$url .= "&map=$map_file";
$url .= "&map_web_imagepath=$gif_file_location";
$url .= "&map_web_imageurl=/maps/tmp/";
$url .= "&map_web_template=$template_location/map_template.html";
$url .= "&root=/maps/teacher";
$url .= "&program=/cgi-bin/mapserv";
$url .= "&imgext=5890870.385 -192053.200 8943310.847 3226086.749";
$url .= "&mode=browse";
print qq!<meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=$url"
target="map_frame">!;
}
sub build_map{
my ($point_hash) = @_;
my $template_location =
'/usr/local/apache/htdocs/mco.communityservers.com/htdocs/maps/teacher/templ
ates';
my $gif_location =
'/usr/local/apache/htdocs/mco.communityservers.com/htdocs/maps/tmp/';
my $mapfile =
"/usr/local/apache/htdocs/mco.communityservers.com/htdocs/maps/teacher/zip.m
ap";
my $map = new mapscript::mapObj("$mapfile");
if(!$map){
warn "New mapObj() error: $mapscript::ms_error->{message}\n";
}
my $img = $map->prepareImage();
if(!$img){
warn "prepareImage() error: $mapscript::ms_error->{message}\n";
}
$layerObj = undef;
$layerObj = $map->getLayerByName('calzip');
if(!$layerObj){
warn "layerObj() error: $mapscript::ms_error->{message}\n";
}
my $point = new mapscript::pointObj();
my @point_lookup = ();
my $i = 0;
my %zips;
my $zip_expression;
foreach my $point_id (keys %{$point_hash}){
my ($cs_rid, $zip) = split /~/, $point_id;
next if (!$point_hash->{$point_id}->{lon} &&
!$point_hash->{$point_id}->{lat});
if (! (exists $zips{$zip}) ){
$zips{$zip} = 1;
$zip_expression .= "$zip|";
}
$point->{x} = $point_hash->{$point_id}->{lon};
$point->{y} = $point_hash->{$point_id}->{lat};
my $line = new mapscript::lineObj();
$line->add($point);
my $shp = new mapscript::shapeObj($mapscript::MS_SHAPE_POINT);
$shp->add($line);
$shp->setBounds();
$shp->{text} = "$point_hash->{$point_id}->{firstname}
$point_hash->{$point_id}->{lastname}";
$shp->{index} = $i;
push @point_lookup, $point_id;
$i++;
}
$zip_expression =~ s/\|$//;
my $class = new mapscript::classObj($layerObj);
$class->setExpression("/$zip_expression/");
my $style = new mapscript::styleObj($class);
$style->{color}->setRGB(255,102,102);
# $class->{numstyles} = 1;
# $class->{styles}->{color}->{red} = 255;
# $class->{styles}->{color}->{green} = 102;
# $class->{styles}->{color}->{blue} = 102;
$layerObj->draw($map, $img);
my $image_name = POSIX::tmpnam();
$image_name .= '.gif';
$image_name =~ s/\///;
my $img_loc = $gif_location . $image_name;
# $map->drawLabelCache($img);
$img->save($img_loc, $map);
$img->free();
$image_name = '/maps/' . $image_name;
return ($mapfile, $gif_location, $template_location);
}
Forget the point stuff - I am not even there yet - I just want the color
now - my kingdom for colored polygons!
Here is my map file:
map
name calmap
status on
extent 5890870.385 -192053.200 8943310.847 3226086.749
imagetype gif
size 500 500
units dd
shapepath
"/usr/local/apache/htdocs/mco.communityservers.com/htdocs/maps/teacher/tdata
"
imagecolor 255 255 255
templatepattern "map"
layer
name "calzip"
type polygon
data "cal_zp_st"
status default
classitem "zcta"
class
expression /./
outlinecolor 204 204 204
end
end
end # end map from header
All I need to do - I think - is add my class for selection of zip polygons -
which is what I think that I am doing. I wish that there were more concrete
examples!
Thanks again.
#Joseph Norris (Perl - what else is there?/Linux/CGI/Mysql) print @c=map chr
$_+100,(6,17,15,16,-68,-3,10,11,16,4,1,14,-68,12,1,14,8,-68,4,-3,-1,7,1,14,-
68,-26,11,15,1,12,4,-68,-22,11,14,14,5,15,-90);
-----Original Message-----
From: UMN MapServer Users List [mailto:MAPSERVER-USERS at lists.umn.edu]On
Behalf Of Sean Gillies
Sent: Monday, November 29, 2004 6:43 AM
To: MAPSERVER-USERS at lists.umn.edu
Subject: Re: [UMN_MAPSERVER-USERS] going crazy on setting the colors of my
zip polygons via perl mapscript
Everybody, do yourselves a big favor and upgrade to MapServer 4.2.5
or the 4.4 beta. The 4.0 mapscript had some real useablity problems
and you are only making it worse by referring to ancient 3.5 and 3.6
examples!
This is how you do it (4.2+). Add noise to translate into Perl :)
new_class = mapscript.classObj(layer)
# Add a style since a new class has 0 styles
new_style = mapscript.styleObj(new_class)
# Set the color using setRGB
new_style.color.setRGB(255, 153, 153)
Sean
On Nov 29, 2004, at 6:06 AM, Simpson wrote:
> I think if you dynamically create your class objects you need to
> manually
> keep track of the 'numstyles' property.
> Try setting 'numstyles' to 1 like so:
>
> $class->{numstyles} = 1;
>
> Drew.
>
> -----Original Message-----
> From: UMN MapServer Users List [mailto:MAPSERVER-USERS at LISTS.UMN.EDU]On
> Behalf Of Joseph Norris
> Sent: Sunday, November 28, 2004 6:48 PM
> To: MAPSERVER-USERS at LISTS.UMN.EDU
> Subject: Re: [UMN_MAPSERVER-USERS] going crazy on setting the colors of
> my zip polygons via perl mapscript
>
>
> Come on!!!! Does anyone out there set color with perl/mapscript? I can
> do a
> class->setExpression("/lkasjdfkj/") - at least I think it is working -
> no
> errors however I can not set the color so that my map reflects that the
> regex was found? I just know that someone out there has got it to
> work.
> Please and thanks.
>
>
> #Joseph Norris (Perl - what else is there?/Linux/CGI/Mysql) print
> @c=map chr
> $_+100,(6,17,15,16,-68,-3,10,11,16,4,1,14,-68,12,1,14,8,-68,4,-3,
> -1,7,1,14,-
> 68,-26,11,15,1,12,4,-68,-22,11,14,14,5,15,-90);
>
> -----Original Message-----
> From: UMN MapServer Users List [mailto:MAPSERVER-USERS at lists.umn.edu]On
> Behalf Of Joseph Norris
> Sent: Saturday, November 27, 2004 10:03 PM
> To: MAPSERVER-USERS at lists.umn.edu
> Subject: [UMN_MAPSERVER-USERS] going crazy on setting the colors of my
> zip
> polygons via perl mapscript
>
> Group,
>
> I have found several samples of setting the color of a polygon based
> upon
> class that I come up with this:
>
> $zip_expression =~ s/\|$//;
> my $class = new mapscript::classObj($layerObj);
> $class->setExpression("/$zip_expression/");
> $class->{styles}->{color}->{red} = 255;
> $class->{styles}->{color}->{green} = 153;
> $class->{styles}->{color}->{blue} = 153;
> $layerObj->draw($map, $img);
>
> However - this is not working. I am getting the right zip codes but no
> matter what I do I can not get my mapscript to set the color based
> upon the
> expression. When I was build the map file via perl I could set these
> just
> fine but now I would like to use the perl->mapscript interface to do
> this.
>
> What is wrong with the above snippet?
>
> Thanks.
>
>
> #Joseph Norris (Perl - what else is there?/Linux/CGI/Mysql) print
> @c=map chr
> $_+100,(6,17,15,16,-68,-3,10,11,16,4,1,14,-68,12,1,14,8,-68,4,-3,
> -1,7,1,14,-
> 68,-26,11,15,1,12,4,-68,-22,11,14,14,5,15,-90);
>
>
--
Sean Gillies
sgillies at frii dot com
http://users.frii.com/sgillies
More information about the MapServer-users
mailing list