perl mapscript
MapServer 5.2で地図の上に線を引くことに取り組んでいて、よくわからないことがあった。
だれか他の人がハマってGoolgeで探すかもしれないので、サンプルコードを書いておきます参考までに。
mapfileはこれで
-----------------------------------------------------
MAP
STATUS ON
UNITS DD
IMAGECOLOR 0 0 0
FONTSET fonts.txt
IMAGETYPE GIF
LAYER
NAME "main"
CONNECTIONTYPE POSTGIS
CONNECTION "user=xxx password=xxx dbname=xxx host=xxx"
DATA "geom FROM t_tatemono"
TYPE POLYGON
STATUS ON
CLASS
COLOR 255 255 255
OUTLINECOLOR 0 0 255
SYMBOL 0
END
END
END
mapscriptはこれでうまくいく
-----------------------------------------------------
#!/usr/bin/perl
use mapscript;
use CGI;
use Switch;
use Geo::Proj4;
use Data::Dumper;
my $cgi = CGI->new;
my @params = $cgi->param();
my $mapserv = "/usr/local/mapserver/mapserv";
my $mapfile = "/usr/local/mapserver/mapfile/test.map";
my $w = "500";
my $h = "500";
my $minlat = "35.63";
my $maxlat = "35.65";
my $minlon = "139.72";
my $maxlon = "139.74";
my $map = new mapscript::mapObj($mapfile);
$map->setSize($w, $h);
$map->setExtent($minlon, $minlat, $maxlon, $maxlat);
my $line = new mapscript::lineObj();
my $point = new mapscript::pointObj();
$point->{x} = $minlon;
$point->{y} = $minlat;
$line->add( $point );
my $point = new mapscript::pointObj();
$point->{x} = $maxlon;
$point->{y} = $maxlat;
$line->add( $point );
my $shape = new mapscript::shapeObj($mapscript::MS_SHAPE_LINE);
$shape->{classindex} = 0;
$shape->add($line);
$shape->setBounds();
my $layer = new mapscript::layerObj($map);
$layer->{type} = $mapscript::MS_LAYER_LINE;
$layer->{status} = 1;
$layer->addFeature($shape);
$layer->{name} = "xxx";
my $class = new mapscript::classObj($layer);
$class->{status} = 1;
$class->{name} = "xxx";
$style = new mapscript::styleObj($class);
$style->{symbol} = 0;
$style->{size} = 3;
$style->{maxsize} = 3;
$style->{minsize} = 3;
$style->{color} = new mapscript::colorObj();
$style->{color}->setRGB(255,0,0); #<< if use this line , $img->write() fails.
#$style->{color}->setRGB(0,0,0);
$img = $map->draw();
$img->save("/tmp/tokyo_addline.cgi.gif");
#$img->write(); #<< if use this line , setRGB(255,0,0) cant be used.
open IMAGE, "/tmp/tokyo_addline.cgi.gif";
my ($image, $buff);
while(read IMAGE, $buff, 1024) {
$image .= $buff;
}
close IMAGE;
print "Content-type: image/gif\n\n";
print $image;
ただ、引くラインの色がもし黒なら、ファイルをいちいち保存しなくてもうまくいく
-----------------------------------------------------
#!/usr/bin/perl
use mapscript;
use CGI;
use Switch;
use Geo::Proj4;
use Data::Dumper;
my $cgi = CGI->new;
my @params = $cgi->param();
my $mapserv = "/usr/local/mapserver/mapserv";
my $mapfile = "/usr/local/mapserver/mapfile/test.map";
my $w = "500";
my $h = "500";
my $minlat = "35.63";
my $maxlat = "35.65";
my $minlon = "139.72";
my $maxlon = "139.74";
my $map = new mapscript::mapObj($mapfile);
$map->setSize($w, $h);
$map->setExtent($minlon, $minlat, $maxlon, $maxlat);
my $line = new mapscript::lineObj();
my $point = new mapscript::pointObj();
$point->{x} = $minlon;
$point->{y} = $minlat;
$line->add( $point );
my $point = new mapscript::pointObj();
$point->{x} = $maxlon;
$point->{y} = $maxlat;
$line->add( $point );
my $shape = new mapscript::shapeObj($mapscript::MS_SHAPE_LINE);
$shape->{classindex} = 0;
$shape->add($line);
$shape->setBounds();
my $layer = new mapscript::layerObj($map);
$layer->{type} = $mapscript::MS_LAYER_LINE;
$layer->{status} = 1;
$layer->addFeature($shape);
$layer->{name} = "xxx";
my $class = new mapscript::classObj($layer);
$class->{status} = 1;
$class->{name} = "xxx";
$style = new mapscript::styleObj($class);
$style->{symbol} = 0;
$style->{size} = 3;
$style->{maxsize} = 3;
$style->{minsize} = 3;
$style->{color} = new mapscript::colorObj();
$style->{color}->setRGB(0,0,0); #<<ここ
$img = $map->draw();
print "Content-type: image/gif\n\n";
$img->write(); #<<ここ
数時間はかかってしまった。
コメント