openlayers-3wmsmapservermap-files

How to tell mapserver to ignore a filter?


I have a filter that I don't want to use at the launch of the application, only on a certain action. I know there is already a question about this but it doesn't help me, I actually don't understand both of the answers.

I was in a logic of "my column = value or 1 = 1" to get all my datasets instead of just the filter if it's not called.

Here's what I wrote :

FILTER (([ct]='%ct%') or  '%ct%' = '%ct%')
VALIDATION
    'ct' '^[a-zA-Z\-]+$'
END

I call my layer with a param on Openlayers 3 with

url: 'http://localhost:5000/cgi-bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/essai.map&SERVICE=WMS&VERSION=1.1.1%20&REQUEST=GetCapabilities', serverType: 'mapserver', params: {'LAYERS': 'aisdata', 'ct':'myvalue', 'TILED': true} });

But all my dataset is returned. (If I remove '%ct%' = '%ct%' in my mapfile, the filter is well applied)

Can anyone help me to ignore my condition please?


Solution

  • Add a default value in the VALIDATION block, so that your value defaults to a empty string, and than add a OR condition in the FILER block which check if the value is a empty string:

    VALIDATION
        'ct' '^[a-zA-Z\-]+$'
        'default_ct' ''  # <-- ct will be a empty string if not provided via URL 
    END
    FILTER (([ct]='%ct%') or  ('%ct%' = '') )
    

    If the database column ct does have a numeric type, the previous filter will produce a internal server error, because you cannot compare a empty string with a number. In this case use a numeric value as default, something like 0 or -1.