[mapserver-users] layer classification with MapScript

Mr. Puneet Kishor punk.kish at gmail.com
Sun Aug 28 15:32:49 EDT 2011


On Aug 28, 2011, at 1:27 PM, Mr. Puneet Kishor wrote:

> 
> On Aug 28, 2011, at 8:11 AM, Cristiano Sumariva wrote:
> 
>> You can do database stuff using your programming language to access the
>> database.
>> 
>> To create classes there is the msNewClassObj function to append a class to
>> an layer reference.
>> You can get function details in documentation page at
>> http://mapserver.org/mapscript/php/index.html#classes.
>> 
>> If you want walk over records from the data statement declared on the data
>> field, maybe using the getShape(  ), getNumResults() method on your layer
>> reference may do what you want. But I do not have practice using it.
>> 
>> for ( loop = 0; loop < layer->getNumResults(); loop + 1 )
>> dbRowFromDataField = layer->getResult( loop );
>> class = msnewclass( layer );
>> class->set( name, dbRowFromDataField.field );
>> ...
> 
> 
> The above seems logical, but I think it might have to do with the results from a "click" query rather than the data retrieved by MS from the data source in the first place. In any case, the following doesn't do anything at all (empty, white image) --
> 
>    for ( 0 .. $layerObj->getNumResults() - 1) {
>        my $row = $layerObj->getResult( $_ );
>        my $classObj = new mapscript::classObj( $layerObj );
>        $classObj->{name} = $row->{interval_name};
>        my $styleObj = new map script::styleObj( $classObj );
> 	set style color based on $row->{class_color};
>    }
> 
>> 
>> 


Heh, the above code was ridiculous on my part... it would try to create a new class for every single feature. Instead, the following works just fine


    my $dbh = DBI->connect( .. );
    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( $layerObj );
        $classObj->{name} = $class_name;
        $classObj->setExpression( $class_name );
        my $styleObj = styleObj(classObj => $classObj, colorObj => colorObj('hex' => $class_color));    
    }


I get all the classes made correctly without having to code them individually.

The only minor downside, I have to make two different db calls -- one for MapServer, to get all the features, and one for the color information.

Now, on to the next step -- retrieve these color values via a REST API, which is really my original aim.



>> 2011/8/27 Mr. Puneet Kishor <punk.kish at gmail.com>
>> 
>>> I have a layer classification issue. I want to do something along the lines
>>> of (semi-pseudocode ahead) --
>>> 
>>>  $layerObj->{data} = 'the_geom FROM (SELECT gid, the_geom, class_name,
>>> class_color FROM table) t USING UNIQUE gid';
>>>  $layerObj->{classitem} = 'class_name';
>>> 
>>>  while (my $row in $layerObj->{data}) {
>>>      create class with 'name = class_name', 'expression = class_name',
>>> 'color = class_color'
>>>  }
>>> 
>>> How do I iterate over the rows retrieved from the table in order to create
>>> classes? I don't see any obvious reference to a db result
>>> set._______________________________________________
>>> maps


More information about the mapserver-users mailing list