3. Pretty PrintingΒΆ

Please note this page is currently a draft and subject to further updates.

A Mapfile with inconsistent formatting such as the example below:

map
/**
Some comments
*/
            EXTENT -180 -90 180 90    
    NAME "MyMap"
    WEB
metadata
'wms_enable_request'  '*'
'wms_feature_info_mime_type' 'text/html' # this is required to return the content-type for GetFeatureInfo requests       
END
    END

PROJECTION
"init=epsg:4326"
END
 # START OF THE LAYER DEFINITION
    LAYER
    PROCESSING 'BANDS=1'
        PROCESSING 'CONTOUR_ITEM=elevation'
    PROCESSING 'CONTOUR_INTERVAL=20'
     EXTENT -180 -90 180 90 # set this here as it is not stored in the image
     NAME "rgb"
        TYPE RASTER
     DATA '../data/raster/bluemarble.tif' 
     TEMPLATE 'raster.template.html'    
    END    
END

Can be converted using mappyfile to a nicely formatted version using the code below

import mappyfile
mf = mappyfile.load("./docs/examples/before.map")
mappyfile.write(mf, "./docs/examples/after.map")

The result:

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

Warning

When standardising quotes be careful that no quotes chosen for formatting are found within string values. For example large DATA blocks of SQL may contain single quotes, that would then create an invalid Mapfile.

Notes:

  • Comments will be removed
  • Quotes will be standardised
  • Indentation can be set as an option e.g. 2 or 4 spaces