changing the color of polygons in a layer???(forgot the link...)

Matthew Roberson matt at MSILEADS.COM
Fri Jul 8 08:23:43 PDT 2005


Hi,

I am having a problem getting some PHP/Mapscript to work like I want  
it to and I hope that somebody can offer some help.

Here is what I am trying to do.

I have a shp file from the US Census Bureau that contains 5-digit zip  
code boundaries for a particular state(Texas in this case).  I do a  
query to find the zip codes i'm interested in.  I want some of the  
zipcode boundaries to be red(or whatever color) and some to be blue  
(or whatever color).  I have written the PHP/Mapscript that I think  
will produce the results I want, however what
I get is all yellow zip codes(see the link below).  I have included  
the php and .map file that generates this map.  I would really  
appreciate any suggestions.

Thanks,

Matt

http://machine.msileads.com/TXZips.html

-------------------------------------------------   php file  
---------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<TITLE>2080 Machine</TITLE>
<meta http-equiv = "Content-Language" content="en-us">
<meta http-equiv = "Content-Type" content="text/html;  
charset=iso-8859-1">
<style type="text/css">
<!--
.red {color: red; font-size: 10pt;}
.black {color: black; font-size: 10pt;}
.bold {color: black; font-size: 12pt;font-weight:bold;}
-->
</style>
</head>
<body>
<table width="300" align="center" bgcolor="#FFFFFF">
     <tr>
         <td><img src="http://localhost/JW8020/MSILOGO1.gif" alt=""  
width="120" height="112"></td>
     </tr>
</table>
<?php
//require('/Library/WebServer/Documents/fpdf.php');
//require('/Library/WebServer/Documents/create8020PDF.php');
ini_set("display_errors","1");
//$username = "sa";
$username = "admin";
//$password = "s1q2l3S1";
$password = "w3xp2BA5";
$hostName = "localhost";
$databaseName = "8020Machine";
$sQVal = "";

$jID8020 = 3;

function get8020CountPercent($val,
                              $iTotalVal)
{
       if($iTotalVal>0)
        {
            $dPercent=($val/$iTotalVal)*100;
            $dPercent = number_format($dPercent, 2, '.', '');
        }// ($iTotalVal>0)
        else
        {
            $dPercent = 0.0;
            //print "{$iTotalVal} is iTotalVal<br>";
        }// end if ($iTotalVal>0)

        return $dPercent;
}

function create8020OutputRecord($query,
                                 $zip_array,
                                 $adPercents)
{

      // Run the query on the server
      if (!($result = @ mysql_query ($query)))

      if ($result)
      {
          $jobID = mysql_insert_id();
      }

      //return $jobID;

} // end of function

mysql_connect ($hostName,$username,$password);
mysql_select_db($databaseName);


$address_query = mysql_query("select distinct ZipCode from  
eightytwentyinput where ZipCode <> '' and ZipCode is not NULL");
$address_rows = mysql_num_rows($address_query);

for($i=0; $i<$address_rows; $i++)
{
     $address_array = mysql_fetch_array($address_query);
     $temp_zip = $address_array['ZipCode'];
     $zip_array[$temp_zip] =0;
     $adPercents[$temp_zip]=0;
        $data[$i][0]=0;
        $data[$i][1]=0;
        $data[$i][2]=0;
}

$address_query2 = mysql_query("select ZipCode from eightytwentyinput  
where ZipCode <> '' and ZipCode is not NULL");
$address_rows2 = mysql_num_rows($address_query2);


for($i=0;$i<$address_rows2;$i++)
{
     $address_array2 = mysql_fetch_array($address_query2);
     $temp_zip2 = $address_array2['ZipCode'];
     $zip_array[$temp_zip2] += 1;


}

arsort($zip_array);
arsort($adPercents);

//mapserver stuff
dl("php_mapscript.so");

$map_file="/Library/WebServer/Documents/test/TX1.map";
$map = ms_newMapObj($map_file);

$oZipLayer = $map->getLayerByName("TX zip code boundaries");

$sAttrName = 'ZCTA';

//print_r($zip_array);
print("There are <strong>$address_rows</strong> total unique zip  
codes<BR>");

$iTotalVal = array_sum($zip_array);
$percent_num = $address_rows*.20;
$i=0;
print("<table border=\"1\"><tr><td class=\"bold\">Zip Code</td><td  
class=\"bold\">Count</td><td class=\"bold\">Percent</td></tr>");

while (list($key, $val) = each($zip_array))
{

    $i++;

    if($i <= $percent_num)
    {
        $dPercent = get8020CountPercent($val,$iTotalVal);
        $qry8020Output = mysql_query("INSERT INTO EightyTwentyOutput  
VALUES (NULL,'{$jID8020}','{$key}','{$val}','{$dPercent}')");
        create8020OutputRecord($qry8020Output, $zip_array, $adPercents);
        echo "<tr><td class=\"red\">$key</td><td class=\"red\">$val</ 
td><td class=\"red\">$dPercent</td></tr>";
        //pdf stuff
        $data[$i][0]=$key;
        $data[$i][1]=$val;
        $data[$i][2]=$dPercent;
        //mapserver stuff
        if(strlen($key)<=5)
        {
            if(strncmp($key, "77", 2)== 0)
            {
                $oZipLayer->set("labelitem", 'ZCTA');
                $sQVal=$sQVal.$key."|";
                $oZipClassRed = ms_newClassObj($oZipLayer);
                $oZipClassRed->setexpression($key);
                $oZipStyleRed = ms_newStyleObj($oZipClassRed);
                $oZipStyleRed->color->setRGB(250,0,0);

            }
        }
    } // ($i <= $percent_num)
    else
    {
        $dPercent = get8020CountPercent($val,$iTotalVal);
        $qry8020Output = mysql_query("INSERT INTO EightyTwentyOutput  
VALUES (NULL,'{$jID8020}','{$key}','{$val}','{$dPercent}')");
        create8020OutputRecord($qry8020Output, $zip_array, $adPercents);
        echo "<tr><td class=\"black\">$key</td><td class=\"black\"> 
$val</td><td class=\"black\">$dPercent</td></tr>";
        //pdf stuff
        $data[$i][0]=$key;
        $data[$i][1]=$val;
        $data[$i][2]=$dPercent;
        //mapserver stuff
        if(strlen($key)==5)
        {
            if(strncmp($key, "77", 2)== 0)
            {
                $oZipLayer->set("labelitem", 'ZCTA');
                $sQVal=$sQVal.$key."|";
                $oZipClassBlue = ms_newClassObj($oZipLayer);
                $oZipClassBlue->setexpression($key);
                $oZipStyleBlue = ms_newStyleObj($oZipClassBlue);
                $oZipStyleBlue->color->setRGB(0,0,255);
            }
        }
    }// end if ($i <= $percent_num)
    $adPercents[$key] = $dPercent;
}// end while

print("</table>");



//more mapserver stuff
$sQVal = substr($sQVal, 0, strlen($sQVal)-1);

//Do query
$qresult = @$oZipLayer->queryByAttributes($sAttrName,"/". 
$sQVal."/",MS_MULTIPLE);


$image = $map->drawQuery();
$image_url = $image->saveWebImage();
$error = ms_GetErrorObj();
while($error && $error->code != MS_NOERR)
{
     printf("Error in %s: %s \n", $error->routine, $error->messaage);
     $error = $error->next();
}
?>

<img SRC="<?php echo $image_url?>">
<p>This is an unprojected map of the 5 digit zip code boundries for  
the state of Texas,
from the <a href="http://www.census.gov/geo/www/cob/z52000.html"> US  
Census Bureau</a>.</p>
<p>To find the categories for the legend, I used the <code>ogrinfo</ 
code> utility twice.
<code>ogrinfo -a -summary zt48_d00.shp</code> let me see what fields  
were available, and
<code>ogrinfo -a zt48_d00.shp|grep SYMBOL|sort -u</code> told me what  
all of the unique values were so that
I could build my <code>CLASS</code> definitions in <code>TX1.map</code>.
</p>

<?php
//print_r($oZipLayer);
print_r($qresult);
if ($oZipLayer->getNumResults() > 0)
     echo $oZipLayer->getNumResults()." zips found.";
else
     echo "No zips found."

?>
</body>
</html>

-------------------------------------------------   map file  
---------------------------------------------
MAP
     NAME TX
     IMAGETYPE PNG
     SIZE 600 600
     IMAGECOLOR 255 255 255
     SHAPEPATH "/Library/WebServer/Documents/data"
     FONTSET "/Library/fonts/fonts.list"
     EXTENT -106.645646 25.837377 -93.516407 36.500704
     UNITS METERS
     WEB
         IMAGEPATH "/Library/WebServer/Documents/test/"
         IMAGEURL "/test/"
     END

     LEGEND
         STATUS EMBED
         POSITION UR
     END

     SCALEBAR
         TRANSPARENT ON
         BACKGROUNDCOLOR 0 0 0
         STATUS EMBED
         POSITION LR
         STYLE 1
     END
         LAYER
             NAME "TX zip code boundaries"
             TYPE POLYGON
             STATUS DEFAULT
             DATA "zt48_d00"
             CLASS
                 COLOR 255 255 255
                 OUTLINECOLOR 0 0 0
             END
             TEMPLATE "x"
         END

         LAYER
             NAME "credits"
             STATUS DEFAULT
             TRANSFORM FALSE
             TYPE ANNOTATION
             FEATURE
                 POINTS
                     10 480
                 END
                 TEXT 'Made with Mapserver.'
             END

             CLASS
                 LABEL
                     TYPE BITMAP
                     POSITION CR
                     COLOR 0 0 0
                 END
             END
         END
END



More information about the MapServer-users mailing list