A pure Python parser for working with MapServer MapFiles, built using Lark
mappyfile is used for formatting and validation in https://app.mapserverstudio.net/, and can be tested for free on any of your Mapfiles. If you find mappyfile useful please consider signing up for a professional account at https://mapserverstudio.net/. This will help to fund maintenance and further development of both mappyfile and MapServer.
A presentation on mappyfile was given at FOSS4G Europe 2017 - slides are available for download here.
- Python Python 3.8 or higher
mappyfile is available on PyPI (the Python Package Index), and can be installed using pip:
pip install mappyfile
This will also install its required dependencies - Lark, and jsonschema.
Full documentation is available at http://mappyfile.readthedocs.io/en/latest/
From within Python scripts:
import mappyfile
mapfile = mappyfile.open("./docs/examples/raster.map")
# update the map name
mapfile["name"] = "MyNewMap"
new_layer_string = """
LAYER
NAME 'land'
TYPE POLYGON
DATA '../data/vector/naturalearth/ne_110m_land'
CLASS
STYLE
COLOR 107 208 107
OUTLINECOLOR 2 2 2
WIDTH 1
END
END
END
"""
layers = mapfile["layers"]
new_layer = mappyfile.loads(new_layer_string)
layers.insert(0, new_layer) # insert the new layer at any index in the Mapfile
for l in layers:
print("{} {}".format(l["name"], l["type"]))
print(mappyfile.dumps(mapfile, indent=1, spacer="\t"))
Three command line tools are available - format
, validate
, and schema
:
mappyfile format raster.map formatted_raster.map
mappyfile validate D:\ms-ogc-workshop\ms4w\apps\ms-ogc-workshop\**\*.map
mappyfile schema mapfile-schema-7-6.json --version=7.6
- Seth Girvin @geographika
- Erez Shinan @erezsh
- Julien Enselme @jenselme
- Loïc Gasser @loicgasser
- Ian Turton @ianturton
- @thorag76