Adding an Event Theme/Layer

Yewondwossen Assefa assefa at DMSOLUTIONS.CA
Tue Oct 5 14:25:10 PDT 2004


Bryon,
  Here are some functions that might help you acheive what you want.
This code was taken from the php mapscript workshop at the Mapserver
user meeting (http://www.omsug.ca/osgis2004/proceedings.html).

Hope it helps.

Later,

/*
  * CreateLayer()
  */
function CreateLayer($szDataName)
{
     $oMap = $GLOBALS["goMap"];
     //create layer and set members
     $oLayer = ms_newLayerObj($oMap);
     $oLayer->set("name", "canada_cities");
     $oLayer->set("status", MS_ON);
     $oLayer->set("data", $szDataName);
     $oLayer->set("type", MS_LAYER_POINT);

     $oLayer->setMetaData("DESCRIPTION", "New Cities Layer");


     //projection is latlong
     $oLayer->setProjection("init=epsg:4326");

     $oLayer->set("labelitem", "NAME");
     $oLayer->set("classitem", "CAPITAL");


     //Country capital
     //create class
     $oClass = ms_newClassObj($oLayer);

     $oClass->set("name", "Capital");
         $oClass->setexpression("/1/");

     //create style
     $oStyle = ms_newStyleObj($oClass);
     $oStyle->set("symbol", 2);
     $oStyle->set("size", 8);
     $oStyle->color->setRGB(255, 0, 0);

     //set label object
     $oClass->label->set("font", "fritqat-italic");
     $oClass->label->set("type", MS_TRUETYPE);
     $oClass->label->set("size", 8);
     $oClass->label->color->setRGB(255, 0 , 0);
     $oClass->label->set("position", MS_AUTO);
     $oClass->label->set("partials", MS_FALSE);

     //provincial capitals
     $oClass = ms_newClassObj($oLayer);
     $oClass->set("name", "Provincial Capital");
     $oClass->setexpression("/2|3/");

     $oStyle = ms_newStyleObj($oClass);
     $oStyle->set("symbol", 7);
     $oStyle->set("size", 6);
     $oStyle->color->setRGB(0, 0, 0);

     //set label object
     $oClass->label->set("font", "fritqat");
     $oClass->label->set("type", MS_TRUETYPE);
     $oClass->label->set("size", 8);
     $oClass->label->color->setRGB(0, 0 , 0);
     $oClass->label->set("position", MS_AUTO);
     $oClass->label->set("partials", MS_FALSE);
}
/*
  * AddDynamicLayer()
  */
function AddDynamicLayer()
{
     //Creating the shapefiles
     $oMap = $GLOBALS["goMap"];
     $szFileName = $oMap->web->imagepath . uniqid("");
     $oShapFile = ms_newShapeFileObj($szFileName, MS_SHP_POINT);

     //create a new DBF attached with few attributes
     $hDbf = dbase_create($szFileName.".dbf",
                          array(array("POP_RANGE", "N", 5, 0),
                                array("NAME", "C", 50, 0),
                                array("CAPITAL", "N", 5, 0)));
     if (!$hDbf)
       return;

     //open the original dbf file
     $szOrginalDbfName = "../data/canada_cities.dbf";
     $hOrigDbf = dbase_open($szOrginalDbfName, 0);
     if (!$hOrigDbf)
       return;

     $nRecords = dbase_numrecords($hOrigDbf);
     for ($i=1; $i<=$nRecords; $i++)
     {
         //retreive field attributes
         $aRecord = dbase_get_record_with_names($hOrigDbf, $i);
         $dfLat = floatval($aRecord["LAT"]);
         $dfLong = floatval($aRecord["LONG"]);
         $nPopRange = intval($aRecord["POP_RANGE"]);
         $szName = strval($aRecord["NAME"]);
         $nCapital = intval($aRecord["CAPITAL"]);

         //create a point for each record and add it to the shapefile
         $oShp = ms_newShapeObj(MS_SHP_POINT);
         $oLine = ms_newLineObj();
         $oLine->addXY($dfLong, $dfLat);
         $oShp->add( $oLine );
         $oShapFile->addShape($oShp);

         //add a record to the DBF
         $aAttValues[0] = $nPopRange;
         $aAttValues[1] = $szName;
         $aAttValues[2] = $nCapital;
         dbase_add_record($hDbf, $aAttValues);
         $oShp->free();
     }
     dbase_close($hOrigDbf);

     dbase_close($hDbf);
     $oShapFile->free();

     //Add a new layer in the map
     CreateLayer($szFileName);
}

Bryon wrote:

> Question:
> As an ArcView user, I am used to being able to integrate what is called an
> event theme into a map view.  An event theme is nothing more than a
> database with x and y attribute fields.  The application sees these fields
> and maps the points.  My question is is there a way to map a database
> using PHP Mapscript to create a Layer?  My databases are generally in
> dBase format.
>
> Does anybody have any suggestions or examples?
>
> Sincerely,
> Bryon
>


--
----------------------------------------------------------------
Assefa Yewondwossen
Software Analyst

Email: assefa at dmsolutions.ca
http://www.dmsolutions.ca/

Phone: (613) 565-5056 (ext 14)
Fax:   (613) 565-0925
----------------------------------------------------------------



More information about the MapServer-users mailing list