Quick MapServer Extents Calculation

MapServer's scripting module allows users access to the program's projection capabilities. One of these is particularly useful for quick calculation of map extents. Let's say you have a world-wide dataset with a lat/long WGS84 coordinate system, and want to render it onto a Mollweide projection map. Start your interpreter, import the mapscript module, and create output and input projection objects. For the input I use the EPSG shorthand for a lat/long WGS84 coordinate system. Initialize a rectangle to the full extent of the world in units of the input projection, and transform it to output units using its project() method:

>>> import mapscript
>>> proj_out = mapscript.projectionObj('+proj=moll +lon_0=0.0')
>>> proj_in = mapscript.projectionObj('+init=epsg:4326')
>>> e = mapscript.rectObj(-180,-90,180,90)
>>> e.project(proj_in, proj_out)
>>> print e.minx, e.miny, e.maxx, e.maxy
-18040095.6961 -9020047.84807 18040095.6961 9020047.84807

The output values are ready to be used within your map's extent definition.