2. TransformingΒΆ

Once mappyfile has parsed a Mapfile, it uses a transformer to transform it into a dict object. This allows a familiar API to edit the Mapfile further.

For example taking the Mapfile below:

MAP
    EXTENT -180 -90 180 90
    NAME 'MyMap'
    WEB
        METADATA
            'wms_enable_request' '*'
            'wms_feature_info_mime_type' 'text/html'
        END
    END
    PROJECTION
        "init=epsg:4326"
    END
    LAYER
        PROCESSING 'BANDS=1'
        PROCESSING 'CONTOUR_ITEM=elevation'
        PROCESSING 'CONTOUR_INTERVAL=20'
        EXTENT -180 -90 180 90
        NAME 'rgb'
        TYPE RASTER
        DATA '../data/raster/bluemarble.tif'
        TEMPLATE 'raster.template.html'
    END
END

The following code can be used to see the dictionary structure (represented here as a JSON object):

import mappyfile
import json
mf = mappyfile.load("./docs/examples/after.map")

with open("./docs/examples/sample.json", "w") as f:
    json.dump(mf, f, indent=4)

Output:

{
    "extent": [
        -180, 
        -90, 
        180, 
        90
    ], 
    "name": "'MyMap'", 
    "web": {
        "metadata": {
            "'wms_enable_request'": "'*'", 
            "'wms_feature_info_mime_type'": "'text/html'"
        }, 
        "__type__": "web"
    }, 
    "projections": [
        [
            "\"init=epsg:4326\""
        ]
    ], 
    "layers": [
        {
            "processing": [
                "'BANDS=1'", 
                "'CONTOUR_ITEM=elevation'", 
                "'CONTOUR_INTERVAL=20'"
            ], 
            "extent": [
                -180, 
                -90, 
                180, 
                90
            ], 
            "name": "'rgb'", 
            "type": "RASTER", 
            "data": "'../data/raster/bluemarble.tif'", 
            "template": "'raster.template.html'", 
            "__type__": "layer"
        }
    ], 
    "__type__": "map"
}

Notes on the above:

  • Objects that can have multiple instances in a Mapfile will be stored in lists (order is important).

  • Most objects have a set of key/value pairs. PROJECTION however is treated as a list (see http://www.mapserver.org/mapfile/projection.html).

  • Some keys are already quoted e.g. in the METADATA object items such as “wms_enable_request” are strings rather than keywords.

  • Some keys are duplicated within an object. E.g.

    PROCESSING "BANDS=1"
    PROCESSING "CONTOUR_ITEM=elevation"
    PROCESSING "CONTOUR_INTERVAL=20"
    

    These are turned into lists:

    "processing": [
        "'BANDS=1'",
        "'CONTOUR_ITEM=elevation'",
        "'CONTOUR_INTERVAL=20'"
    ],