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 4.2+ and found under mapserver/tests.
All examples will use a pseudocode that is consistent with the language independent API reference. Each line is a statement. For object attributes and methods we use the dot, „.“, operator. Creation and deletion of objects will be indicated by „new“ and „del“ keywords. Other than that, the pseudocode looks a lot like Python.
By «Mapfile» here, I mean all the elements that can occur in (nearly) arbitrary numbers within a MapScript mapObj: Layers, Classes, and Styles. MapServer 4.4 has greatly improved capability to manipulate these objects.
An instance of mapObj is a parent for zero to many layerObj children.
The mapfile path argument to the mapscript.mapObj constructor is now optional
empty_map = new mapscript.mapObj
generates a default mapObj with no layers. A mapObj is initialized from a mapfile on disk in the usual manner:
test_map = new mapscript.mapObj('tests/test.map')
An independent copy, less result and label caches, of a mapObj can be produced by the new mapObj.clone() method:
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.
A mapObj can be saved to disk using the save method:
Frankly, the msSaveMap() function which is the foundation for mapObj::save is incomplete. Your mileage may vary.
There is a common parent/child object API for Layers, Classes, and Styles in MapServer 4.4.
Referencing a Child¶
References to Layer, Class, and Style children are obtained by «getChild»-like methods of their parent:
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 memory.
Cloning a Child¶
A completely independent Layer, Class, or Style can be created using the clone method of layerObj, classObj, and styleObj:
clone_layer = layer_i.clone()
This instance has no parent, and is self-owned.
Uninitialized instances of layerObj, classObj, or styleObj can be created with the new constructors:
new_layer = new mapscript.layerObj new_class = new mapscript.classObj new_style = new mapscript.styleObj
and 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 store it in the parent with all ownership taken on by the parent.
see the API reference for more details.
The old style child object constructors with the parent object as a single argument:
new_layer = new mapscript.layerObj(test_map) new_class = new mapscript.classObj(new_layer) new_style = new mapscript.styleObj(new_class)
remain in MapServer 4.4.
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.
In MapServer 4.4, the metadata attributes of mapObj.web, layerObj, and classObj are instances of hashTableObj, a class which functions like a limited dictionary
layer.metadata.set('wms_name', 'foo') name = layer.metadata.get('wms_name') # returns 'foo'
You can iterate over all keys in a hashTableObj like
key = NULL while (1): key = layer.metadata.nextKey(key) if key == NULL: break value = layer.metadata.get(key) ...
See the API Reference (mapscript.txt) for more details.
Backwards Compatibility for Metadata¶
The old getMetaData and setMetaData methods of mapObj, layerObj, and classObj remain for use by older programs.