Skip to content

MapScript

Warning

This page is currently in a draft form.

Overview

MapServer has its own scripting language - MapScript. MapScript is available in several programming languages including Python, PHP, and Perl. They all share a common MapScript API.

Adding MapScript to the Docker Container

MapScript is not installed on the MapServer Docker image by default, but it can be added using the approach below.

Tip

The MapScript package must match the version of MapServer installed on the Docker image or segmentation faults will occur.

# start an interactive session with the MapServer Docker container
docker exec -it mapserver /bin/bash

# install MapScript via a downloaded Debian package available in the workshop repository
dpkg -i /scripts/python3-mapscript_8.0.1-1~jammy2_amd64.deb

# test that we can import MapScript successfully
python -c "import mapscript;print(mapscript.msGetVersion())"

Example Script

One use of MapScript is to help with writing Mapfiles by getting information from its data sources. Some examples are provided below.

Reading Data Extents

"""
python /scripts/extents.py
"""

import mapscript

mapfile = "/etc/mapserver/lakes.map"
m = mapscript.mapObj(mapfile)

lyr = m.getLayerByName("lakes")
extent = lyr.getExtent()

original_projection_code = m.getProjection()
original_projection = mapscript.projectionObj(original_projection_code)

webmercator = mapscript.projectionObj("epsg:3857")

extent_string = f"[{extent.minx}, {extent.miny}, {extent.maxx}, {extent.maxy}]"
print(extent_string)

# reprojection is done in-place
extent.project(original_projection, webmercator)

extent_string = f"[{extent.minx}, {extent.miny}, {extent.maxx}, {extent.maxy}]"
print(extent_string)

center = f"[{(extent.maxx + extent.minx) / 2}, {(extent.maxy + extent.miny) / 2}]"
print(center)

print("Done!")