PHP Mapscript - PowerPoint PPT Presentation

About This Presentation
Title:

PHP Mapscript

Description:

Because it provides the ability to do things that can't be done in ... echo ' tr bgcolor='#E2EFF5' n'; echo ' td /td n'; echo ' td /td n'; echo ' /tr n' ... – PowerPoint PPT presentation

Number of Views:791
Avg rating:3.0/5.0
Slides: 46
Provided by: dmsol3
Learn more at: http://dl.maptools.org
Category:
Tags: php | bgcolor | mapscript

less

Transcript and Presenter's Notes

Title: PHP Mapscript


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/do
    c/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-gtname
  • How to access functions of the object (case
    insensitive)
  • oLayer oMap-gtgetlayer(0)

9
Access to the Map object
  • How to modify member of the object
  • oMap-gtset("name", "a new name")
  • common mistake oMap-gtname "a new name"
  • How to access the sub objects
  • oMap-gtweb-gtset(...)

10
Layer Object
11
Access to the Layer object
  • Accessing layers through the map object
  • nLayers oMap-gtnumlayers
  • for (i0 iltnLayers i)
  • oLayer oMap-gtgetlayer(i)
  • echo oLayer-gtname .
  • ..
  • Creating a new layer
  • oLayer ms_newLayerObj(oMap)
  • oLayer-gtset("name", "my_new_layer")
  • oLayer-gtset("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-gtnumclass
  • for (i0 iltnClass i)
  • oClass oMap-gtgetclass(i)
  • echo oClass-gtname
  • ...
  • Creating a new class
  • oClass ms_newClassObj(oLayer)
  • oLayer-gtset("name", "my_new_layer")
  • ...

14
Class and Style
  • Creating a new style
  • oStyle ms_newStyleObj(oClass)
  • oStyle-gtset("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.p
    html
  • 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"-gtdraw()
  • url img-gtsaveWebImage()
  • nWidth GLOBALS"goMap"-gtwidth
  • nHeight GLOBALS"goMap"-gtheight
  • echo"ltINPUT TYPEimage SRC".url." BORDER0
    WIDTH\"".
  • nWidth."\" HEIGHT\"".nHeight."\"
    NAMEMAINMAPgt\n"

24
Step3 Keymap
  • Draw a key map
  • function DrawKeyMap()
  • img GLOBALS"goMap"-gtdrawreferencemap()
  • url img-gtsaveWebImage()
  • echo "ltINPUT TYPEimage SRCurl BORDER0
    NAMEKEYMAPgt\n"

25
Step4 Scalebar
  • Draw scalebar
  • function DrawScaleBar()
  • img GLOBALS"goMap"-gtdrawScaleBar()
  • url img-gtsaveWebImage()
  • echo"ltIMG SRCurl BORDER0gt\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) gt 0) ? _POST
    (sizeof(_GET) gt 0) ? _GET array()
  • ProcessURLArray( aVars )
  • In DrawMap function
  • echo"ltINPUT TYPEimage SRC".url." BORDER0
    WIDTH\"".
  • nWidth."\" HEIGHT\"".nHeight."\"
    NAMEMAINMAPgt\n"
  • echo "ltINPUT TYPEHIDDEN NAMEMINX VALUE\"".
  • GLOBALS"goMap"-gtextent-gtminx."\"gt\n"
  • echo "ltINPUT TYPEHIDDEN NAMEMINY VALUE\"".
  • GLOBALS"goMap"-gtextent-gtminy."\"gt\n"
  • echo "ltINPUT TYPEHIDDEN NAMEMAXX VALUE\"".
  • GLOBALS"goMap"-gtextent-gtmaxx."\"gt\n"
  • echo "ltINPUT TYPEHIDDEN NAMEMAXY VALUE\"".
  • GLOBALS"goMap"-gtextent-gtmaxy."\"gt\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-gtextent-gtminx
  • fMinY isset(aVars"MINY") ?
    aVars"MINY" oExt-gtextent-gtminy
  • fMaxX isset(aVars"MAXX") ?
    aVars"MAXX" oExt-gtextent-gtmaxx
  • fMaxY isset(aVars"MAXY") ?
    aVars"MAXY" oExt-gtextent-gtmaxy
  • GLOBALS"goMap"-gtsetextent( fMinX, fMinY,
    fMaxX, fMaxY )
  • //some short cuts
  • fWidthPix GLOBALS"goMap"-gtwidth
  • fHeightPix GLOBALS"goMap"-gtheight

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-gtsetxy(nX, nY)
  • oGeorefExt ms_newrectobj()
  • oGeorefExt-gtsetextent(fMinX,
    fMinY, fMaxX, fMaxY)
  • if (aVars"CMD" "ZOOM_IN")

30
Step 6 All Nav. tools
  • Modify function ProcessURLArray to look for other
    CMD values
  • .
  • else if (aVars"CMD" "ZOOM_OUT")
  • GLOBALS"goMap"-gtzoompoint(-2,
    oPixelPos, fWidthPix,
  • fHeightPix,
    oGeorefExt)
  • else if (aVars"CMD" "RECENTER")
  • GLOBALS"goMap"-gtzoompoint(1,
    oPixelPos, fWidthPix,
  • fHeightPix,
    oGeorefExt)
  • else if (aVars"CMD"
    "ZOOM_FULL")
  • GLOBALS"goMap"-gtsetextent(GLOB
    ALS"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"-gtreference-gtext
    ent
  • nX intval(aVars"KEYMAP_x")
  • nY intval(aVars"KEYMAP_y")
  • fWidthPix doubleval(GLOBALS"goMap"-gtre
    ference-gtwidth)
  • fHeightPix doubleval(GLOBALS"goMap"-gtr
    eference-gtheight)
  • nGeoX Pix2Geo(nX, 0, fWidthPix,
    oRefExt-gtminx, oRefExt-gtmaxx, 0)
  • nGeoY Pix2Geo(nY, 0, fHeightPix,
    oRefExt-gtminy, oRefExt-gtmaxy, 1)
  • fDeltaX (fMaxX - fMinX) / 2.0
  • fDeltaY (fMaxY - fMinY) / 2.0
  • GLOBALS"goMap"-gtsetextent(nGeoX -
    fDeltaX, nGeoY - fDeltaY,
  • nGeoX
    fDeltaX, nGeoY fDeltaY)

33
Step9 draw legend
  • function DrawLegend()
  • img GLOBALS"goMap"-gtdrawLegend()
  • url img-gtsaveWebImage()
  • echo"ltIMG SRCurl BORDER0gt\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 orderascending opt_flag15 lttr
    height20pxgt
  • lttd align"center"gtltinput type"checkbox"
    name"legendlayername" value"leg_layer_name"
    if namelayer_status opereq value1CHECKED/if
    if namelayer_status opereq value2CHECKED/if
    gtlt/tdgt
  • lttdgtnbspltimg src"leg_icon width20
    height13" width20 height13gtnbsplt/tdgt
  • lttdgtltfont face"Arial, Helvetica, sans-serif"
    size"3"gtmetadata nameDESCRIPTIONlt/fontgtlt/tdgt
  • lt/trgt
  • /leg_layer_html

35
Step 10 HTML Legend
  • Call function DrawHTMLLegend() in index10.phtml
  • function DrawHTMLLegend()
  • echo "lttable cellspacing0 cellpadding0gt"
  • echo "lttr bgcolor\"E2EFF5\"gt\n"
  • echo "lttdgtlt/tdgt\n"
  • echo "lttdgtlt/tdgt\n"
  • echo "lt/trgt\n"
  • echo GLOBALS"goMap"-gtprocessLegendTemplate(
    array() )
  • echo "lttrgt\n"
  • echo "lttdgtltinput type\"image\"
    src\"./images/icon_update.png\" width\"20\"
    height\"20\"gtlt/tdgt\n"
  • echo "lttd colspan2gtltfont face\"Arial,Helveti
    ca,sans-serif\" size\"2\"gtUpdatelt/tdgt\n"
  • echo "lt/trgt\n"
  • echo "lt/tablegt"

36
Step11 Layer control
  • Inside function ProcessURLArray
  • //process layer visibility
  • if (isset(_GET"legendlayername"))
  • for( i0 iltGLOBALS"goMap"-gtnumlayers
    i )
  • oLayer GLOBALS"goMap"-gtgetLayer(
    i)
  • if (in_array( oLayer-gtname,
    _GET"legendlayername" ))
  • oLayer-gtset( "status", MS_ON )
  • else
  • oLayer-gtset( "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-gtsetXY(nGeoX, nGeoY)
  • // Use '_at_' to avoid warning if
    query found nothing
  • _at_GLOBALS"goMap"-gtqueryByPoint(
    oGeo, MS_MULTIPLE, -1)
  • GLOBALS"gShowQueryResults"
    TRUE

38
Step12 Query
  • GLOBALS"gShowQueryResults" FALSE
  • function DrawMap()
  • if (GLOBALS"gShowQueryResults")
  • img GLOBALS"goMap"-gtdrawQuery()
  • else
  • img GLOBALS"goMap"-gtdraw()
  • function DrawQueryResults()
  • if (!GLOBALS"gShowQueryResults")
  • echo "nbsp"
  • else
  • sTemplate GLOBALS"goMap"-gtprocessque
    rytemplate(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-gtweb-gtimagepath .
    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 (i1
    iltnRecords 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-gtaddXY(dfLong, dfLat)
  • oShp-gtadd( oLine )
  • oShapFile-gtaddShape(oShp)
  • //add a record to the DBF
  • aAttValues0 nPopRange
  • aAttValues1 szName
  • aAttValues2 nCapital
  • dbase_add_record(hDbf, aAttValues)
  • oShp-gtfree()
  • dbase_close(hOrigDbf)
  • dbase_close(hDbf)
  • oShapFile-gtfree()

43
Adding a new layer and styling it
  • function CreateLayer(szDataName)
  • oMap GLOBALS"goMap"
  • //create layer and set members
  • oLayer ms_newLayerObj(oMap)
  • oLayer-gtset("name", "canada_cities")
  • oLayer-gtset("status", MS_ON)
  • oLayer-gtset("data", szDataName)
  • oLayer-gtset("type", MS_LAYER_POINT)
  • //projection is latlong
  • oLayer-gtsetProjection("initepsg4326")
  • oLayer-gtset("labelitem", "NAME")
  • oLayer-gtset("classitem", "CAPITAL")

44
Adding a new layer and styling it
  • //Country capital
  • //create class
  • oClass ms_newClassObj(oLayer)
  • oClass-gtset("name", "Capital")
  • oClass-gtsetexpression("/1/")
  • //create style
  • oStyle ms_newStyleObj(oClass)
  • oStyle-gtset("symbol", 2)
  • oStyle-gtset("size", 8)
  • oStyle-gtcolor-gtsetRGB(255, 0, 0)
  • //set label object
  • oClass-gtlabel-gtset("font",
    "fritqat-italic")
  • oClass-gtlabel-gtset("type", MS_TRUETYPE)
  • oClass-gtlabel-gtset("size", 8)
  • oClass-gtlabel-gtcolor-gtsetRGB(255, 0 , 0)
  • oClass-gtlabel-gtset("position", MS_AUTO)

45
Adding a new layer and styling it
  • //provincial capitals
  • oClass ms_newClassObj(oLayer)
  • oClass-gtset("name", "Provincial Capital")
  • oClass-gtsetexpression("/23/")
  • oStyle ms_newStyleObj(oClass)
  • oStyle-gtset("symbol", 7)
  • oStyle-gtset("size", 6)
  • oStyle-gtcolor-gtsetRGB(0, 0, 0)
  • //set label object
  • oClass-gtlabel-gtset("font", "fritqat")
  • oClass-gtlabel-gtset("type", MS_TRUETYPE)
  • oClass-gtlabel-gtset("size", 8)
  • oClass-gtlabel-gtcolor-gtsetRGB(0, 0 , 0)
  • oClass-gtlabel-gtset("position", MS_AUTO)
  • oClass-gtlabel-gtset("partials", MS_FALSE)
Write a Comment
User Comments (0)
About PowerShow.com