Skip to main content

MapServer Python MapScript bindings

Project description

Author:

MapServer Team

Last Updated:

2024-03-29

Introduction

The Python mapscript module provides users an interface to MapServer classes on any platform, and has been tested on Python 3.8+.

The Python mapscript module is created using SWIG the the Simplified Wrapper and Interface Generator. This is used to create MapServer bindings in many different programming languages.

When working with Mapfiles in Python the mappyfile project can also be used, this allows creating, parsing, formatting, and validating Mapfiles without any dependencies on MapServer.

Wheels and PyPI

Python wheels for Windows are automatically and uploaded to PyPI - the Python Package Index on each MapServer release. Note - MapServer binaries still need to be installed on the system, and are not included in the wheel itself, see the Installation section below.

Advantages of ready-made wheels on PyPI include:

  • easy installation using pip

  • mapscript can be added as a dependency to Requirements Files

  • mapscript can be easily added to a Python Virtual Environment

  • Python2 or Python3 versions of mapscript can be installed and work with a single installation of MapServer

Wheels are built based on the Appveyor build environments. These are as follows at the time of writing:

  • Python 3.9 x64

  • Python 3.10 x64

  • Python 3.11 x64

  • Python 3.12 x64

  • Python 3.13 x64

The mapscript wheels have been compiled using Visual Studio 2022 version 17 (MSVC++ 17.9 _MSC_VER == 1939). Linux Wheels may also be available in the future using the manylinux project.

No source distributions will be provided on PyPI - to build from source requires the full MapServer source code, in which case it is easiest to take a copy of the full MapServer project and run the CMake process detailed below.

The Wheels contain a full test suite and sample data that can be run to check that the installed MapServer is running correctly.

Installation on Windows

To use mapscript you will need to add the MapServer binaries to your system path.

For Python 3.8+

As of Python 3.8 PATH and the current working directory are no longer used when searching for the MapServer DLLs. A new environment variable MAPSERVER_DLL_PATH has been introduced to set the location of the MapServer DLLs. On Windows you can use the following, replacing C:\MapServer\bin with the location of your MapServer binaries.

SET MAPSERVER_DLL_PATH=C:\MapServer\bin

If several folders are required (e.g. GDAL DLLs) multiple paths can be provided separated by semi-colons:

SET MAPSERVER_DLL_PATH=C:\MapServer\bin;C:\GDAL\bin

In PowerShell you can set this as follows:

$env:MAPSERVER_DLL_PATH="C:\MapServer\bin"

For Earlier Python Versions

For Python 3.7 and earlier (including Python 2.7) you can use either the MAPSERVER_DLL_PATH variable documented above, or the system PATH variable as below, replacing C:\MapServer\bin with the location of your MapServer binaries.

SET PATH=C:\MapServer\bin;%PATH%

Windows Binaries

Windows binary packages can be downloaded from GIS Internals. To ensure compatibility with the wheels, please use identical release packages, e.g. release-1930-x64-gdal-3-8-4-mapserver-8-0-1 for mapscript 8.0.1.

When using these packages the MapServer path will be similar to C:\release-1930-x64-gdal-3-8-4-mapserver-8-0-1\bin.

Prior to installing mapscript it is recommended to update pip to the latest version with the following command:

python -m pip install --upgrade pip

If there are binary wheels available for your system, mapscript can be installed using:

pip install mapscript

If you already have mapscript installed and wish to upgrade it to a newer version you can use:

pip install mapscript --upgrade

Now you should be able to import mapscript:

python -c "import mapscript;print(mapscript.msGetVersion())"
MapServer version 8.0.1 PROJ version 9.3 GDAL version 3.9 OUTPUT=PNG OUTPUT=JPEG SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=CAIRO SUPPORTS=SVG_SYMBOLS SUPPORTS=SVGCAIRO SUPPORTS=ICONV SUPPORTS=FRIBIDI SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT SUPPORTS=WCS_SERVER SUPPORTS=OGCAPI_SERVER SUPPORTS=FASTCGI SUPPORTS=THREADS SUPPORTS=GEOS SUPPORTS=PBF INPUT=JPEG INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE INPUT=FLATGEOBUF

Installation on Unix

For Unix users there are two approaches to installing mapscript. The first is to install the python3-mapscript package using a package manager. For example on Ubuntu the following command can be used:

sudo apt-get install python3-mapscript

The second approach is to build and install the Python mapscript module from source. Full details on compiling MapServer from source are detailed on the Compiling on Unix page. To make sure Python mapscript is built alongside MapServer the following flag needs to be set:

-DWITH_PYTHON=ON

To configure the path of the mapscript installation location -DCMAKE_INSTALL_PREFIX can be set, e.g.

sudo cmake .. -DCMAKE_INSTALL_PREFIX=/usr

When installing the DESTDIR variable can be set (note DESTDIR is not used on Windows) to install mapscript to a non-default location. E.g.

make install DESTDIR=/tmp

In summary the install target runs the setup.py install command using custom paths (when set) similar to below:

python setup.py install –root=${DESTDIR} –prefix={CMAKE_INSTALL_PREFIX}

Installation Troubleshooting

If the _mapscript.pyd (or _mapscript.so on Unix) is missing from the Lib/site-packages/mapscript folder (which can happen if the source installation is installed rather than a pre-compiled version) the following error will occur:

ImportError: cannot import name '_mapscript' from partially initialized module 'mapscript' (most likely due to a circular import)

If the mapscript library is not on your PYTHONPATH you may see one of the following errors:

ModuleNotFoundError: No module named '_mapscript' # Python 3.x

If the MapServer.dll cannot be found in your system paths (or MAPSERVER_DLL_PATH environment variable when using Python 3.8 or higher on Windows) you will see the following message:

ImportError: DLL load failed: The specified module could not be found.

If MapServer has been built with a dependency also used by Python, and the versions don’t match you may see the error below.

ImportError: DLL load failed: The specified procedure could not be found.

This is a particular problem on Windows with sqlite3.dll as it is used by both Python and MapServer. Copying the sqlite3.dll from the MapServer binaries folder alongside _mapscript.pyd in Lib/site-packages/mapscript can resolve this.

Another common cause is if the Python environment contains multiple versions of the GEOS binary. For example geos_c.dll is included as part of the Shapely Python library, as well as a MapServer installation.

If you are using 32 bit Python on Windows and attempt to use a 64 bit version of MapScript the following import error will occur:

ImportError: DLL load failed while importing _mapscript: %1 is not a valid Win32 application.

Quickstart

Some basic examples of what can be done with mapscript are shown below. Note - before running any scripts using mapscript, you will need to add the MapServer binaries to your system path, see the Installation section above.

To open an existing Mapfile:

>>> import mapscript
>>> test_map = mapscript.mapObj(r"C:\Maps\mymap.map")
>>> extent = test_map.extent

Create a layer from a string:

>>> import mapscript
>>> layer = mapscript.fromstring("""LAYER NAME "test" TYPE POINT END""")
>>> layer
<mapscript.layerObj; proxy of C layerObj instance at ...>
>>> layer.name
'test'
>>> layer.type == mapscript.MS_LAYER_POINT
True

Building the Mapscript Module

The mapscript module is built as part of the MapServer CMake build process. This is configured using the mapserver/mapscript/CMakeLists.txt file.

Before the switch to CMake MapServer mapscript was built using distutils and setup.py. Now the setup.py.in file is used as a template that is filled with the MapServer version number and used to created wheel files for distribution, or install mapscript directly on the build machine.

The build process works as follows.

  • CMake runs SWIG. This uses the SWIG interface files to create a mapscriptPYTHON_wrap.c file, and a mapscript.py file containing the Python wrapper to the mapscript binary module.

  • CMake then uses the appropriate compiler on the system to compile the mapscriptPYTHON_wrap.c file into a Python binary module - _mapscript.pyd file on Windows, and a _mapscript.so file on Unix.

CMakeLists.txt is configured with a pythonmapscript-wheel target that copies all the required files to the output build folder where they are then packaged into a Python wheel. The wheel can be built using the following command:

cmake --build . --target pythonmapscript-wheel

The pythonmapscript-wheel target creates a virtual environment, creates the Python wheel, installs it to the virtual environment and finally runs the test suite. This process runs commands similar to the following:

python -m venv mapscriptvenv
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
python setup.py bdist_wheel
pip install --no-index --find-links=dist mapscript
python -m pytest --pyargs mapscript.tests

SWIG can also be run manually, without using CMake. This may allow further optimizations and control on the output.

cd C:\Projects\mapserver\build
SET PATH=C:\MapServerBuild\swigwin-4.0.1;%PATH%
swig -python -shadow -o mapscript_wrap.c ../mapscript.i

SWIG has several command line options to control the output, examples of which are shown below:

swig -python -shadow -modern -templatereduce -fastdispatch -fvirtual -fastproxy
-modernargs -castmode -dirvtable -fastinit -fastquery -noproxydel -nobuildnone
-o mapscript_wrap.c ../mapscript.i

Testing

The mapscript module includes a test suite and a small sample dataset to check the output and MapServer installation. It is recommended pytest is used to run the tests. This can be installed using:

pip install pytest

Make sure the MapServer binaries are on the system path, and that the PROJ_DATA variable has been set as this is required for many of the tests.

SET PATH=C:\release-1930-x64-gdal-3-8-4-mapserver-8-0-1\bin;%PATH%
SET PROJ_DATA=C:\release-1930-x64-gdal-3-8-4-mapserver-8-0-1\bin\proj\SHARE

Finally run the command below to run the test suite:

pytest --pyargs mapscript.tests

Credits

  • Steve Lime (developer)

  • Sean Gillies (developer)

  • Frank Warmerdam (developer)

  • Howard Butler (developer)

  • Norman Vine (cygwin and distutils guru)

  • Tim Cera (install)

  • Michael Schultz (documentation)

  • Thomas Bonfort (developer)

  • Even Rouault (developer)

  • Seth Girvin (Python3 migration, documentation and builds)

  • Claude Paroz (Python3 migration)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mapscript-8.6.0.tar.gz (441.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mapscript-8.6.0-cp314-cp314-win_amd64.whl (480.1 kB view details)

Uploaded CPython 3.14Windows x86-64

mapscript-8.6.0-cp313-cp313-win_amd64.whl (473.0 kB view details)

Uploaded CPython 3.13Windows x86-64

mapscript-8.6.0-cp312-cp312-win_amd64.whl (473.6 kB view details)

Uploaded CPython 3.12Windows x86-64

mapscript-8.6.0-cp311-cp311-win_amd64.whl (470.4 kB view details)

Uploaded CPython 3.11Windows x86-64

mapscript-8.6.0-cp310-cp310-win_amd64.whl (470.3 kB view details)

Uploaded CPython 3.10Windows x86-64

mapscript-8.6.0-cp39-cp39-win_amd64.whl (470.9 kB view details)

Uploaded CPython 3.9Windows x86-64

mapscript-8.6.0-cp38-cp38-win_amd64.whl (470.7 kB view details)

Uploaded CPython 3.8Windows x86-64

File details

Details for the file mapscript-8.6.0.tar.gz.

File metadata

  • Download URL: mapscript-8.6.0.tar.gz
  • Upload date:
  • Size: 441.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0.tar.gz
Algorithm Hash digest
SHA256 2d213bed02df67478f89d44f47ee9833774459651b4343b55b14a3062641b02b
MD5 e59a87fc571e07452241d827b0cc51a8
BLAKE2b-256 09a7a10bdfc55e5d59593e75fdff0d4d358276aa6217bb3c2a35e7b5da5e1154

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 480.1 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ef2fa0b58b6a55691f24744e9e01e76a7a0a8115efb22ad13d7a804b26d2bf8d
MD5 ffb507581783cce796001c49e36f4f46
BLAKE2b-256 7f3bd4474d1817d381d5140f62d3612f008ef014e305927186e3ea7d64eabdd9

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 473.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 53a2727ea66c912a320598c620e964a33b5930acb2240ea340b693630221e390
MD5 dd7e5d66672c781c272361eeefe6007e
BLAKE2b-256 c6a062c01c7eddb61b5c0372f22093b70e9155eb526f0ee73b07704ce91c5ba3

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 473.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 40c9ef0136897dcf5ea2ca62e8cd4a313a63552bec94c90fb7acba0dc50d2d8c
MD5 345c284dbb4286a0c7af5e5e621fdb2c
BLAKE2b-256 87421099b77e0fe1bf5446f979219a47a242e5794ea181b6beae023482920a38

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 470.4 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 32746a659a9c5164151c8469b16a2f131762952a6d4147dddee25c2a5ada5648
MD5 d73a5eb15736ba96c16c6ff4f8ce6780
BLAKE2b-256 9ccb31aae97d068a640d51356ecf37f219ce012c52da45e228d4b3371f70f2ce

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 470.3 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b088ffb01abace1c49fe2aed584a72fe817ebfb93f2a278d1c90aae64e0a6c58
MD5 60dcc96d7f671dc224091fd00cd396d3
BLAKE2b-256 ca334ca4e6fa937637664f24031d3e2142e9fdda96bf174e4d1b2129580c4949

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 470.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 30cbcb740680f6ffc1bb0cad1673bfeee903e333a8c04833a93632b24cf5484f
MD5 9123fcf81135069e8f19be5f0e7f7f78
BLAKE2b-256 988dac0dc658dfe04177c960df7d5404f3a279f7623201b30c05f4793c58aa16

See more details on using hashes here.

File details

Details for the file mapscript-8.6.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: mapscript-8.6.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 470.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mapscript-8.6.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f8f71e5d3e40c71499ceb67d2f0bfba9af4b1a0e6e5e8df11d647e6d0cb416be
MD5 1f4296ceb3abfbb4f27be06d75b2219d
BLAKE2b-256 f3c15f51470f2a6594bcb361b4bd23b042654b36c0a4ac86e981f0bb17752a9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page