Mapserver PHP/MapScript Class Reference

Abstract

This document describes all of the classes, properties and methods associated with the PHP/Mapscript module.

Last Updated: 06/04/2003


PHP MapScript was originally developed for PHP-3.0.14 but after MapServer 3.5 support for PHP3 has been dropped and as of the last update of this document, PHP 4.1.2 or more recent was required.

The module has been tested and used on Linux, Solaris, *BSD, and WinNT.

This module is constantly under development.

Instances of errorObj are created internally by MapServer as errors happen. Errors are managed as a chained list with the first item being the most recent error. The head of the list can be fetched using ms_GetErrorObj(), and the list can be cleared using ms_ResetErrorList()

   The grid is always embedded inside a layer object defined as
   a grid (layer->connectiontype = MS_GRATICULE)
   (for more docs : http://mapserver.gis.umn.edu/cgi-bin/wiki.pl?MapServerGrid)

   A layer can become a grid layer by adding a grid object to it using :
      ms_newGridObj(layerObj layer)

   Example :   $oLayer = ms_newlayerobj($oMap);
              $oLayer->set("name", "GRID");
              ms_newgridobj($oLayer);
              $oLayer->grid->set("labelformat", "DDMMSS");				
				

Accessible only through the map object (map->labelcache). This object is only used to give the possiblity to free the label cache (map->labelcache->free())

   int set(string property_name, new_value)
        Set object property to a new value. Returns -1 on error.

   int draw(imageObj image)
        Draw a single layer, add labels to cache if required. 
        Returns -1 on error.

   int drawQuery(imageObj image)
        Draw query map for a single layer.

   classObj getClass(int classIndex)
        Returns a classObj from the layer given an index value (0=first class)

   int queryByPoint(pointObj point, int mode, double buffer)
        Query layer at point location specified in georeferenced map 
        coordinates (i.e. not pixels).  
        The query is performed on all the shapes that are part of a CLASS 
        that contains a TEMPLATE value or that match any class in a
        layer that contains a LAYER TEMPLATE value.
        Mode is MS_SINGLE or MS_MULTIPLE depending on number of results
        you want. 
        Passing buffer <=0 defaults to tolerances set in the map file 
        (in pixels) but you can use a constant buffer (specified in 
        ground units) instead.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByRect(rectObj rect)
        Query layer using a rectangle specified in georeferenced map 
        coordinates (i.e. not pixels).
        The query is performed on all the shapes that are part of a CLASS 
        that contains a TEMPLATE value or that match any class in a
        layer that contains a LAYER TEMPLATE value.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByShape(shapeObj shape)
        Query layer based on a single shape, the shape has to be a polygon
        at this point.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByFeatures(int slayer) 
        Perform a query set based on a previous set of results from
        another layer. At present the results MUST be based on a polygon
        layer.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByAttributes(string qitem, string qstring, int mode)
        Query layer for shapes that intersect current map extents.
        qitem is the item (attribute) on which the query is performed, 
        and qstring is the expression to match.
        The query is performed on all the shapes that are part of a CLASS 
        that contains a TEMPLATE value or that match any class in a
        layer that contains a LAYER TEMPLATE value.  
        Note that the layer's FILTER/FILTERITEM are ignored by this function.
        Mode is MS_SINGLE or MS_MULTIPLE depending on number of results
        you want. 
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int setFilter(string expression)
        Set layer filter expression.

   char getProjection()
        Returns a string represenation of the projection. If no 
        projection is set, MS_FALSE is returned.

   int setProjection(string proj_params)
        Set layer projection and coordinate system.  Parameters are given as 
        a single string of comma-delimited PROJ.4 parameters.

   int setWKTProjection(string proj_params)
        Same as setProjection(), but takes an OGC WKT projection 
        definition string as input.

   int getNumResult()
        Returns the number of results from this layer in the last query.

   resultCacheMemberObj getResult(int index)
        Returns a resultCacheMemberObj by index from a layer object with 
        index in the range 0 to numresults-1.  
        Returns a valid object or FALSE(0) if index is invalid.

   int open()
        Open the layer for use with getShape().  
        Returns MS_SUCCESS/MS_FAILURE.

   void close()
        Close layer previously opened with open().

   shapeObj getShape(int tileindex, int shapeindex)
        Retrieve shapeObj from a layer by index.
        Tileindex is used only for tiled shapefiles (you get it from the
        resultCacheMemberObj returned by getResult() for instance).
        Simply pass tileindex = -1 for other data sources.

   int addFeature(shapeObj shape)
        Add a new feature in a layer.  Returns -1 on error.

   int getMetaData(string name)
        Fetch layer metadata entry by name.  Returns "" if no entry 
        matches the name.  Note that the search is case sensitive.

   int setMetaData(string name, string value)
        Set a metadata entry for the layer.  Returns MS_SUCCESS/MS_FAILURE.

   int getWMSFeatureInfoURL(int clickX, int clickY, int featureCount, 
                            string infoFormat)
        Return a WMS GetFeatureInfo URL (works only for WMS layers)
        clickX, clickY is the location of to query in pixel coordinates
        with (0,0) at the top left of the image.
        featureCount is the number of results to return.
        infoFormat is the format the format in which the result should be
        requested.  Depends on remote server's capabilities.  MapServer
        WMS servers support only "MIME" (and should support "GML.1" soon).
        Returns "" and outputs a warning if layer is not a WMS layer 
        or if it is not queriable.
        
   char ** getItems()
        return a list of items. Must call open function first.     
		     
   mapObj clone()
        Returns a handle to a new mapObj which is a clone of the current
        mapObj.  All parameters in the current mapObj are copied to the
        new mapObj.  Returns NULL (0) on error.

   int set(string property_name, new_value)
        Set map object property to new value. Returns -1 on error.

   int getsymbolbyname(string symbol_name)  
        Returns the symbol index using the name.

   void preparequery()
        Calculate the scale of the map and assign it to the map->scale.
             
   imageObj prepareImage()
        Return handle on blank image object.

   imageObj draw()
        Render map and return handle on image object.

   imageObj drawQuery()
        Render a query map and return handle on image object.

   imageObj drawLegend()
        Render legend and return handle on image object.

   imageObj drawReferenceMap()
        Render reference map and return handle on image object.

   imageObj drawScaleBar()
        Render scale bar and return handle on image object.

   int embedlegend(imageObj image) 
        embeds a legend. Actually the legend is just added to the label 
        cache so you must invoke drawLabelCache() to actually do the 
        rendering (unless postlabelcache is set in which case it is 
        drawn right away).

   int embedScalebar(imageObj image) 
        embeds a scalebar. Actually the scalebar is just added to the label 
        cache so you must invoke drawLabelCache() to actually do the rendering 
        (unless postlabelcache is set in which case it is drawn right away).

   int drawLabelCache(imageObj image)
        Renders the labels for a map. Returns -1 on error.

   layerObj getLayer(int index)
        Returns a layerObj from the map given an index value (0=first layer)

   layerObj getLayerByName(string layer_name)
        Returns a layerObj from the map given a layer name.  
        Returns FALSE if layer doesn't exist.

   colorObj getcolorbyindex(int iCloIndex)
	Returns a colorObj corresponding to the color index in the palette

   void setextent(double minx, double miny, double maxx, double maxy)
	Set the map extents using the georef extents passed in argument.

   void zoompoint(int nZoomFactor, pointObj oPixelPos, int nImageWidth, 
                  int nImageHeight, rectObj oGeorefExt)

	Zoom to a given	XY postion;

	Parmeters are :                                                 
       - Zoom factor : positive values do zoom in, negative values  
                      zoom out. Factor of 1 will recenter.           
       - Pixel position (pointObj) : x, y coordinates of the click, 
                                     with (0,0) at the top-left
       - Width : width in pixel of the current image.                 
       - Height : Height in pixel of the current image.               
       - Georef extent (rectObj) : current georef extents.            
       - MaxGeoref extent (rectObj) : (optional) maximum georef extents.
         If provided then it will be impossible to zoom/pan outside of
         those extents.

   void zoomrectange(rectObj oPixelExt, int nImageWidth, int nImageHeight,
		     rectObj oGeorefExt)
	Set the map extents to a given extents.
	
	Parmeters are :
	- oPixelExt (rect object) : Pixel Extents, with (0,0) at the top-left
          The rectangle contains the coordinates of the LL and UR coordinates
          in pixel. (the maxy in the rect object should be < miny value)
          
          ------- UR (values in the rect object : maxx, maxy)
          |     | 
          |     |
          |     |
          ------
        LL (values in the rectobject minx, miny)                      
         
        - Width : width in pixel of the current image.
        - Height : Height in pixel of the current image. 
        - Georef extent (rectObj) : current georef extents.

   void zoomscale(double nScale, pointObj oPixelPos, int nImageWidth, 
                  int nImageHeight, rectObj oGeorefExt)

	Zoom in or out to a given XY position so that the map is displayed
        at specified scale.

	Parmeters are :                                                 
       - Scale : Scale at which the map should be displayed.
       - Pixel position (pointObj) : x, y coordinates of the click, 
                                     with (0,0) at the top-left
       - Width : width in pixel of the current image.                 
       - Height : Height in pixel of the current image.               
       - Georef extent (rectObj) : current georef extents.            
       - MaxGeoref extent (rectObj) : (optional) maximum georef extents.
         If provided then it will be impossible to zoom/pan outside of
         those extents.

   int queryByPoint(pointObj point, int mode, double buffer)
        Query all selected layers in map at point location specified in 
        georeferenced map coordinates (i.e. not pixels).  
        The query is performed on all the shapes that are part of a CLASS 
        that contains a TEMPLATE value or that match any class in a
        layer that contains a LAYER TEMPLATE value.
        Mode is MS_SINGLE or MS_MULTIPLE depending on number of results
        you want. 
        Passing buffer <=0 defaults to tolerances set in the map file 
        (in pixels) but you can use a constant buffer (specified in 
        ground units) instead.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByRect(rectObj rect)
        Query all selected layers in map using a rectangle specified in 
        georeferenced map coordinates (i.e. not pixels).
        The query is performed on all the shapes that are part of a CLASS 
        that contains a TEMPLATE value or that match any class in a
        layer that contains a LAYER TEMPLATE value.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByShape(shapeObj shape)
        Query all selected layers in map based on a single shape, the 
        shape has to be a polygon at this point.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByFeatures(int slayer) 
        Perform a query based on a previous set of results from
        a layer. At present the results MUST be based on a polygon layer.
        Returns MS_SUCCESS if shapes were found or MS_FAILURE if nothing
        was found or if some other error happened (note that the error 
        message in case nothing was found can be avoided in PHP using 
        the '@' control operator).

   int queryByIndex(layerindex, tileindex, shapeindex, addtoquery)
        Add a specific shape on a given layer to the query result.
        If addtoquery (which is a non mandatory argument) is set to MS_TRUE,
        the shape will be added to the existing query list. Default behavaior
        is to free the existing query list and add only the new shape.

   int savequery(filename)
        Save the current query in a file. Returns MS_SUCESS or MS_FAILURE.
        Can be used with loadquery

   int loadquery(filename)
        Loads a query from a file. Returns MS_SUCESS or MS_FAILURE.
        To be used with savequery.

   void freequery(layerindex)
        Frees the query result on a specified layer. If the layerindex is -1,
        all queries on layers will be freed.

   int save(string filename)
        Save current map object state to a file. Returns -1 on error.
        Use absolute path. If a relative path is used, then it will be 
        relative to the mapfile location.

   char getProjection()
        Returns a string represenation of the projection. If no 
        projection is set, MS_FALSE is returned.

   int setProjection(string proj_params, boolean bSetUnitsAndExtents)
        Set map projection and coordinate system.  Parameters are given as 
        a single string of comma-delimited PROJ.4 parameters.
        The argument : bSetUnitsAndExtents is used to automatically update
        the map units and extents based on the new projection. Possible 
        values are MS_TRUE and MS_FALSE. By defualt it is set at MS_FALSE

   int setWKTProjection(string proj_params, boolean bSetUnitsAndExtents)
        Same as setProjection(), but takes an OGC WKT projection 
        definition string as input.

   int getMetaData(string name)
        Fetch metadata entry by name (stored in the WEB object in the map
        file).  Returns "" if no entry matches the name.  Note that the
        search is case sensitive.

   int setMetaData(string name, string value)
        Set a metadata entry for the map (stored in the WEB object in the map
        file).  Returns MS_SUCCESS/MS_FAILURE.

   array getLayersIndexByGroup(string groupname)
        Return an array containing all the layer's indexes given
        a group name.

   array getAllGroupNames()
        Return an array containing all the group names used in the
        layers.  

   array getAllLayerNames()
        Return an array containing all the layer names.

   boolean moveLayerUp(int layerindex)
        Move layer up in the hierarcy of drawing. 

   boolean moveLayerDown(int layerindex)
        Move layer down in the hierarcy of drawing.

   array   getlayersdrawingorder()
        Return an array containing layer's index in the order which they
        are drawn.

   boolean  setlayersdrawingorder(array layeryindex)
        Set the layer's order array. The argument passed must be a valid
        array with all the layer's index. 
        Return TRUE on success or else FALSE.

   char *processtemplate(array params, boolean generateimages)
        Process the template file specified in the web object and return 
        the resut in a buffer. 
        The processing consists of opening the template file and replace
        all the tags found in it.
        Only tags that have an equivalent element in the map object are
        replaced (ex [scale]). 
        The are two expetions to the previous statement :
          - [img], [scalebar], [ref], [legend] would be replaced with the 
            appropriate url if the parameter generateimages is set to 
            MS_TRUE. (Note :  the images corresponding to the diffrent objects 
            are generated if the object is set to MS_ON in the map file)
         - the user can use the params parameter to specify tags and 
           their values. For example if the user have a specific tag call
           [my_tag] and would like it to be replaced by "value_of_my_tag"
           he would do :
              $tmparray["my_tag"] = "value_of_my_tag";
              $map->processtemplate($tmparray, MS_FASLE);
         
   char *processquerytemplate(array params)     
       Process query template files and return the resut in a buffer.

       See also : processtemplate function.
   
   char *processlegendtemplate(array params)     
       Process legend template files and return the resut in a buffer.

     See also : processtemplate function.

   int setSymbolSet(string fileName)
       Load and set a symbol file dynamictly.
       
   int getNumSymbols()
       Return the number of symbols in map.
       
   int setFontSet(string fileName)
       Load and set a new fontset.
       

   int selectOutputFormat(string type)
        Selects the output format to be used in the map. 
        Returns MS_SUCCESS/MS_FAILURE.
       
        Note : the type used should correspond to one of the output
               formats declared in the map file.
               The type argument passed is compared with the mimetype
               parameter in the output format structure and then to
               the name parameter in the structure. 

   int saveMapContext(string filename)
        Available only if WMS support is enabled.
        Save current map object state in WMS Map Context format. 
        Only WMS layers are saved in the WMS Map Context XML file.
        Returns MS_SUCCESS/MS_FAILURE.

   int loadMapContext(string filename)
        Available only if WMS support is enabled.
        Load a WMS Map Context XML file into the current mapObj.
        If the map already contains some layers then the layers defined
        in the WMS Map context document are added to the current map.
        Returns MS_SUCCESS/MS_FAILURE.
			    
Q:.

How do I change the colour of a shape on-the-fly?

A:.

Say you have an image object that you are trying to draw more objects on:

    $imageObj = $mapObj->draw();
    $pointObj->draw($mapObj, $layerObj, $imageObj, 0, $label);						
						      

Fine. However, you want to change the colour of the point on the fly before you draw it:

    $imageObj = $mapObj->draw();
    $classObj = $layerObj->getClass(0);
    $newColour = $mapObj->addColor(255,0,0);
    $classObj->set('color', $newColour);
    $pointObj->draw($mapObj, $layerObj, $imageObj, 0, $label);						
						      

This won't work. In order to get the result you want, you must call $mapObj->draw(); after you add your colour to the palatte.