MapScript labelling problem
Sean O'Hagan
seano at metavera.com
Fri Jun 9 14:29:03 PDT 2000
Hello list,
I'm not able to get a label for an in-line feature to render using
mapscript. When I used the /cgi-bin/mapserv? method with feature_new,
feature_text, etc., etc. it works fine. When I tried to mimic this using
mapscript, I don't get any labels. I've tried a couple of things with
$map->drawLabelCache but to no avail. Here is my map file and perl file:
*** Map File ***
NAME mv
STATUS ON
SIZE 400 400
SHADESET symbols/shade.sym
MARKERSET symbols/marker.sym
LINESET symbols/line.sym
FONTSET font.list
EXTENT -79.4477 43.6279 -79.3413 43.7254
UNITS METERS
SHAPEPATH d:/shapedata
IMAGECOLOR 224 224 224
LAYER
NAME streets
TYPE line
STATUS default
DATA streetdata
CLASS
SYMBOL 0
OUTLINECOLOR 0 0 0
COLOR 49 49 185
END
END
LAYER
NAME places
TYPE point
STATUS default
CLASS
SYMBOL 7
SIZE 12
OUTLINECOLOR 0 0 0
COLOR 185 49 49
LABEL
BACKGROUNDCOLOR 253 253 253
COLOR 0 0 0
FONT arial
PARTIALS true
POSITION auto
TYPE truetype
SIZE 6
END
END
END
END
*** Perl Script: Search for mabObj to get to the MapScript part ***
#!perl
print "Content-Type: text/html\n\n";
print "<html><body>\n";
use mapscript;
use DBI;
use XBase;
read STDIN, $gcvars, $ENV{CONTENT_LENGTH};
@gcvarsarray = split /&/,$gcvars;
foreach $gcvar (@gcvarsarray) {
($var,$val) = split /=/,$gcvar;
$vars{$var} = $val;
}
($gcnumber=$vars{'gcnumber'})=~tr/[A-Za-z]+/[] /;
($gcstreet=$vars{'gcstreet'})=~tr/[A-Za-z]+/[A-ZA-Z] /;
($gctype=$vars{'gctype'})=~tr/[A-Za-z]+/[A-ZA-Z] /;
($gcdir=$vars{'gcdir'})=~tr/[A-Za-z]+/[A-ZA-Z] /;
($gccity=$vars{'gccity'})=~tr/[A-Za-z]+/[A-ZA-Z] /;
$gcname=$vars{'gcname'};
$gcmode=$vars{'gcmode'};
if ($gcname eq "") {
$gcname = "Here";
}
my $x;
my $y;
my $from;
my $to;
my $shapefile = new shapefileObj("streetdata.shp",-1);
my $table = new XBase "streetdata.dbf";
my $cur = $table->prepare_select_with_index("streetdata.ndx", "STREETNAME",
"FROMLEFT", "TOLEFT", "FROMRIGHT", "TORIGHT", "STREETTYPE", "LEFT_CITY",
"SUFDIR");
my @data;
my $shape = new shapeObj(MS_LINE);
my $streetname;
$xmin;
$ymin;
$xmax;
$ymax;
if ($gcmode eq 'map') {
$xmin = -79.4477;
$ymin = 43.6279;
$xmax = -79.3413;
$ymax = 43.7254;
}
$cur->find_eq($gcstreet);
while (@data = $cur->fetch()) {
last if ($data[0] !~ /^$gcstreet/);
$streetname = $data[0];
if (($data[0] eq $gcstreet) && ($data[6] eq $gccity) &&
($data[5] eq $gctype) && (!$gcdir || ($data[7] eq $gcdir))) {
if ((0 eq ($data[1] % 2)) && (0 eq ($data[2] % 2))) {
if (0 == ($gcnumber % 2)) {
$from = $data[1];
$to = $data[2];
} else {
$from = $data[3];
$to = $data[4];
}
} else {
if (0 eq ($gcnumber % 2)) {
$from = $data[3];
$to = $data[4];
} else {
$from = $data[1];
$to = $data[2];
}
}
if (($gcnumber < $from) || ($gcnumber > $to)) {
next;
}
my $ratio = 0.5;
if (0 != ($to-$from)) {
$ratio = ($gcnumber-$from)/($to-$from);
}
my $recnum = $cur->last_fetched-1;
$shapefile->get($recnum, $shape);
$numlines = $shape->{numlines};
$line = new lineObj;
$line = $shape->get(0);
$numpoints = $line->{numpoints};
my $pointfirst = new pointObj();
$pointfirst = $line->get(0);
my $pointnext = new pointObj();
my $distance = 0;
for (my $i=1; $i<$numpoints; $i++) {
$pointnext = $line->get($i);
my $deltax = $pointnext->{x}-$pointfirst->{x};
my $deltay = $pointnext->{y}-$pointfirst->{y};
$distance += sqrt(($deltax * $deltax) + ($deltay * $deltay));
$pointfirst = $pointnext;
}
my $sofar = 0;
my $stopdistance = $ratio * $distance;
my $partdistance;
$pointfirst = $line->get(0);
for (my $i=1; $i<$numpoints; $i++) {
$pointnext = $line->get($i);
my $deltax = $pointnext->{x}-$pointfirst->{x};
my $deltay = $pointnext->{y}-$pointfirst->{y};
$partdistance = sqrt(($deltax * $deltax) + ($deltay * $deltay));
$sofar += $partdistance;
last if ($sofar > $stopdistance);
$pointfirst = $pointnext;
}
my $fullpartdistance = $sofar - $partdistance;
my $remainingdistance = $stopdistance - $fullpartdistance;
my $partratio = $remainingdistance/$partdistance;
my $minx = $pointfirst->{x};
my $miny = $pointfirst->{y};
my $maxx = $pointnext->{x};
my $maxy = $pointnext->{y};
$x = $minx + $partratio * ($maxx - $minx);
$y = $miny + $partratio * ($maxy - $miny);
if ($gcmode eq 'centre') {
$xmin = $x - .01;
$ymin = $y - .01;
$xmax = $x + .01;
$ymax = $y + .01;
}
}
}
$map = new mapObj('plot.map') or die('Unable to open map file');
$map->{extent}->{minx} = $xmin or die('Unable to set minx of extent');
$map->{extent}->{miny} = $ymin or die('Unable to set miny of extent');
$map->{extent}->{maxx} = $xmax or die('Unable to set maxx of extent');
$map->{extent}->{maxy} = $ymax or die('Unable to set maxy of extent');
$img = $map->prepareImage();
$layer = $map->getLayerByName('places') or die('Unable to get layer');
$mainlayer = $map->getLayerByName('streets') or die('Unable to get layer');
$point = new pointObj() or die('Unable to create point');
$point->{x} = $x or die('Unable to add x value to point');
$point->{y} = $y or die('Unable to add y value to point');
$line = new lineObj() or die('Unable to create line');
$line->add($point);
$feature = new featureObj($layer) or die('Unable to create feature');
$feature->add($line);
$feature->{text} = $gcname or die('Unable to add text to feature');
$mainlayer->draw($map,$img);
$layer->draw($map,$img);
$map->drawLabelCache($img);
mapscript::msSaveImage($img,'plot.gif',$map->{interlace},$map->{transparent}
);
print "<img src=plot.gif>\n";
print "</body></html>\n";
Thanks a million,
Sean O'Hagan
Metavera Solutions, Inc.
192 Spadina Ave., Suite 405
Toronto, ON
M5T 2C2
Tel: (416) 504-8351
Fax: (416) 364-2636
Email: seano at metavera.com
More information about the MapServer-users
mailing list