This question is similar to an earlier question about displaying vector data in a web-map. I want to have a basic web interface which uses OpenLayers and can display raster data that is currently in PostGIS, using a basemap such as google. I have installed PostGIS2.0 with the raster library and see from the documentation that there are several options to output data, such as JPEG, GeoTIFF and PNG. Is their a recommend format to use?

To display vector data I am currently using javascript to send coordinates from the user's browser to my server, and then performing a query using PHP/SQL on PostGIS. The results are then returned as GeoJSON which can be overlaid on the basemap. I wondered if a similar approach could be applied using rasters but I don't know what raster format is best, or if there are any other constraints I should consider.

I have seen a lot of references to GeoServer but I'm not sure if it is necessary for this project (I'm not entirely sure when GeoServer should be used or not).

3 Answers 3


Rasters are images, so the best format will be an image format (png/jpg/etc). Geojson is a good format for vectors. You will never see, say, your facebook profile pic be transported back as pure json. Of course, returning a url to an image as json is very very different :)

As far as the jpeg vs png vs any other image format discussion, there are tons of different opinions about it. What it boils down to is that some image formats are lossless while others are lossy. Your decision depends on what you are going to do with the data. For analysis you may probably not tolerate lossy, but for displaying things on the web you would.

Can PostGIS 2.0 generate images? Sure. Look at the Raster output section.

Is this a good idea? It really really really depends on what you are doing. Sometimes generating images may take a really really really long time, and you want to cache them to avoid paying the price of generating them every single time you request them through a web environment. The only "caching" you can do on the PostGIS side is to save the results to a different table and then query the table instead when you need it. Geoserver has an entire infrastructure (GeoWebCache) for doing this.

Do you need GeoServer, probably not - but it makes life a lot easier when you have this middleware that exposes all of your data using OGC web services, does caching, styling of vectors and rasters a whole bunch more things.

  • I understand how to generate an image using PostGIS - what I'm trying to understand is the best way of loading it into Openlayers. For example, if I use ST_AsPNG() - I will still need to identify the upper and lower coordinates where this image is positioned. I thought there might be a neat way of returned a self-contained image that I could directly load into openlayers (aside from using a geoserver).
    – djq
    Commented Oct 11, 2011 at 12:46
  • Yes you can load it directly to openlayers from st_aspng, but it will require extra work. You then have to keep track of the bbox, and specificy it when you load it dev.openlayers.org/apidocs/files/OpenLayers/Layer/Image-js.html Trust me, it is the long route. You are better off letting geoserver or mapserver do this. They give you back WMS/WMTS and you use two lines of code to add it to openlayers. Commented Oct 12, 2011 at 14:50

Mapserver has support for postgis rasters and directions for a basic setup are given here (item 9.9).

In your case you would setup Mapserver to work as a WMS server so that it returns the Postgis rasters as normal WMS layers. It is then almost trivial to add the layers into your OpenLayers map by using a WMS layer object.

This kind of solution is probably your best option because it is all based on OpenSource software, it's a proven stack and does not require extensive middleware. All mapserver needs is a webserver (the apache you are probably already using for php is ok) that can run cgi scripts. If performance is not a critical issue then you can avoid using a (tile)cache and just have mapserver render the layers on the fly every time.

If you want to cache the tiles to minimize server load and improve response times consider using mapcache or tilecache. Mapcache is a younger sister project of Mapserver, but should outperform all other caches as it runs as an apache module.

For Ubuntu users the easiest easy way to install Mapserver's dependencies is to add the Ubuntu GIS PPA to your system.

  • Mapserver is definitely a great alternative from GeoServer (I use it for some other projects). Generally, I pick geoserver for my customers because it has an easy to use interface. Nevertheless, I wanted to point out that serving tile caches is serving static content, and any solution based on nginx (or any other non-blocking server) would be far faster than any apache solution - in either process-based isolation or threaded mode. Commented Oct 10, 2011 at 19:44
  • Thanks @unicoletti. Can I use Mapserver if I want to generate rasters based on the users query? For example if I allow users to specify any area (within a certain boundary) will it enable me to make a spatial query of that area?
    – djq
    Commented Oct 10, 2011 at 19:45
  • @celenius Mapserver and GeoServer will both allow you to do that. Commented Oct 10, 2011 at 21:04
  • 1
    @RagiYaserBurhum I compare mapcache to other caches. Static tiles are not strictly speaking a cache.
    – unicoletti
    Commented Oct 11, 2011 at 8:39

It all depends. Image size, caching, available libraries (some proprietary jp2k drivers for example).

Here is a link to an awesome pdf, Geoserver on steroids. This will run you through the various formats, their pros and cons and some basic setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.