sethg at geographika.co.uk
- Last Updated:
The MapScript HowTo docs are intended to complement the API reference with
examples of usage for specific subjects. All examples in this document refer
to the Mapfile and testing layers distributed with MapServer and found in source control
The examples below are written in Python. All MapScript languages use the same API, but will need to be rewritten using the relevant language’s syntax.
By “Mapfile” here, I mean all the elements that can occur in (nearly) arbitrary numbers within a MapScript mapObj: Layers, Classes, and Styles.
The mapfile path argument to the
mapObj constructor is optional. The following
generates a default mapObj with no layers.
empty_map = mapscript.mapObj()
mapObj is initialized from a Mapfile on disk in the usual manner:
test_map = mapscript.mapObj('tests/test.map')
If you receive the following error
msProcessProjection(): Projection library error.
proj error "No such file or directory" for "init=epsg:4326" then make sure you have
PROJ_LIB environment variable correctly
clone_map = test_map.clone()
the Java MapScript module implements a “cloneMap” method to avoid conflict with the clone method of Java’s Object class.
mapObj can be saved to disk using the save method:
There is a common parent/child object API for Layers, Classes, and Styles in MapServer.
Referencing a Child¶
References to Layer, Class, and Style children are obtained by “getChild”-like methods of their parent:
i, j, k = 1, 0, 0 layer_i = test_map.getLayer(i) class_ij = layer_i.getClass(j) style_ijk = class_ij.getStyle(k)
These references are for convenience only. MapScript doesn’t have any
reference counting, and you are certain to run into trouble if you try to use
these references after the parent
mapObj has been deleted and freed from
Cloning a Child¶
clone_layer = layer_i.clone()
This instance has no parent, and is self-owned.
new_layer = mapscript.layerObj() new_class = mapscript.classObj() new_style = mapscript.styleObj()
They are added to a parent object using “insertChild”-like methods of the parent which returns the index at which the child was inserted:
li = test_map.insertLayer(new_layer) ci = test_map.getLayer(li).insertClass(new_class) si = test_map.getLayer(li).getClass(ci).insertStyle(new_style)
The insert* methods create a completely new copy of the object and stores it in the parent with all ownership taken on by the parent. See the SWIG API reference for more details.
The old style child object constructors with the parent object as a single argument remain in MapServer:
new_layer = mapscript.layerObj(test_map) new_class = mapscript.classObj(new_layer) new_style = mapscript.styleObj(new_class)
Child objects can be removed with “removeChild”-like methods of parents, which return independent copies of the removed object:
# following from the insertion example ... # remove the inserted style, returns a copy of the original new_style removed_style = test_map.getLayer(li).getClass(ci).removeStyle(si) removed_class = test_map.getLayer(li).removeClass(ci) removed_layer = test_map.removeLayer(li)
Map, Layer, and Class metadata are the other arbitrarily numbered elements (well, up to the built-in limit of 41) of a mapfile.
layer = test_map.getLayerByName('POLYGON') layer.metadata.set('wms_name', 'foo') name = layer.metadata.get('wms_name') # returns 'foo'
You can iterate over all keys in a class:hashTableObj as follows:
key = None while (1): key = layer.metadata.nextKey(key) if key == None: break value = layer.metadata.get(key) print(key, value)