classifying with an external datasource

Sandeep sandeep.bashyal at GMAIL.COM
Fri Feb 25 06:49:20 EST 2005


On Fri, 25 Feb 2005 02:30:39 -0600, Richard Jansen <richardjansen at TMOP.NL>
wrote:

> Hi List,
>
> I want to classify and color a poly shape. The values for classifying are
> stored in a SQL Server database. When I read the List I understand that
> this is not possible. I'm trying to join a template dbf file with the
> needed values. I also tried to add new field to the dbf, that's not
> simple.
> I'm using PHP/Mapscript. Is there a way to do this?
>
> Thanks for your comments,
> Richard Jansen

Richard,

If you don't have a very large number of polygons to classify, here's a
snip from one of my earlier posts:

... you can generate classes and expressions dynamically with mapscript.
Here is the php mapscript code I use to classify districts into
"$no_classes" number of classes (using arcview-style quantile method)
using data from an external mysql database (the external database has a
field called DIST_ID that associates with the shapefile):

//run sql query
$sql = "SELECT DIST_ID, DIST_NAME, ".$databasefield." FROM
DISTRICTS_".$databasetable.$filter_where;
$result = mysql_query($sql, $connection);

// assign sql results into arrays
while ($row = mysql_fetch_assoc($result)){
        $ID[]=$row["DIST_ID"];
        $DIST_NAME[]=$row["DIST_NAME"];
        ${$databasefield}[]=$row[$databasefield];
} //end while

//sort the query results
if (${$databasefield}){array_multisort(${$databasefield}, SORT_ASC,
SORT_NUMERIC, $DIST_NAME, SORT_STRING, $ID, SORT_STRING);}
mysql_free_result($result);
mysql_close($connection);

//count the number of records in the array
$no_records=count($ID);
if ($no_classes > $no_records){$no_classes = $no_records;}
if ($no_classes > 0){
        //number of class items
        $no_classitems = ceil($no_records / $no_classes);
        //split the arrays into chunks
        $class_ID=array_chunk($ID,$no_classitems,true);
        $class_value=array_chunk(${$databasefield},$no_classitems,true);
} // end if $no_classes > 0

//set classitem
$layer_districts->set("classitem","OBJID");

//draw classes
$i=0;
while ($i < $no_classes){
        $i++;
        //class expressions
        $class_array = $class_ID[$i-1];
        if ($class_array){
                $expression[$i]="/".implode("|",$class_array)."/";
                //legend key labels
                $class_legend_key1=$class_value[$i-1][($i-1)*$no_classitems];
                $class_legend_key2=$class_value[$i-1][($i-1)*$no_classitems
+ $no_classitems -1];
                if ($class_legend_key2 == NULL){$class_legend_key2 =
$class_value[$i-1][$no_records-1];}
                $class_legend_key[$i]=$class_legend_key1." to ".$class_legend_key2;
                //draw layers
                $class[$i] = ms_newClassObj($layer_districts);
                $class[$i]->set("name",$class_legend_key[$i]);
                $class[$i]->setexpression($expression[$i]);
                $class_style[$i] = ms_newStyleObj ($class[$i]);
                $class_style[$i]->outlinecolor->setRGB(-1,-1,-1);
                $class_style[$i]->color->setRGB($color_r[$i-1],$color_g[$i-1],$color_b[$i-1]);
        } // end if $class_array
}//end while

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



More information about the mapserver-users mailing list