Enabling GeoJSON output
The default output format for WFS, and the only one currently demanded to be supported by the OGC standard, is GML. However, developers of modern web applications tend to move away from the traditional XML-based interfaces and turning more and more to the use of REST-based APIs that use more simple and light-weight exchange formats. For use in web-clients, JSON is a logical choice, and thus GeoJSON for spatial data.
Therefore, most server software is supporting JSON and GeoJSON, and MapServer is no exception. You just have to set the behaviour
of your service by making the appropriate additions to your .map
file.
Edit the configWFS.map
file by adding the sections highlisted in Listing 1 below.
Do not forget to exchange the [NETPATH]
parameters with its proper value...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
MAP NAME Thailand PROJECTION "init=epsg:4326" END EXTENT 97.3 5.6 105.7 20.5 # lon/lat extents of Thailand OUTPUTFORMAT NAME "geojson" DRIVER "OGR/GEOJSON" MIMETYPE "application/json; subtype=geojson" FORMATOPTION "STORAGE=stream" FORMATOPTION "FORM=SIMPLE" END WEB METADATA "ows_enable_request" "*" "map" "d:/iishome/staff/kobben/thailand/configWFS.map" "ows_title" "Thailand WFS Server" "ows_service" "WFS" "wfs_onlineresource" "https://gisedu.itc.utwente.nl/cgi-bin/mapserv.exe?map=d:/iishome/staff/kobben/thailand/configWFS.map&" "ows_srs" "EPSG:4326" #latlon "wfs_getfeature_formatlist" "geojson" END #metadata VALIDATION "map" "*" END END #web LAYER NAME airportsWFS TYPE POINT STATUS ON CONNECTIONTYPE postgis CONNECTION "user=exercises password=exercises dbname=exercises host=gisedu.itc.utwente.nl port=5434 options='-c client_encoding=UTF8'" DATA "geom FROM thailand.airports USING UNIQUE id USING srid=4326" METADATA "wfs_title" "airportsWFS" "gml_include_items" "all" "gml_featureid" "id" "wfs_srs" "EPSG:4326" "wfs_extent" "97.3 5.6 105.7 20.5" END #metadata END #layer airportsWFS END #map |
In lines 7–13 you tell MapServer to use the GeoJSON output driver that is available in the OGR library.
The MIMETYPE
parameter is the standardised (by the W3C) naming you'll have to use in the URL request.
In 22 you add the format to the list of available formats that this instance will deliver (adding it to the default list which already includes the GML formats).
Testing GeoJSON output
You can test the geoJSON output simply by requesting it using a simple URL request.
Note you will have to use the proper MIMETYPE
as you have set it in the format definition above...
Type the following request URL in a web browser:
https://gisedu.itc.utwente.nl/cgi-bin/mapserv.exe?map=[NETPATH]/configWFS.map&SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAME=airportsWFS&MAXFEATURES=5&OUTPUTFORMAT=application/json; subtype=geojson
The result should be a GeoJSON file send to your browser...