Dynamic point labeling
Jim Bowlin
bowlin at sirius.com
Wed Jul 26 00:53:36 PDT 2000
"Paul G. Allen" wrote:
>
> My application plots points on a map from data retrieved from a DB2
> database. No problem here, I can plot points 'til my heart's content.
> How do I label these points from data retreived from the same
> database? The mapfile layer definitions from my current mapfile for
> the points and labels are below. Note that the "DATA field" entry in
> the LAYER definitions is now meaningless as I am no longer using this
> to define the points and labels. I am still using MapServer v. 3.3.001
> (or whatever the first MapScript version was :)
Here is some "stripped down" Perl code I used to plot either plain points
or labeled points depending on the number of points on the map.
~~~~~~~~~~~~~~~~~~~~~~~
my $map = new mapObj($mapfile) or
cgi_err($mapscript::ms_error->{code}.": ".$mapscript::ms_error->{message});
my $img = $map->prepareImage() or
cgi_err ("Unable to prepareImage $mapscript::ms_error->{message}");
for my $i (0 .. ($map->{numlayers}-1)) {
my $layer = $map->getLayer($i);
$layer->draw($map, $img); # if $layer->{status};
}
my $array_refref = $db->selectall_arrayref($sql) || [];
my $count = @$array_refref;
my $class = "class1";
if ($use_labels && $count <= 25) {
my $labs = "labels";
my $layer = $map->getLayerByName($labs) or
cgi_err("Unable to getLayerByName($labs) $mapscript::ms_error->{message}");
for my $row (@$array_refref) {
my $point = new pointObj();
$point->{x} = $row->[1];
$point->{y} = $row->[0];
my $label = $row->[2];
$point->draw($map, $layer, $img, $class, $label);
}
$layer->draw($map, $img);
}
else {
my $labs = "no_labels";
my $layer = $map->getLayerByName($labs) or
cgi_err("Unable to getLayerByName($labs) $mapscript::ms_error->{message}");
for my $row (@$array_refref) {
my $point = new pointObj();
$point->{x} = $row->[1];
$point->{y} = $row->[0];
$point->draw($map, $layer, $img, $class, undef);
}
$layer->draw($map, $img);
}
$map->drawLabelCache($img);
mapscript::msSaveImage($img, $full_file, 0, 0);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Below are the relevant layers from the .map file. The key difference
is the "TYPE POINT" versus the "TYPE ANNOTATION". I don't claim to
understand all of this. I just sort of fusted with it until it did
what I wanted. The $db thingy is a DBI object. The SQL statement
selected on lat/lon and other criteria and returned three fields per
row: lat, lon and the text for the label. I was writing the image to
a file for server-side caching but mostly because this was running
under FastCGI (which means the process was persistent) and I think
there was a bug somewhere that was closing STDOUT if I let mapscript
write directly to STDOUT.
HTH -- Jim Bowlin
LAYER
NAME no_labels
TYPE POINT
STATUS DEFAULT
PROJECTION
geographic
END
MAXSCALE 100000000
CLASS
NAME "class1"
SYMBOL 16
COLOR 0 0 0
LABEL
TYPE TRUETYPE
ANTIALIAS
FONT arial-bold
BUFFER 4
SIZE 8
POSITION AUTO
COLOR 0 0 0
BACKGROUNDCOLOR 255 255 204
END
END
END
LAYER
NAME labels
TYPE ANNOTATION
STATUS DEFAULT
PROJECTION
geographic
END
MAXSCALE 100000000
CLASS
NAME "class1"
SYMBOL 16 # red ball
COLOR 0 0 0
LABEL
TYPE TRUETYPE
ANTIALIAS
FONT arial-bold
BUFFER 4
SIZE 8
POSITION AUTO
COLOR 220 0 0
BACKGROUNDCOLOR 255 255 204
END
END
END
More information about the MapServer-users
mailing list