[mapserver-users] questions re. MapScript

Mr. Puneet Kishor punk.kish at gmail.com
Sat Sep 3 20:21:40 EDT 2011


Thanks Cristiano, for your replies. The most puzzling and show-stopping for me is that as soon as I add another layer, MapServer crashes. See the very end of my original post below.

On Sep 3, 2011, at 7:12 PM, Cristiano Sumariva wrote:

> Question 0: well I do not think it is too much overhead to set some
> variables in memory for the mapserver objects.
> But you can do all you need at the file with extension .map and only update
> what you need in the programming language.
> So you can call
> new mapObject = mapscript::map( 'path/file.map' );
> // load all your parameters that not change ( or do not change often ) //
> // update only the snippets you need //
> // need to set up first layer data to a sql assembled at runtime //
> // so only grab the reference to the layer you want set //
> layer = mapObject->getLayer( index /* start on zero */ )
> // and update only that property //
> layer->set( 'data', 'the_geom FROM ( SELECT att FROM table WHERE att OP
> 'value' ) AS  TableFromWhere USING UNIQUE att USING SRID  ).
> 
> You maybe doing something like a did in past. Creating all mapfile
> configuration at application using forms and storing it inside database
> schema.
> That is not much hard to create an manager to do that. You need a couple of
> tables on some schema/db and create some checks for mapserver enumerations.
> The most benefits of this strategy is to have a system that will be error
> free on mapfile syntax and have a very fast mapserver configuration for
> users that want create
> map collections from a shared pool of features.
> 
> Question 2 is on the sample above. You need to wrap the where at a sub query
> so mapserver can see it like a one entire new table.
> 
> Question 1: I not much sure but I guess you can not share database
> descriptors. If you having low speed due to database stuff you can try use a
> persistent
> connection for your script so you only open one for mapserver. I guess that
> mapserver can also use a shared database connection for him but do not
> remember a sample case for it.
> 
> Hope you enjoying your map rendering engine.
> 
> Most times low speed is due to databases not properly indexed.
> 
> 2011/9/3 Mr. Puneet Kishor <punk.kish at gmail.com>
> 
>> 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 _______________________________________________
>> mapserver-users mailing list
>> mapserver-users at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/mapserver-users
>> 



More information about the mapserver-users mailing list