[Mapserver-users] Re: Clarifications about mapscript 4.2
Eric Bridger
eric at gomoos.org
Wed May 12 13:27:59 PDT 2004
--=-gyxgLlHGdin/Zl8z8RxS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hi Sean,
I've been having trouble with styles and colors in Mapscript4.2b2. I've
attached a small perl script and map file which demonstrates the
problem.
Loop thru some points, get the one class and style defined in the map
file and change various attributes: size, symbol, color. I have no
problem changing the class symbol or symbol size. But the color seems
strangely "sticky". Whichever color is set first using
$style->{color}->setRGB() remains in force. The color DOES change from
the color defined in the mapfile but then will not change again.
I've also found that (this is a separate problem)
my $red_color = $mapscript::colorObj(255,0,0);
$style->{color} = $red_color;
does not work as expected. The color is always black in this case.
Am I missing something? I will next attempt a completely dynamic
approach, new class, new sytle, etc. and see if that works better.
Thanks,
Eric
On Fri, 2004-05-07 at 13:21, Sean Gillies wrote:
> Hi Eric,
>
> Was just looking at your Wiki pages and have some clarifications
> for you.
>
> 1) STYLES: Most users will want to dynamically add new styles to a
> class by
> this syntax:
>
> $class = $layer.getClass(0);
> $new_style = mapscript::styleObj($class);
>
> insertStyle() is for more specialized cases where you really want to
> change
> the ordering of styles or pass a fully-defined style around in your app.
>
> 2) SYMBOLS: I'd like to recommend that symbols be accessed through
> methods of symbolSetObj class rather than through methods of mapObj.
> For example:
>
> $foo_index = $map->{symbolset}->index("foo");
>
> is, IMO, the better way to go than
>
> $foo_index = $map->getSymbolByName("foo");
>
> The later is a poorly named function because it returns an integer, not
> a
> symbol, compared to mapObj::getLayerByName which *does* return a layer.
> We're going to leave this in mapscript for backwards compatibility, but
> the future direction is to make mapObj, layerObj, classObj,
> symbolSetObj,
> etc more container-like with index() methods that return the index of
> a "child" object given the child's name, and that getFoo() methods
> uniformly return instances of Foo, not a mix of instances and indexes.
>
> 3) You might want to point folks to the development documentation which
> is
> committed in CVS under mapserver/mapscript/doc/mapscript.txt and to the
> HTML document that I have been generating and keeping online at
>
> http://users.frii.com/sgillies/projects/mapscript/
>
> cheers,
> Sean
>
>
> --
> Sean Gillies
> sgillies at frii dot com
> http://users.frii.com/sgillies
--=-gyxgLlHGdin/Zl8z8RxS
Content-Disposition: attachment; filename=test_color.cgi
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-perl; name=test_color.cgi; charset=ISO-8859-15
#!/usr/bin/perl
use strict;
# Mapscript 4.2 Beta2
use mapscript42;
use CGI ":cgi";
my $q =3D new CGI;
# A hash of points. hash keys are values used in addFeature() and returned =
by queryByPoint().
my %points =3D (
1 =3D> { 'longitude' =3D> -67.0173,
'latitude' =3D> 44.8911,
'size' =3D> 15,
},
2 =3D> { 'longitude' =3D> -66.0146,
'latitude' =3D> 45.2045,
'size' =3D> 8,
},
3 =3D> { 'longitude' =3D> -68.3578,
'latitude' =3D> 43.7148,
'size' =3D> 12,
},
4 =3D> { 'longitude' =3D> -66.5528,
'latitude' =3D> 43.6243,
'size' =3D> 18,
},
5 =3D> { 'longitude' =3D> -68.9983,
'latitude' =3D> 44.0555,
'size' =3D> 24,
},
);
# In tmp/ subdir
my $image_name =3D sprintf("tmp/%0.10d",rand(1000000000)) . ".png";
my $map =3D new mapscript42::mapObj("test_color.map");
my $circle_idx =3D $map->{symbolset}->index("circle");
my $plus_idx =3D $map->{symbolset}->index("plus");
if(!$map){
warn "New mapObj() error: $mapscript42::ms_error->{message}\n";
}
my $img =3D $map->prepareImage();
if(!$img){
warn "prepareImage() error: $mapscript42::ms_error->{message}\n";
}
my $layerObj =3D $map->getLayerByName('points');
foreach my $point_id (sort keys %points){
my $point =3D new mapscript42::pointObj();
$point->{x} =3D $points{$point_id}{longitude};
$point->{y} =3D $points{$point_id}{latitude};
my $class =3D $layerObj->getClass(0);
unless($class){ warn "No class\n";}
my $style =3D $class->getStyle(0);
unless($style){ warn "No style\n";}
# set class symbol size from database
$style->{size} =3D $points{$point_id}{size};
# STYLE COLORS
if($point_id =3D=3D 1){ # sample test case, the first point
$style->{symbol} =3D $plus_idx;
# color objects can't be assigned at all. symbols are black
#my $red =3D new mapscript42::colorObj(255,0,0);
#$style->{color} =3D $red;
# This first color "sticks", determining all point colors. But {symbol} a=
nd {size} don't
# outline colors stick also.
$style->{color}->setRGB( 255,0,0 );
# $style->{color}->setRGB( 0,255,0 );
}else{
$style->{symbol} =3D $circle_idx;
# This color has no affect after above setRGB()
# $style->{color}->setRGB( 255,0,0 );
$style->{color}->setRGB( 0,255,0 );
}
#my $color =3D $style->{color};
#warn "$point_id r:$color->{red} g:$color->{green} b:$color->{blue}\n";
$class->{label}->{color}->setRGB(0,0,0);
$point->draw($map, $layerObj, $img, undef, "$point_id");
#undef $style;
#undef $class;
}
$map->drawLabelCache($img);
$img->save($image_name);
# Output an HTML form to show image
print $q->header();
print $q->start_html(-title=3D>'MapServer4.2b2 - Dynamic Points', -bgcolor=
=3D>"#ffffff");
print "<form name=3D\"pointmap\" action=3D\"test_color.cgi\" method=3D\"GET=
\">\n";
print "<table border=3D\"1\" cellpadding=3D\"5\" cellspacing=3D\"2\">\n";
print "<tr>\n";
print "<td>\n";
print "<input border=3D\"2\" type=3D\"image\" name=3D\"img\" src=3D\"$image=
_name\">\n";
print "</td>\n";
print "</tr>\n";
print "</table>\n";
print "</form>\n";
print "<p>\n";
print $q->end_html();
exit;
--=-gyxgLlHGdin/Zl8z8RxS
Content-Disposition: attachment; filename=test_color.map
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=test_color.map; charset=ISO-8859-15
MAP
NAME "points42"
STATUS ON
EXTENT -71.5 39.5 -63.0 46.0
SIZE 504 385
IMAGETYPE PNG
UNITS DD
PROJECTION
"proj=3Dlatlong"
END
OUTPUTFORMAT
NAME PNG
DRIVER "GD/PNG"
MIMETYPE "image/png"
# 24bit
IMAGEMODE RGB
EXTENSION "png"
END
SYMBOL
TYPE ELLIPSE
NAME "circle"
POINTS 1 1 END
FILLED TRUE
END
SYMBOL
TYPE VECTOR
NAME "plus"
POINTS .5 0 .5 1 -99 -99 0 .5 1 .5 END
END=20
LAYER
NAME "points"
DEBUG ON
TYPE POINT
STATUS ON
TOLERANCE 10
TEMPLATE "bogus.html"
CLASS
NAME "point"
STYLE
SYMBOL "circle"
SIZE 0=20
COLOR 0 255 0
OUTLINECOLOR 0 0 0
END
LABEL
COLOR 255 0 0
TYPE BITMAP
SIZE MEDIUM
POSITION AUTO
END
END
PROJECTION
"proj=3Dlatlong"
END
END
END
--=-gyxgLlHGdin/Zl8z8RxS--
More information about the MapServer-users
mailing list