[mapserver-users] questions re. MapScript

Mr. Puneet Kishor punk.kish at gmail.com
Sat Sep 3 16:48:03 PDT 2011


for you MapScript mavens --


A few things have me puzzled about MapScript. I have the following code, and it works --

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpswd);
my $msh = "host=$dbhost port=$dbport dbname=$dbname user=$dbuser password=$dbpswd";

my $map = new mapscript::mapObj();

my $classOutline = new mapscript::colorObj;
$classOutline->setHex('#444444');

######### Layer One
my $layer_one = new mapscript::layerObj($map);
$layer_one->{name} = 'Layer One';
$layer_one->{status} = $mapscript::MS_ON;
$layer_one->{type} = $mapscript::MS_LAYER_POLYGON;
$layer_one->setConnectionType($mapscript::MS_POSTGIS, '');
$layer_one->{connection} = $msh;
$layer_one->{data} = 'the_geom FROM (SELECT gid, class_name, the_geom FROM table1) foo USING UNIQUE gid';
$layer_one->setFilter('gid NOT IN (.. bunch of gids ..)');
$layer_one->{classitem} = 'class_name';

my $sth = $dbh->prepare("SELECT class_name, class_color FROM color_table");
$sth->execute;
while (my ($class_name, $class_color) = $sth->fetchrow_array) {
	my $classObj = new mapscript::classObj( $layer_one );
	$classObj->{name} = $class_name;
	$classObj->setExpression( $class_name );

	my $styleObj = new mapscript::styleObj($classObj);
	
	my $classFill = new mapscript::colorObj;
	$classFill->setHex($class_color);
	
    $styleObj->{color} = $classFill;
    $styleObj->{outlinecolor} = $classOutline;
}

Question 0: Is there are more efficient way to accomplish the above?

Question 1: I am assuming there is no way I can reuse the same db handle above, 
once for MapServer, and the other time to make my own queries. Neverthless, I 
would like to confirm.

Question 2: I tried to add a WHERE clause in the "data" value, but that caused 
error. I am assuming that is not allowed, but I would like to confirm.

Now, here is a really puzzling thing. If I add another layer, exactly after the 
above pattern, for example, see the code below, MapServer crashes with the 
message "prepareImage() error: Can't call method 'save on an undefined value."
Seems like there is an error somewhere, and $map gets all messsed up. 

######### Layer Two
my $layer_two = new mapscript::layerObj($map);
$layer_two->{name} = 'Layer Two';
$layer_two->{status} = $mapscript::MS_ON;
$layer_two->{type} = $mapscript::MS_LAYER_POLYGON;
$layer_two->setConnectionType($mapscript::MS_POSTGIS, '');
$layer_two->{connection} = $msh;
$layer_two->{data} = 'the_geom FROM (SELECT gid, class_id, the_geom FROM table2) bar USING UNIQUE gid';
$layer_two->{classitem} = 'class_id';

$sth = $dbh->prepare("SELECT class_id, class_color FROM another_color_table");
$sth->execute;
while (my ($class_id, $class_color) = $sth->fetchrow_array) {
	my $classObj = new mapscript::classObj( $layer_two );
	$classObj->{name} = $class_name;
	$classObj->setExpression( $class_name );

	my $styleObj = new mapscript::styleObj($classObj);
	
	my $classFill = new mapscript::colorObj;
	$classFill->setHex($class_color);
	
    $styleObj->{color} = $classFill;
    $styleObj->{outlinecolor} = $classOutline;
}


How do I get MapServer to write out detailed error messages? 

There seems to be an `errorObj` that allows inspection of the MapServer error 
stack. "Only needed for the Perl module as the other language modules expose 
the error stack through exceptions." Alright, but there is no constructor for 
this object. How do I use this?


--
Puneet Kishor 


More information about the MapServer-users mailing list