[mapserver-users] New method for Python mapscript
Sean Gillies
sgillies@i3.com
Mon, 12 Aug 2002 12:07:08 -0600
This is a multi-part message in MIME format.
--------------040705040901030905090409
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Greetings Python mapscript users,
Recently, I've found the use for getting mapserver map images as
Python string objects. Rather than fill in the empty Python
getImageToVar() method, I decided to write a new interface for
a method I'm calling getImageString(). Mostly I disagreed with
the notion of getting to a variable through its name as a string.
I'm including the code that you might paste into your mapscript.i
file within the imageObj definition. You will need to modify your
setup.py script so that -DUSE_GD_PNG and -DUSE_GD_JPEG as passed as
arguments to swig. I've only tested this with gd-1.8.4 but will be
moving on to gd-2 next week.
So, now we can do stuff like this:
import MapScript
mapob = MapScript.mapObj(...)
imgob = mapObj.draw()
data = imgob.getImageString(...)
png = file("map.png", "wb")
png.write(data)
png.close()
This is trivial. It's more useful for creating Zope image objects
from a Zope Python script like this:
import StringIO
import MapScript
mapob = MapScript.mapObj(...)
imgob = mapObj.draw()
data = StringIO.StringIO(imgob.getImageString(...))
self.manage_addImage('map.png', data, "", "", 'image/png')
Hope that you find this of use and happy to hear any other ideas.
cheers,
Sean
--------------040705040901030905090409
Content-Type: text/plain;
name="getImageString.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="getImageString.txt"
// Method getImageString renders the imageObj into image data and returns
// it as a string. Inspired by and used like the saveImage() method. Python
// only at this time. Questions and comments to Sean Gillies <sgillies@i3.com>
#ifdef SWIGPYTHON
PyObject *getImageString(
int imagetype,
int transparent,
int interlace,
int quality)
{
unsigned char *imgbytes;
int size;
PyObject *imgstring;
if (interlace) gdImageInterlace(self->bytes, 1);
if (transparent) gdImageColorTransparent(self->bytes, 0);
switch(imagetype) {
case(MS_GIF):
msSetError(MS_MISCERR, "GIF output is not available.", "saveImageString()");
return(MS_FAILURE);
break;
case(MS_PNG):
#ifdef USE_GD_PNG
imgbytes = gdImagePngPtr(self->bytes, &size);
#else
msSetError(MS_MISCERR, "PNG output is not available.", "saveImageString()");
return(MS_FAILURE);
#endif
break;
case(MS_JPEG):
#ifdef USE_GD_JPEG
imgbytes = gdImageJpegPtr(self->bytes, &size, quality);
#else
msSetError(MS_MISCERR, "JPEG output is not available.", "saveImageString()");
return(MS_FAILURE);
#endif
break;
case(MS_WBMP):
#ifdef USE_GD_WBMP
imgbytes = gdImageWBMPPtr(image->bytes, &size, 1);
#else
msSetError(MS_MISCERR, "WBMP output is not available.", "saveImageString()");
return(MS_FAILURE);
#endif
break;
default:
msSetError(MS_MISCERR, "Unknown output image type.", "saveImageString()");
return(MS_FAILURE);
}
// Create a Python string from the (char *) imgbytes.
// The gdImage*Ptr functions return a size for just this purpose.
imgstring = PyString_FromStringAndSize(imgbytes, size);
// The gd docs recommend gdFree()
gdFree(imgbytes);
return imgstring;
}
#endif
--------------040705040901030905090409--