MapServer Python MapScript bindings
Project description
- Last Updated:
2018-08-17
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.
Language agnostic documentation is available at http://mapserver.org/mapscript/introduction.html
Python specific documentation is available at http://mapserver.org/mapscript/python.html
For working with Mapfiles in Python the mappyfile project is also available, this allows creating, parsing, formatting, and validating Mapfiles.
Wheels and PyPI
Python wheels have been created for Windows and uploaded to PyPI - the Python Package Index. This allows easy installation using pip. Other advantages of ready-made wheels on PyPI are:
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.2
Python 3.6 x64 for MapServer 7.2
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.
MapServer Versions
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%
Window binary packages can be downloaded from GIS Internals <https://www.gisinternals.com/stable.php>. To ensure compatibility with the wheels, please use release-1911-x64-gdal-2-3-mapserver-7-2.
When using these packages the MapServer path will be similar to C:release-1911-x64-gdal-2-3-mapserver-7-2bin.
The mapscript wheels have been compiled using Visual Studio 2017 version 15.3 (MSVC++ 14.11 _MSC_VER == 1911).
Installation
Prior to installing it is first recommended to update pip to the latest version with the following command:
python -m pip install --upgrade pip
Next 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
Add your MapSever binaries folder to your system path (see MapServer Versions):
SET PATH=C:\MapServer\bin;%PATH%
Now you should be able to import mapscript:
python -c "import mapscript;print(mapscript.msGetVersion())"
MapServer version 7.2.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.
Quickstart
Prior to 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")
>>> e = test_map.extent
Create a layer from a string:
>>> import mapscript
>>> lo = mapscript.fromstring("""LAYER NAME "test" TYPE POINT END""")
>>> lo
<mapscript.layerObj; proxy of C layerObj instance at ...>
>>> lo.name
'test'
>>> lo.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.
Prior to the switch to using CMake to build 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.
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 Windows.
CMakeLists.txt is configured so that all files required to make a Python wheel are copied into the output build folder. The wheel can then be built with the following commands.
python -m pip install --upgrade pip
pip install wheel
cd C:\Projects\MapServer\build\mapscript\python
python setup.py bdist_wheel
SWIG can 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:
swig -python -shadow -modern -templatereduce -fastdispatch -fvirtual -fastproxy
-modernargs -castmode -dirvtable -fastinit -fastquery -noproxydel -nobuildnone
-o mapscript_wrap.c ../mapscript.i
Testing
Once the mapscript module has been built there is a test suite to check the output. It is recommended pytest is used to run the tests. This can be installed using:
pip install pytest
Change the directory to the mapscript output build folder and run the command below. Some tests are currently excluded, these will be fixed for upcoming releases. It is also planned to include the test suite in the Python wheels to allow easy testing of a mapscript installation.
python -m pytest --ignore=tests/cases/fonttest.py --ignore=tests/cases/hashtest.py --ignore=tests/cases/pgtest.py --ignore=tests/cases/threadtest.py tests/cases
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for mapscript-7.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cd80d479b54ffe64b7962771e85db0f502351c83c7ea7a5494a84900590fe07 |
|
MD5 | 916b29a5f9a175b2b60851beabdd2118 |
|
BLAKE2b-256 | e4e3a2726e8b6b8c8c22e1ca015380fa0c04c60450f714e208b42982bda63c70 |
Hashes for mapscript-7.2-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dd45d2f4f1aae9a8fb7d770cb37ab0269dbbb0c8440f50f4f26a870d64f04a9 |
|
MD5 | 0d9e25b662ac738ac45810c1eb6f7779 |
|
BLAKE2b-256 | 29a41b2052c2c8d8246b2c5e54177e4bd48887eacec78af1b5cfde8bd9f732ba |