[GIS] MapServer GeoJSON output template

geojsonmapservertemplate

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:

<gml:innerBoundaryIs>
  <gml:LinearRing>
    <gml:coordinates>
      -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
    </gml:coordinates>
  </gml:LinearRing>
</gml:innerBoundaryIs>

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]
      }
    },
    [/feature]
  ]
}
[/resultset]

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.

Best Answer

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

OUTPUTFORMAT
  NAME "geojson"
  DRIVER "OGR/GEOJSON"
  MIMETYPE "application/json; subtype=geojson"
  FORMATOPTION "STORAGE=stream"
  FORMATOPTION "FORM=SIMPLE"
END

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.

Related Question