2

Could you please provide a working example of WMS mapfile with GeoJSON layer for the MapServer?

Mine is not working:

MAP
  EXTENT -120 34 -119 35 # Geographic
  SIZE 800 400

 LAYER
  TYPE polygon
  NAME "cells"
  METADATA
    OWS_TITLE "MetGeoJSON Polygon"
  END
  CONNECTIONTYPE OGR
  CONNECTION "shp/geoJson.json"
  DATA "OGRGeoJSON"
  STATUS default
  PROJECTION
    "init=epsg:4326"
  END
END
END

The error message is:

msDrawMap(): Image handling error. Failed to draw layer named 'cells'. msOGRFileOpen(): OGR error. GetLayer(OGRGeoJSON) failed for OGR connection. Check logs. 
2
  • The error message says "Check logs". What do the logs say? Can you open the geojson in another software, like QGIS?
    – user2856
    May 1, 2021 at 9:20
  • I've added an error file to the mapfile but didn't see more details. The mapfile changes suggested by @user30184 works fine May 6, 2021 at 20:47

2 Answers 2

4

@david-osipyan I used your question as the excuse needed to create new documentation for GeoJSON access in MapServer, with a new page visible now at: https://mapserver.org/input/vector/geojson.html

I hope that helps the next person coming along, in the MapServer community :)

By the way, it was so nice to read here in an earlier comment from you that MS4W is working great for you for GeoJSON access. Excellent! (it's nice to hear complements for once online, ha) I spend a lot of effort (through my company, GatewayGeo) to create/distribute/pay for all the downloads of MS4W. I'm glad to hear it helps your organization.

PS. as you notice at the bottom of that new page, it mentions that all MS4W installations include a default mapfile (in /ms4w/apps/local-demo/local.map) that includes GeoJSON output, out-of-the-box.

Have a nice weekend,

Jeff McKenna
@mapserving
1
  • Thank you, Jeff. A mapfile from @user30184's answer also works great. The Linux issue was related to the user permissions. Actually I need to get a JSON file itself from the MapServer, not an image created with that file. Now I looking for WFS configuration for that, May 8, 2021 at 2:44
1

Your mapfile is mostly OK. I used the states.shp shapefile from the GeoServer demo data in my test and converted it into GeoJSON with ogr2ogr

ogr2ogr -f geojson geoJson.json states.shp

I checked with ogrinfo that my geoJson.json file contains a layer named "states" and used that in my mapfile. Other edits that I did are

Because you used the relative path I created also a subdirectory "shp" under the directory where the mapfile is located and plased the GeoJSON file there. Myself I prefer to use absolute paths but relative paths are OK as well.

MAP
  #EXTENT -120 34 -119 35 # Geographic
  EXTENT -180 -90 180 90 # Geographic
  SIZE 800 400
 

 LAYER
  TYPE polygon
  NAME "cells"
  METADATA
    OWS_TITLE "MetGeoJSON Polygon"
  END
  CONNECTIONTYPE OGR
  CONNECTION "shp/geoJson.json"
  #DATA "OGRGeoJSON"
  data "states"
  STATUS default
  CLASS
        NAME "Parks"
        STYLE
          COLOR 200 255 0
          OUTLINECOLOR 120 120 120
        END # Style
  END # Class
  PROJECTION
    "init=epsg:4326"
  END
END
END

I used this query on my browser for testing

http://localhost/cgi-bin/mapserv.exe?map=c:\test\json.map&mode=map

The result is this

enter image description here

I suppose that your GeoJSON file is not in the correct directory that is relational to the mapfile location, of the layer of the GeoJSON is not "OGRGeoJSON".

Your mapfile is very minimal but it does work even without CLASS and STYLE. You just won't see anything on the map because nothing is rendered. For next tests I suggest to use the mapfile from the WMS Server manual page as a template. It is still short but contains useful examples about how to configure projections and basic styles and all that is needed for setting up a WMS service.

2
  • Thank you very much. It works fine. Is it possible to get a GeoJSON file instead of the image? May 6, 2021 at 20:48
  • With Web Feature Service, yes mapserver.org/ogc/wfs_server.html. The default format is GML but GeoJSON can be configured as an alternative format. Make it work first with the standard GML It is easier to test so. Once everything works with GML it is rather simple to add GeoJSON. Hardest part is to find the correct documentation.
    – user30184
    May 6, 2021 at 22:03

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.