Setting up GeoWebCache – how to make pre-rendered tiles for high availability

One of the main uses of GeoServer is that it acts as a map server. It gets your data, applies styles and other transformations, and outputs pretty maps.

Every time a client requests a map to visualize, GeoServer has to perform a complex set of operations, load data, apply styles, render the result to a bitmap, and push it back to the client who performed the request. As your web application gains popularity, more and more concurrent requests will add and you could run out of resource to satisfy them all.

Having to build the map from scratch every time seems like nonsense, especially if your web application does not offer the user the possibility to modify styles for layers. In many cases, the styles are defined once and never, or very rarely, updated. So, your GeoServer instance will render lots of identical maps.

Indeed, when you are requesting a map to GeoServer, the chances are that the same map was produced before. We need a procedure to store maps and to retrieve them when required and match them for equality. This is a more general problem and is not specifically linked to GeoServer. Several systems to implement map caching exist. Earlier, GeoServer releases didn't include any caching mechanism and you had to set a software in front of GeoServer intercepting map requests and forward only those that can't get a hit from the cache to GeoServer.

In this recipe, you will learn how to use the included GeoWebCache.

How to do it…

  1. Locate your webapps folder inside the Apache Tomcat installation folder:
    $ cd /opt/Tomcat7042/webapps/
  2. Go to the geoserver/WEB-INF folder:
    $ cd geoserver/WEB-INF/
  3. Open the web.xml file and locate the line that contains the following code:
    <display-name>GeoServer</display-name>
  4. There are several other parameters already defined. We will insert a new one to set the GeoWebCache folder location. You may enter the following code just after the previous line. The param-value is valorized with a folder path valid on Linux (use the appropriate syntax on Windows):
    <!-- Setting GeoWebCache folder  -->
      <context-param>
        <param-name>GEOWEBCACHE_CACHE_DIR</param-name>
        <param-value>/opt/gwc</param-value>
      </context-param>
  5. Save the file and close it. Now, go to the Tomcat manager application to reload GeoServer. All the parameters you changed from the web administration interface don't need to be reloaded to be effective. Instead, GeoServer reads the web.xml file on startup, so any change inside it is effective only after an application reload or a servlet container restarts.
  6. Open your browser and go to http://localhost:8080/manager/html/list. Locate GeoServer in the application list and click on the Reload button on the left.
    How to do it…
  7. After a while, depending on the complexity of your configuration, a success message appears.
    How to do it…
  8. Now, go to the Tile Layers section on the Administration interface of GeoServer and browse the list to find the NaturalEarth:populatedplaces layer. From the drop-down list, select a combination of SRS and image format, for example, EPSG:4326/jpeg. A new map preview will show up in the browser window.
    How to do it…
  9. Navigate the map by panning and zooming. Each operation will request tiles to GeoWebCache; since this is the first time you use it, they have to be requested to GeoServer and stored for use in the future. Now, close the map and click on the Tile Layers link in the administration interface. When you go to the row showing the information for the layer, you can see that now there is a number showing the disk storage used by tiles.
    How to do it…

How it works…

You configured the storage location for your tiles. By default, GeoWebCache stores them in the temp folder located inside the Tomcat installation location. For the production site, it is a good idea to use a folder on a different device. Also, try to avoid storing tiles on the same disk where the data is stored.

As map requests reach GeoServer, the size of the cache may grow very fast and can also fill your disk if you are caching a lot of layers. You may want to set a disk quota to avoid filesystem corruption.

You may also want to override the default setting of GeoServer that sets all layers to be cached. Cache is very useful with static data; if you have layers with data source updating very frequently, having them cached is a waste of time and may produce maps that do not reflect the actual state of the data.

There are several configuration parameters for GeoWebCache. We will explore them in detail in Chapter 6, Automating GeoServer Configurations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243