Looking for Basic information about using Mapserver query functionality

Sean Gillies sgillies at FRII.COM
Thu Sep 9 15:28:49 PDT 2004


On Sep 9, 2004, at 10:24 AM, Bill Hudspeth wrote:

> Hello,
>
> Does anyone have some basic conceptual information, as well as code
> examples about using the Mapscript query functions (bypoint, byRect,
> etc.). While I am using Python mapscript on Mapserver 4.2.1, other
> programming languages are OK.
>
> Thanks, Bill
>
>

Bill,

Here's an excerpt from a new HOWTO document I am writing.  It's
formatted as Structured Text (for Zope) and uses examples in a
mapscript pseudo-code.  Nothing specific about the query methods
yet, for that check out the reference in
mapserver/mapscript/doc/mapscript.txt of your mapserver source.

cheers,
Sean

--------

Querying

   Map layers can be queried to select features using spatial query
methods:
'layerObj::queryByPoint', 'layerObj::queryByRect', and
'layerObj::queryByShape', or the attribute query method
'layerObj::queryByAttributes'.

One of MapServer's most annoying quirks is that a layer's 'template'
attribute is also the switch controlling whether or not the layer is
queryable.  Setting the 'template' attribute to _any_ string value
suffices
to activate querying::

     test_map = new mapscript.mapObj('test.map')
     layer = test_map.getLayer(0)   # this layer begins as non-queryable

     # activate querying - any value will do
     layer.template = 'foo'

Ignoring for the moment whether we are doing a spatial or attribute
query,
query results are obtained like so::

     ...  # continuing from above

     query_status = layer.queryByUnspecifiedCriteria() # not an actual
method!

     if (query_status == mapscript.MS_SUCCESS) then    # query has
results
     {
         # iterate over layer's query result set
         result_count = layer.getNumResults()
         for i in (0 .. result_count-1)
         {
             result = layer.getResult(i)
             ...  # do something with result
         }
     }

'layerObj::getResult' returns an instance of
'mapscript::resultCacheMemberObj'.
This 'result' object is a handle, of sorts, for a feature of the layer.
  Any
feature of a layer can be obtained using 'layerObj::getShape'.  If you
are
using the old-style API of previous MapServer versions, this method is
used
like::

     shape = mapscript.shapeObj(layer.type)  # new shape of the layer's
type

     layer.open()                            # must be done before
reading

     # Copy the first feature (shape index = 0) from the layer into
'shape'
     # while ignoring any tileindexing (tile index = -1)
     layer.getShape(shape, -1, 0)

     layer.close()                           # close after done reading

If you are using the optional next generation API that is a mapscript
build
option starting in MapServer version 4.2, you can use the much more
direct
method::

     layer.open()

     # in the new API getShape returns a shape of the proper type and
     # tile indexing is ignored by default
     shape = layer.getShape(0)

     layer.close()

Our 'result' object has 'shapeindex' and 'tileindex' attributes that can
be used as arguments to 'getShape'.  The previous example code can now
be
extended to the case of obtaining all queried features::

     query_status = layer.queryByUnspecifiedCriteria()

     if (query_status == mapscript.MS_SUCCESS) then
     {

         # open layer in preparation of reading shapes
         layer.open()

         result_count = layer.getNumResults()
         for i in (0 .. result_count-1)
         {
             result = layer.getResult(i)

             # Get the feature corresponding to the result using
             # the more direct next generation API
             shape = layer.getShape(result.shapeindex, result.tileindex)

             ...  # do something with this feature
         }

         # Close when done
         layer.close()
     }



More information about the MapServer-users mailing list