A colleague and I recently released an Open Source project called Mapoteca which is a set of Python Scripts that integrates with a GDAL/FWTools installation allowing a full walk-through of directories looking for spatial data and extracting extent, geometry, number of elements, projection and user writing it into a csv file. We are working on the part that describes the DBF files associated and we wanted to generate a thumbnail image of each shapefile, do you know of a way to generate these thumbnails in jpeg or png using Python?
6 Answers
I had posted a question on creating images from vector data that you might find useful. The accepted answer, the combination of shapely, ogr, matplotlib, and numpy seems like it would work nicely for you.
The simplest way would be to call gdal_rasterize using subprocess.call(), but I suspect it doesn't do any interpolation so it would alias to the point of illegibility if you generated a thumbnail-sized image straight from the data. So you should generate a "reasonably sized"* temporary image and use the Python image library to scale it to the size you want.
But I expect the biggest problem will be on which attribute(s) will you base your styling on? Shapefiles have no inherent display information as I'm sure you're aware, so you will need to find a way of deciding which colours to use. gdal_rasterize does give you the capability to do this, but the choice is normally based on domain-specific knowledge. To have a generic tool, you're going to have to have a generic styling scheme.
*This too is something for which you may have to make a few hard-coded rules. Perhaps start with an ideal pixel size (say 10m per pixel), and increase that if the resulting image is too large to be practical.
Mapnik and gdal_rasterize are good options. I usually create a simple mapfile styled however I want and then use Mapserver's shp2img executable. There are good tutorials on creating mapfiles.
Just for the sake of completeness - mapserver can also render good looking maps from a range of base data, on commandline using the shp2img utility. Again, you will need to decide on a very generic, very simple style that fits your purposes.
Another related question: Platforms for repetitive automated mapping? There mapserver was suggested.