Python Examples
Examples of how to use Python MapScript
Introduction
Introductory stuff.
Exception Handling
The Python mapscript module maps a few MapServer errors into Python exceptions. Attempting to load a non-existent mapfile raises an 'IOError', for example
>>> import mapscript
>>> mapfile = '/no/such/file.map'
>>> m = mapscript.mapObj(mapfile)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/site-packages/mapscript.py", line 799, in __init__
newobj = _mapscript.new_mapObj(*args)
IOError: msLoadMap(): Unable to access file. (/no/such/file.map)
>>>
The message of the error is written by 'msSetError' and so is the same message that CGI mapserv users see in error logs.
This How-to applies to: Any version.
use mapscript to generate image
#read mapfile
mf_path = "D:\\ms4w\\Apache\\htdocs\\webgis\\global.map"
mf = mapscript.mapObj(mf_path)
print mf.extent
#draw the scale, legend
img = mf.drawLegend()
img.save("D:\\mapserver_win32\\legend.gif")
print "lengend.gif saved"
img = mf.drawScalebar()
img.save("D:\\mapserver_win32\\scale.gif")
print "scale.gif saved"
print img.format.name
img = mf.draw()
img.save("D:\\mapserver_win32\\map.gif")
print "scale.gif saved"