GeoJSON

Author:

Jeff McKenna

Contact:

jmckenna at gatewaygeomatics.com

Last Updated:

2021-09-15

GeoJSON is a text-based, open standard format that can represent vector points, lines, polygons, and multi-part collections. Officially, it is a geospatial data interchange format based on JavaScript Object Notation (JSON). GeoJSON is a specification as part of the Internet Engineering Task Force (IETF).

More Information

More information about GeoJSON is available at:

File listing

GeoJSON files are usually a single text file with a JSON filename extension, such as:

countries.json

You can open that file in a text editor, and it will look something like:

{
    "type": "FeatureCollection",
    "name": "countries",
    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
    "features": [
    { "type": "Feature", "properties": { "name_en": "Fiji" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 180.0, -16.067132663642447 ], [clipped for display here...], [ -179.793320109048636, -16.020882256741224 ] ] ] ] } },
    { "type": "Feature", "properties": { "name_en": "Tanzania" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 33.903711197104528, -0.95 ], [ 34.07262, -1.05982 ], [clipped for display here...], [ 33.903711197104528, -0.95 ] ] ] ] } },
    ...
    ]
}

Astuce

You can also drag and drop your GeoJSON file into a modern web browser, such as FireFox, and it will nicely display your file.

Note

Windows users who want to follow along, can convert MS4W’s included demo.db SpatiaLite database into a GeoJSON file (for the countries table) with the command (executed inside /ms4w/apps/local-demo/data/) :

ogr2ogr -f GeoJSON countries.json demo.db countries

Data Access / Connection Method

  • GeoJSON access is available in MapServer through OGR’s GeoJSON driver.

  • The CONNECTIONTYPE OGR parameter must be used.

  • The path to the GeoJSON file is required, including the file extension.

CONNECTIONTYPE OGR
CONNECTION "name.json"
DATA "layername"

Step 1: Use ogrinfo to examine

Using ogrinfo on the GeoJSON file:

> ogrinfo countries.json

INFO: Open of `countries.json'
      using driver `GeoJSON' successful.
1: countries (Multi Polygon)

Using ogrinfo to examine the structure of the countries layer:

> ogrinfo countries.json countries -summary

INFO: Open of `countries.json'
      using driver `GeoJSON' successful.

Layer name: countries
Geometry: Multi Polygon
Feature Count: 177
Extent: (-180.000000, -90.000000) - (180.000000, 83.645130)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
featurecla: String (0.0)
scalerank: Integer (0.0)
labelrank: Integer (0.0)
sovereignt: String (0.0)
sov_a3: String (0.0)
adm0_dif: Integer (0.0)
level: Integer (0.0)
type: String (0.0)
admin: String (0.0)
adm0_a3: String (0.0)
...

Step 2: Add the layer in your mapfile

For OGR connections, it is always recommended to set CONNECTIONTYPE, CONNECTION, and DATA, as follows:

/* Countries */
LAYER
  NAME "countries"
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "countries.json"
  DATA "countries"              # the OGR layername, found through ogrinfo
  CLASS
    NAME "World Countries"
    STYLE
      COLOR 200 200 200
      OUTLINECOLOR 0 0 0
      WIDTH 0.1
    END #style
  END #class
END #layer

Step 3: Test your Mapfile with map2img

Use the MapServer commandline utility map2img to verify that your mapfile creates a valid map image, and also display draw times, such as:

map2img -m geojson.map -o ttt.png -map_debug 3

msDrawMap(): rendering using outputformat named png (AGG/PNG).
msDrawMap(): WMS/WFS set-up and query, 0.000s
msDrawMap(): Layer 0 (countries), 0.091s
msDrawMap(): Drawing Label Cache, 0.000s
msDrawMap() total time: 0.091s
msSaveImage(ttt.png) total time: 0.007s
../../_images/countries-map2img.png

Astuce

MS4W users will notice that the demo mapfile that is part of every installation (at /ms4w/apps/local-demo/local.map) is configured to output GeoJSON.