I want to enable GeoJSON output format in MapServer to be used in an OpenLayers client.

I did create the WFS server and can make a GetFeature request as follows: http://localhost:8081/cgi-bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/example2_wfs.map&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=hydro

What I get per future looks like:

      -94.979218,49.354610 -94.975800,49.356033 -94.976563,49.359272 -94.977921,49.358330 -94.980659,49.357376 -94.984085,49.356411 -94.987511,49.355450 -94.984711,49.353626 -94.979218,49.354610

Then I followed the instructions at http://mapserver.org/output/template_output.html to enable GeoJSON output. My template file looks like:

<!– MapServer Template –>
[resultset layer=hydro]
  "type": "FeatureCollection",
  "features": [
    [feature trimlast=","]
      "type": "Feature",
      "id": "NAME",
      "geometry": {
        "type": "Polygon",
        "coordinates":[[shpxy precision="6" xh="[" yf="],"]]
      "properties": {
        "name": [NAME],
        "area": [AREA],
        "perimeter": [PERIMETER],
        "hydrogm020": [HYDROGM020],
        "feature": [FEATURE],
        "state": [STATE],
        "state_fips": [STATE_FIPS]

The problem is when I request the GeoJSON (http://localhost:8081/cgi-bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/example2_wfs.map&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=hydro&MAXFEATURES=100&outputformat=geojson)

There is this extra comma at the end of the coordinates array, like this:

{ "type": "Feature", "id": "NAME", "geometry": { "type": "Polygon", "coordinates":[[-94.979218,49.354610], [-94.984711,49.353626], [-94.987511,49.355450], [-94.984085,49.356411], [-94.980659,49.357376], [-94.977921,49.358330], [-94.976563,49.359272], [-94.975800,49.356033], [-94.979218,49.354610],] }, "properties": { "name": , "area": 0.000, "perimeter": 0.028, "hydrogm020": 11427, "feature": Null, "state": , "state_fips": } }

for which reason I cannot add the GeoJSON layer in the client.

I checked all the formatting options of the [shpxy], available at http://www.mapserver.org/mapfile/template.html but none looks can solve this problem.

2 Answers 2


Instead of templates I suggest to define an OGR outputformat for GeoJSON.

Here are advice taken from https://lists.osgeo.org/pipermail/mapserver-users/2013-November/075559.html

You can use OGR outputformats. Add something like this to your mapfile

  NAME "geojson"
  MIMETYPE "application/json; subtype=geojson"

Add to your LAYER or MAP METADATA, "wfs_getfeature_formatlist” "geojson" And then in your WFS call add &outputformat=geojson. This will return geojson directly in the browser, if you need it to download, you can change the FORMATOPTION to FORM=ZIP See http://mapserver.org/output/ogr_output.html for more detail.

  • Also use FORMATOPTION "LCO:RFC7946=YES" for more modern GeoJSON format. Commented Nov 27, 2019 at 10:28

You can use NextGIS Web (MapServer + QGIS renderers), GeoJSON and much more. You can get it free in the cloud - http://nextgis.com or install on your own server (see documentation http://docs.nextgis.com/docs_ngweb/source/toc.html and http://docs.nextgis.com/docs_ngweb_dev/doc/developer/misc.html#get-resource-data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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