1

I am new to runtime substitutions with Mapserver.

I have a Openlayers map where I show a mapserver WMS.

Now, I'd like the user to choose a value from a dropdown list, and filter the WMS with his/her choice.

The LAYER of my mapfile looks like:

mapfile

LAYER
  NAME zone
  TYPE POLYGON
  CONNECTIONTYPE ogr
  ...
  FILTER ('[vitigni]' = '%vitigni%')
  VALIDATION
    "vitigni" "^[a-zA-Z\-]+$"
  END
END

In my HTML I have:

myol.html

var wmsSource1 = new ol.source.ImageWMS({
    url: 'http://localhost/cgi-bin/mapserver/mapserv.exe?map=//nbgis01/mapserver/umbe/mapfile/test10_runtimesubstitution.map',
    params: {
        'LAYERS': 'cantine',
        'CRS': 'EPSG:3857',
        'BBOX': '870510,5559757,1360294,5868524',
        'WIDTH': '800',
        'HEIGHT': '500',
        'FORMAT': 'image/png'
    },
    projection: 'EPSG:3857',
    serverType: 'mapserver',
    imageExtent: imageExtent
});

What is the way to filter this WMS with (e.g.) a form?

I tried with the below code, but I only got mapserv(): Web application error. Traditional BROWSE mode requires a TEMPLATE in the WEB section, but none was provided.

myol.html (again)

<div class="query">
    <form name="mapserv" method="GET" action="http://localhost/cgi-bin/mapserver/mapserv.exe?">
        <input type="hidden" name="map" value="//nbgis01/mapserver/umbe/mapfile/test10_runtimesubstitution.map">
        <select name="vitigni">
            <option value="Groppello">Groppello</option>
        </select>
        <br><br>
        <input type="submit" value="Click Me">
    </form>
</div>

When I submit the form the request generated is http://localhost/cgi-bin/mapserver/mapserv.exe?map=%2F%2Fnbgis01%2Fmapserver%2Fumbe%2Fmapfile%2Ftest10_runtimesubstitution.map&vitigni=Groppello.

I need the page to display my openalyer map with my WMS filtered with "vitigni"="Groppello".

Just as a sidenote: the map with and without filter works correctly. If I display the whole layers I can visualize everything correctly. If I copy the request and append &vitigni=Groppello, I am able to visualize only the layer I want, but in another webpage.

2

I managed to do it!

Basically:

  1. I defined the filter in the mapfile
  2. I created the dropdown with some possible values to pass to the filter
  3. With jQuery I get the selected value, and pass it to the 'params' option of the my wms source with the updateParams() method.

mapfile

LAYER
  NAME zone
  TYPE POLYGON
  CONNECTIONTYPE ogr
  ...
  FILTER ('[vitigni]' = '%vitigni%')
  VALIDATION
    "vitigni" "^[a-zA-Z]+$"
  END
END

myol.html

<!-- the dropdown list -->

<select class="query">
    <option value="All layers" selected>All layers</option>
    <option value="Groppello">Groppello</option>
</select>

<script>

// the Mapserver WMS source

var wmsSource2 = new ol.source.ImageWMS({
    url: 'https://192.168.0.100/cgi-bin/mapserv?map=/home/umberto/Documents/apps/projects/wineshire/mapfile/mapfile/test11_linux.map',
    params: {
        'LAYERS': 'zone',
        'vitigni': 'Chardonnay, Pinot bianco, Riesling renano, Rieslin',
        'CRS': 'EPSG:3857',
        'BBOX': '884148,5588735,1172521,5764462',
        'WIDTH': '800',
        'HEIGHT': '500',
        'FORMAT': 'image/png'
    },
    projection: 'EPSG:3857',
    serverType: 'mapserver',
    imageExtent: imageExtent
});

// the dynamic filter!!!

$('.query').on('change', function() {
    var filter = $(this).val();
    var params = wmsSource2.getParams();
    params.vitigni = filter;
    wmsSource2.updateParams(params);
    });

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.