45
PHP Mapscript Yewondwossen Assefa Daniel Morissette DM Solutions Group Inc.

PHP Mapscript Yewondwossen Assefa Daniel Morissette DM Solutions Group Inc

  • View
    232

  • Download
    2

Embed Size (px)

Citation preview

PHP Mapscript

Yewondwossen Assefa

Daniel Morissette

DM Solutions Group Inc.

Contents

• Introduction to PHP Mapscript• Object Model• Application building • Some Advanced features• Questions

What is PHP Mapscript

• one flavour of MapScript (Perl, Python, Java versions exist also)

• custom extension for PHP

• abstracts the MapServer object model in PHP constructs

Why do we use PHP Mapscript

• Because it provides the ability to do things that can't be done in MapServer's CGI mode

• Dynamic layers• Customized navigation• On the fly classification• Enhanced legends and layer controls• Advanced query capabilities• PHP is an advanced open source programming

language with significant modules such as GD, dbase, odbc

Object Model

• Cover the main objects found in php/mapscript and how to access members and functions of some of the objects

• Php Mapscript : "Jacket" on top of mapserver library

• Most of the mapserver elements accessible through php/mapscript

• Documentation at : http://mapserver.gis.umn.edu/doc/phpmapscript-class-guide.html

Object Model : global view

Map Object

Access to the Map object

• How to create a new map object$oMap = ms_newMapObj("mymapfile.map"); $oMap = ms_newMapObj("");

• How to access members of the object (case sensitive)echo $oMap->name

• How to access functions of the object (case insensitive)$oLayer = $oMap->getlayer(0);

Access to the Map object

• How to modify member of the object

$oMap->set("name", "a new name");

common mistake : $oMap->name = "a new name";

• How to access the sub objects$oMap->web->set(...);

Layer Object

Access to the Layer object• Accessing layers through the map object

$nLayers = $oMap->numlayers; for ($i=0; $i<$nLayers; $i++) {

$oLayer = oMap->getlayer($i); echo $oLayer->name; ...

}

• Creating a new layer$oLayer = ms_newLayerObj($oMap) $oLayer->set("name", "my_new_layer"); $oLayer->set("type", MS_LAYER_LINE);

//can be used to create a layer from another layer : $oLayer = ms_newLayerObj($oMap, $srclayer)

Layer object

• Diffrent types of layers : predefined constants– MS_LAYER_POINT,

– MS_LAYER_LINE,

– MS_LAYER_POLYGON

– MS_LAYER_RASTER

– MS_LAYER_ANNOTATION

– MS_LAYER_QUERY

– MS_LAYER_CIRCLE

– MS_LAYER_TILEINDEX

Class and Style

• Accessing classes through the layer object$nClass = $oLayer->numclass; for ($i=0; $i<$nClass; $i++) {

$oClass = oMap->getclass($i); echo $oClass->name; ...

}

• Creating a new class$oClass = ms_newClassObj($oLayer); $oLayer->set("name", "my_new_layer"); ...

Class and Style

• Creating a new style$oStyle = ms_newStyleObj($oClass);

$oStyle->set("symbol", 1); ...

• Will be covered with examples in Advanced Features section

Shapefile Object

Shapefile manipulation

• Open existing shapefile//first argument is the shapefilename without the extension //second argument (type) is set to -1 to open an existing file $oShapeFile = ms_newShapefileObj("my_shapefile", -1);

• Creating a shape object//The argument represents the type of shape object to be

created.//Valid types are : MS_SHAPE_POINT, MS_SHAPE_LINE,

MS_SHAPE_POLYGON $oShape = ms_newShapeObj(MS_SHP_POINT);

Shapefile manipulation

• Creating a line/Point object

$oLine = ms_newLineObj();$oPoint = ms_newPointObj();

• Will be covered with examples in Advanced features section

Application Building : Contents

• Drawing a Map

• Adding a Key Map

• Adding a Scalebar

• Adding Navigation Tools (zoom in, …)

• Navigation using the Key Map

• Adding a Legend

• HTML Legend

• Query

Prepare the application structure

• Directory structure of the application (data, map file, html, symbol file …)

• URL to access the application

• Temporary files

Application structure

• Index(X).phtml and step(X).php

• Separate HTML from the PHP code

• Phtml file contains already calls to PHP functions. PHP functions are there but are empty.

Step2 : Adding a map• Loading the php mapscript module• Loading the map file• Draw map functionfunction DrawMap(){ $img = $GLOBALS["goMap"]->draw(); $url = $img->saveWebImage();

$nWidth = $GLOBALS["goMap"]->width; $nHeight = $GLOBALS["goMap"]->height;

echo"<INPUT TYPE=image SRC=".$url." BORDER=0 WIDTH=\"". $nWidth."\" HEIGHT=\"".$nHeight."\" NAME=MAINMAP>\n";}

Step3 : Keymap

• Draw a key map

function DrawKeyMap(){ $img = $GLOBALS["goMap"]->drawreferencemap(); $url = $img->saveWebImage();

echo "<INPUT TYPE=image SRC=$url BORDER=0 NAME=KEYMAP>\n";

}

Step4 : Scalebar

• Draw scalebar

function DrawScaleBar()

{

$img = $GLOBALS["goMap"]->drawScaleBar();

$url = $img->saveWebImage();

echo"<IMG SRC=$url BORDER=0>\n";

}

Step5 : adding Navigation (zoom in)

• Know what type of navigation is selected

• $_GET ($_POST) : super global associative array for HTTP GET

• Preserve current map extents, width and height

Step 5 : adding Navigation (zoom in)

• Top of the file$aVars = (sizeof($_POST) > 0) ? $_POST : (sizeof($_GET) > 0) ? $_GET : array();ProcessURLArray( $aVars );

• In DrawMap function echo"<INPUT TYPE=image SRC=".$url." BORDER=0 WIDTH=\"". $nWidth."\" HEIGHT=\"".$nHeight."\" NAME=MAINMAP>\n";

echo "<INPUT TYPE=HIDDEN NAME=MINX VALUE=\"". $GLOBALS["goMap"]->extent->minx."\">\n"; echo "<INPUT TYPE=HIDDEN NAME=MINY VALUE=\"". $GLOBALS["goMap"]->extent->miny."\">\n"; echo "<INPUT TYPE=HIDDEN NAME=MAXX VALUE=\"". $GLOBALS["goMap"]->extent->maxx."\">\n"; echo "<INPUT TYPE=HIDDEN NAME=MAXY VALUE=\"". $GLOBALS["goMap"]->extent->maxy."\">\n";

Step 5 : adding Navigation (zoom in)

function ProcessURLArray( $aVars){ //set up the previous extents $oExt = $GLOBALS["goMap"]; $fMinX = isset($aVars["MINX"]) ? $aVars["MINX"] : $oExt->extent->minx; $fMinY = isset($aVars["MINY"]) ? $aVars["MINY"] : $oExt->extent->miny;; $fMaxX = isset($aVars["MAXX"]) ? $aVars["MAXX"] : $oExt->extent->maxx;; $fMaxY = isset($aVars["MAXY"]) ? $aVars["MAXY"] : $oExt->extent->maxy;;

$GLOBALS["goMap"]->setextent( $fMinX, $fMinY, $fMaxX, $fMaxY );

//some short cuts $fWidthPix = $GLOBALS["goMap"]->width; $fHeightPix = $GLOBALS["goMap"]->height;

Step5 : adding Navigation (zoom in)//process commands if (isset($aVars["CMD"])) { //determine where to navigate to. $nX = isset($aVars["MAINMAP_x"]) ? intval($aVars["MAINMAP_x"]) : $fWidthPix/2.0; $nY = isset($aVars["MAINMAP_y"]) ? intval($aVars["MAINMAP_y"]) : $fWidthPix/2.0;

if (isset($aVars["MAINMAP_x"]) && isset($aVars["MAINMAP_y"])) { $oPixelPos = ms_newpointobj(); $oPixelPos->setxy($nX, $nY);

$oGeorefExt = ms_newrectobj(); $oGeorefExt->setextent($fMinX, $fMinY, $fMaxX, $fMaxY);

if ($aVars["CMD"] == "ZOOM_IN") { $GLOBALS["goMap"]->zoompoint(2, $oPixelPos, $fWidthPix, $fHeightPix, $oGeorefExt); } } }}

Step 6 : All Nav. tools• Modify function ProcessURLArray to look for other CMD values …. else if ($aVars["CMD"] == "ZOOM_OUT") { $GLOBALS["goMap"]->zoompoint(-2, $oPixelPos, $fWidthPix, $fHeightPix, $oGeorefExt); } else if ($aVars["CMD"] == "RECENTER") { $GLOBALS["goMap"]->zoompoint(1, $oPixelPos, $fWidthPix, $fHeightPix, $oGeorefExt); } else if ($aVars["CMD"] == "ZOOM_FULL") { $GLOBALS["goMap"]->setextent($GLOBALS["gfMinX"],$GLOBALS["gfMinY"], $GLOBALS["gfMaxX"],$GLOBALS["gfMaxY"]); }

Step7 : Keep selected tool• $GLOBALS["gszCurrentTool"] = "ZOOM_IN";

function ProcessURLArray( $aVars){ //record the current command $GLOBALS["gszCurrentTool"] = (isset($aVars["CMD"])) ? $aVars["CMD"] :

"ZOOM_IN"; …}

function IsCurrentTool( $szTool ){ return (strcasecmp($GLOBALS["gszCurrentTool"], $szTool) == 0);}

Step8 : KeyMap navigation• Inside function ProcessURLArray else if (isset($aVars["KEYMAP_x"]) && isset($aVars["KEYMAP_y"])) { $oRefExt = $GLOBALS["goMap"]->reference->extent; $nX = intval($aVars["KEYMAP_x"]); $nY = intval($aVars["KEYMAP_y"]); $fWidthPix = doubleval($GLOBALS["goMap"]->reference->width); $fHeightPix = doubleval($GLOBALS["goMap"]->reference->height); $nGeoX = Pix2Geo($nX, 0, $fWidthPix, $oRefExt->minx, $oRefExt->maxx, 0); $nGeoY = Pix2Geo($nY, 0, $fHeightPix, $oRefExt->miny, $oRefExt->maxy, 1); $fDeltaX = ($fMaxX - $fMinX) / 2.0; $fDeltaY = ($fMaxY - $fMinY) / 2.0; $GLOBALS["goMap"]->setextent($nGeoX - $fDeltaX, $nGeoY - $fDeltaY, $nGeoX + $fDeltaX, $nGeoY + $fDeltaY); }

Step9 : draw legend

function DrawLegend()

{

$img = $GLOBALS["goMap"]->drawLegend();

$url = $img->saveWebImage();

echo"<IMG SRC=$url BORDER=0>\n";

}

Step10 : HTML Legend

• Docs at : http://mapserver.gis.umn.edu/doc40/html-legend-howto.html• The HTML legend is an alternative to the traditional GIF legend in

MapServer• Set TEMPLATE "legend.html“ in the map file inside the legend object[leg_layer_html order=ascending opt_flag=15] <tr height=20px> <td align="center"><input type="checkbox" name="legendlayername[]"

value="[leg_layer_name]"[if name=layer_status oper=eq value=1]CHECKED[/if][if name=layer_status oper=eq value=2]CHECKED[/if]></td>

<td>&nbsp;<img src="[leg_icon width=20 height=13]" width=20 height=13>&nbsp;</td>

<td><font face="Arial, Helvetica, sans-serif" size="3">[metadata name=DESCRIPTION]</font></td>

</tr>[/leg_layer_html]

Step 10 : HTML Legend• Call function DrawHTMLLegend(); in index10.phtml

function DrawHTMLLegend(){ echo "<table cellspacing=0 cellpadding=0>"; echo "<tr bgcolor=\"#E2EFF5\">\n"; echo "<td></td>\n"; echo "<td></td>\n"; echo "</tr>\n"; echo $GLOBALS["goMap"]->processLegendTemplate( array() ); echo "<tr>\n"; echo "<td><input type=\"image\" src=\"./images/icon_update.png\" width=\"20\"

height=\"20\"></td>\n"; echo "<td colspan=2><font face=\"Arial,Helvetica,sans-serif\" size=\"2\">Update</td>\n"; echo "</tr>\n"; echo "</table>"; }

Step11 : Layer control

• Inside function ProcessURLArray…//process layer visibility if (isset($_GET["legendlayername"])) { for( $i=0; $i<$GLOBALS["goMap"]->numlayers; $i++ ) { $oLayer = $GLOBALS["goMap"]->getLayer($i); if (in_array( $oLayer->name, $_GET["legendlayername"] )) $oLayer->set( "status", MS_ON ); else $oLayer->set( "status", MS_OFF ); } }

Step12 : Query• Set templates in a layer• Function ProcessURLArray…. else if ($aVars["CMD"] == "QUERY") { $nGeoX = Pix2Geo($nX, 0, $fWidthPix, $fMinX, $fMaxX, 0); $nGeoY = Pix2Geo($nY, 0, $fHeightPix, $fMinY, $fMaxY, 1);

$oGeo = ms_newPointObj(); $oGeo->setXY($nGeoX, $nGeoY);

// Use '@' to avoid warning if query found nothing @$GLOBALS["goMap"]->queryByPoint($oGeo, MS_MULTIPLE, -1);

$GLOBALS["gShowQueryResults"] = TRUE;

}

Step12 : Query• $GLOBALS["gShowQueryResults"] = FALSE;function DrawMap(){ if ($GLOBALS["gShowQueryResults"]) $img = $GLOBALS["goMap"]->drawQuery(); else $img = $GLOBALS["goMap"]->draw(); …

function DrawQueryResults(){ if (!$GLOBALS["gShowQueryResults"]) echo "&nbsp;"; else { $sTemplate = $GLOBALS["goMap"]->processquerytemplate(array(), 0); echo $sTemplate;

}}

Advanced Features

• Creating a new Shapefile

• Adding a new layer dynamically

• Adding classification dynamically

Creating a shape file

• Files advanced.phtml and advanced.phpfunction 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;

Creating a shape file

• 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"]);

Creating a shape file//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);}

Adding a new layer and styling it

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);

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

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

Adding a new layer and styling it //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);

Adding a new layer and styling it

//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);}