3. Pretty PrintingΒΆ

Taking an input similar to 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

The following code will output a nicely formatted version.

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

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

Notes:

  • Comments will be removed
  • Quotes will be standardised
  • Indentation can be set as an option e.g. 2 or 4 spaces
  • Any INCLUDE directives will have been parsed and treated as part of the original Mapfile so there will never be any INCLUDE keywords in the output