1 / 45

PHP Mapscript

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)

jemima
Download Presentation

PHP Mapscript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Contents Introduction to PHP Mapscript Object Model Application building Some Advanced features Questions

  3. 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

  4. 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

  5. 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

  6. Object Model : global view

  7. Map Object

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

  9. Access to the Map object • Howto 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(...);

  10. Layer Object

  11. 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)

  12. 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

  13. 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"); ...

  14. Class and Style • Creating a new style $oStyle = ms_newStyleObj($oClass); $oStyle->set("symbol", 1); ... • Will be covered with examples in Advanced Features section

  15. Shapefile Object

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

  17. Shapefile manipulation • Creating a line/Point object $oLine = ms_newLineObj(); $oPoint = ms_newPointObj(); • Will be covered with examples in Advanced features section

  18. 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

  19. Prepare the application structure • Directory structure of the application (data, map file, html, symbol file …) • URL to access the application • Temporary files

  20. The whole application • http://http://localhost/mum2_php_mapscript/index.phtml • The directory /ms4w/apps/mum2_php_mapscript/ • Data, htdocs, etc

  21. 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.

  22. Step1 : General • http://localhost/mum2_php_mapscript/index1.phtml • /ms4w/apps/mum2_php_mapscript/htdocs/Step1.php • /ms4w/apps/mum2_php_mapscript/htdocs/index1.phtml

  23. Step2 : Adding a map • Loading the php mapscript module • Loading the map file • Draw map function function 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"; }

  24. 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"; }

  25. Step4 : Scalebar • Draw scalebar function DrawScaleBar() { $img = $GLOBALS["goMap"]->drawScaleBar(); $url = $img->saveWebImage(); echo"<IMG SRC=$url BORDER=0>\n"; }

  26. 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

  27. 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";

  28. 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;

  29. 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); } } } }

  30. 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"]); }

  31. 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); }

  32. 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); }

  33. Step9 : draw legend function DrawLegend() { $img = $GLOBALS["goMap"]->drawLegend(); $url = $img->saveWebImage(); echo"<IMG SRC=$url BORDER=0>\n"; }

  34. 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]

  35. 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>"; }

  36. 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 ); } }

  37. 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; }

  38. 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; } }

  39. Advanced Features • Creating a new Shapefile • Adding a new layer dynamically • Adding classification dynamically

  40. Creating a shape file • Files advanced.phtml and advanced.php 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;

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

  42. 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); }

  43. 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");

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

  45. 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); }

More Related