2

I want to get WMS GetFeatureInfo result in GeoJSON format from MapServer. Result should be valid GeoJSON FeatureCollection containing array of features from one or more layers.

I use Template-Driven Output (https://mapserver.org/output/template_output.html) with external template single file.

Template file (my.tmpl) is referenced in map file:

OUTPUTFORMAT
  NAME "geojson"
  DRIVER "TEMPLATE"
  FORMATOPTION "FILE=my.tmpl"
END

Template file (my.tmpl) looks like (two layers with Point geometry, having simple properties set):

// MapServer Template
{
 "type": "FeatureCollection",
 "features": [

[resultset layer=layer1]
[feature trimlast=","]
{
  "type": "Feature",
  "geometry": {
      "type": "Point",
      "coordinates": [ [shpxy precision=6] ]
  },
  "properties": {
    "id": [num]
  }
},
[/feature]
[/resultset]

<------ How to put ',' optional separator here?

[resultset layer=layer2]
[feature trimlast=","]
{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [ [shpxy precision=6] ]
    },
    "properties": {
      "id": [number],
      "name": "[name]"
  }
},
[/feature]
[/resultset]

]}

It works, but there is problem to put ',' separator between resultsets from several layers. Using trimlast option in [feature] tag is essential to remove ',' separator after last feature in layer results.

Is there any support for conditional template output in MapServer? I.e. output string if some condition (for example, 'featureNumber == featureCount') is satisfied ?

  • Did you get it working? Similar situation for me. – sanderp Apr 9 at 8:36
  • Currently I use a workaround - the proxy service, which is exposed to clients and it performs post-processing of received response from MapServer (along with clients authorization and other tasks, which can't be implemented in MapServer) – andw Apr 9 at 11:56

Your Answer

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

Browse other questions tagged or ask your own question.