1

I'm trying to filter a layer (using mapserver) backed by a Shapefile using layerObj.setFilter() but this doesn't seem to have any effect on the returned tile(s).

Sample code:

layerObj = mapserver.layerObj()
layerObj.setFilter("id = 1)

What am I doing wrong? Nothing blows up per say and mapserver produces tile(s) but they are the same set of tiles as if I had not applied the filter at all. Even applying the filter in the mapfile using FILTER "id = 1" also has no effect.

Here is a more detailed (in code) example of what I'm doing:

def index(self, *args, **kwargs):
    mapObj = mapscript.mapObj(self.mapfile)

    layers = [layer.strip().split(":", 1) for layer in kwargs["layers"].split(",")]

    # create layers
    for i, (layerType, layerName) in enumerate(layers):
        layerObj = mapObj.getLayerByName("raster" if layerType == "models" else "vector").clone()
        layerObj.name = "{0:s}:{1:s}".format(layerType, layerName)
        layerObj.data = str(self.models.join(layerName)) if layerType == "models" else str(self.regions.join(layerName))

        if "filter" in kwargs:
            layerObj.setFilter(kwargs.pop("filter"))

        mapObj.insertLayer(layerObj)

    owsReq = mapscript.OWSRequest()

    # add query string parameters into map request object
    for k, v in kwargs.items():
        owsReq.setParameter(k.upper(), v)

    # write mapserver results to buffer
    mapscript.msIO_installStdoutToBuffer()
    try:
        mapObj.OWSDispatch(owsReq)
    except:
        return SException(self.response, str(sys.exc_info()[1]))

    # adjust content-type
    self.response.headers["Content-Type"] = mapscript.msIO_stripStdoutBufferContentType()
    output = mapscript.msIO_getStdoutBufferBytes()
    self.response.headers["Content-Length"] = str(len(output))

    # output WMS image data
    return output
4

For shapefiles you should write "mapserver style" filtering expressions, e.g.

layer.setFilter("([id] = 1)")

The syntax you are using would be OK for RDBMS backends only (passed to a SQL where clause)

  • I believe I have come across this before. How exactly does MapServer distinguish between MapServer Expressions and non-MapServer Expressions? – James Mills Dec 9 '13 at 23:03
  • This doesn't seem to work. I get the following error: codepad.org/Lixxfwvk -- Can you not filter by the feature id using [id] or must it match an appropriate attribute in the features properties? – James Mills Dec 9 '13 at 23:11
  • I've accepted your answer, as it does work for filtering against attributes. What I'd like to know now is whether you can filter by the feature id? – James Mills Dec 9 '13 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.