Skip to main content

MapServer Python MapScript bindings

Project description

Author:

MapServer Team

Last Updated:

2019-06-04

Introduction

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

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.

For working with Mapfiles in Python the mappyfile project is also available, this allows creating, parsing, formatting, and validating Mapfiles without any dependencies on MapServer.

Wheels and PyPI

Python wheels have been created for Windows and uploaded to PyPI - the Python Package Index. 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

Currently the following wheels are built:

  • Python 2.7 x64 for MapServer 7.4.0

  • Python 3.6 x64 for MapServer 7.4.0

The mapscript wheels have been compiled using Visual Studio 2017 version 15.3 (MSVC++ 14.11 _MSC_VER == 1911). Linux wheels are also planned, 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. On Windows you can use the following, replacing C:\MapServer\bin with the location of your MapServer binaries.

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

Windows binary packages can be downloaded from GIS Internals. To ensure compatibility with the wheels, please use identical release packages, e.g. release-1911-x64-gdal-2-3-mapserver-7-4 for mapscript 7.4.

When using these packages the MapServer path will be similar to C:\release-1911-x64-gdal-2-3-mapserver-7-2\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 7.4.0 OUTPUT=PNG OUTPUT=JPEG OUTPUT=KML 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=SOS_SERVER SUPPORTS=FASTCGI SUPPORTS=THREADS SUPPORTS=GEOS SUPPORTS=PBF INPUT=JPEG INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE

If you failed to add the MapServer binaries to your system path you may see one of the following errors:

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

If your version of mapscript does not match your version of MapServer you may instead one of the following messages:

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

Installation on Unix

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

sudo apt-get install python-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}

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 pip install virtualenv
virtualenv 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-3.0.12;%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_LIB variable has been set as this is required for many of the tests.

SET PATH=C:\release-1911-x64-gdal-2-3-mapserver-7-4\bin;%PATH%
SET PROJ_LIB=C:\release-1911-x64-gdal-2-3-mapserver-7-4\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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

mapscript-7.4.0-cp37-cp37m-win_amd64.whl (420.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

mapscript-7.4.0-cp37-cp37m-win32.whl (338.7 kB view details)

Uploaded CPython 3.7mWindows x86

mapscript-7.4.0-cp36-cp36m-win_amd64.whl (423.2 kB view details)

Uploaded CPython 3.6mWindows x86-64

mapscript-7.4.0-cp27-cp27m-win_amd64.whl (420.1 kB view details)

Uploaded CPython 2.7mWindows x86-64

File details

Details for the file mapscript-7.4.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: mapscript-7.4.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 420.8 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for mapscript-7.4.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 8bf50ca5a205f34a15101bf680359661a596f00bbde7d1fb57a07af75a632509
MD5 4d83daf4015a1cb1389db4df64535eb5
BLAKE2b-256 231f48299e6561e24ff4efbaf115219075067fcf28b57e03414b7b0897d33543

See more details on using hashes here.

File details

Details for the file mapscript-7.4.0-cp37-cp37m-win32.whl.

File metadata

  • Download URL: mapscript-7.4.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 338.7 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.8

File hashes

Hashes for mapscript-7.4.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 27ed0e0e3400a34edf6e262f1497f1cb2620327e66fd0cfa78dfa2a64300dcb4
MD5 64c617030ea1ba1dcf307693c19bc3ba
BLAKE2b-256 e93f78fe0edd47d222b9101ea05555305497a6042efe31da1ee5f2794cfd8aef

See more details on using hashes here.

File details

Details for the file mapscript-7.4.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: mapscript-7.4.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 423.2 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.8

File hashes

Hashes for mapscript-7.4.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 d1e4f6f73fa3c472dfc81395445f1121ce80bbbfc10aaf00aa377f9b31c472ef
MD5 57247f9ce7062c21b3114fd33afcf566
BLAKE2b-256 598cb924d6e92f696ff64dc2ebcd99822ec7fbc9d7053d64ef876d56956c0c23

See more details on using hashes here.

File details

Details for the file mapscript-7.4.0-cp27-cp27m-win_amd64.whl.

File metadata

  • Download URL: mapscript-7.4.0-cp27-cp27m-win_amd64.whl
  • Upload date:
  • Size: 420.1 kB
  • Tags: CPython 2.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for mapscript-7.4.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 3fb3bf6088f60e5b21aa953ab3f4c6e56e7cff345615a001ab0650ca21351317
MD5 adf31f5a32ef2d18a2e5ddc3e46f281c
BLAKE2b-256 1f267f6a680833620429369d726ba866f4a5f3b981f1d41a15db697f1ffcaa89

See more details on using hashes here.

Supported by

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