[OSM-dev] NOT-saving empty Mapnik tiles

marqqs at gmx.eu marqqs at gmx.eu
Mon Aug 15 13:52:09 BST 2011


Hi Igor,

thanks a lot! I just got two further ideas:

1. Mapnik internals
I would guess that Mapnik stored every tile in an array of 256 x 256 Bytes. If you could check such an array's bytes if they all had the same value, this would indicate either strictly monochromacity or complete transparency. In my case there are no monochrome tiles, so I could conclude these tiles to be transparent.

2. tempfs
You could write every tile to ramdisk first, e.g. to /dev/shm, and then decide whether the tile is "empty" or not. If not, you could copy it to the regular position in the tile tree.

I should run some comparisons... :-)


-------- Original-Nachricht --------
> Datum: Mon, 15 Aug 2011 10:08:51 +0200
> Von: Igor Podolskiy <igor.podolskiy at vwi-stuttgart.de>
> An: marqqs at gmx.eu
> CC: dev at openstreetmap.org
> Betreff: Re: [OSM-dev] NOT-saving empty Mapnik tiles

> Hi Markus,
> 
> > unfortunately, this line did not work:
> >
> >> im.save(buf, 'png256')
> >
> > I tried im.tostring() too, did not work either. This is the error
> message:
> >
> >
> > Traceback (most recent call last):
> >    File "./mapnik_tile.py", line 143, in<module>
> >      im.save(buf, 'png256')
> > Boost.Python.ArgumentError: Python argument types in
> >      Image.save(Image, instance, str)
> > did not match C++ signature:
> >      save(mapnik::Image32, std::string)
> >      save(mapnik::Image32, std::string, std::string)
> >
> >
> > Do you have an idea what went wrong?
> Yes - I've again missed what mapnik.Image can and can't :( The problem 
> is that mapnik.Image doesn't support saving to a buffer. The tostring() 
> method return the raw data as a string, not the PNG data, so testing 
> this for length is useless. You can use that to convert a mapnik.Image 
> to a PIL image which is much more versatile. I've got it to work now 
> with the following code:
> 
> import PIL.Image
> import mapnik
> import StringIO
> 
> w, h = 100, 100
> mapnik_image = mapnik.Image(w, h)
> # <render to the image>
> pil_image = PIL.Image.fromstring(
>              'RGBA', (w, h), mapnik_image.tostring())
> buf = StringIO.StringIO()
> # PIL supports saving to a buffer
> pil_image.save(buf, 'png')
> print buf.len # yields 119 on my machine
> # check the length, write the image to disk etc.
> 
> I don't really know how it behaves performance-wise on your application. 
> As of mapnik 0.7.1, it seems to me to be the only way to get a 
> mapnik.Image into a buffer.
> 
> BTW, now that you converted the mapnik.Image to a PIL.Image, you can use 
> all the PIL methods to analyze it in some other way, of course.
> 
> Hope that helps
> Igor



More information about the dev mailing list