In this chapter, we will use tiles, pre-rendered portions of a map, to allow your app to load more rapidly. While the subject of tile caching can get pretty complex, I'll be covering the material you need to know to get your tile cache up and running and fix any issues that may occur.
First, we'll cover the tile caching basics. Then, you'll learn how to configure tile caching and GeoServer. Next, you'll create a tile-backed web map app, you'll learn to use the tile cache, and finally we'll explore methods for solving problems with tile caching.
In this section, you'll learn key concepts for tile caching, including tile caching schemes. We'll look at some software options for tile caching, and finally you'll learn about the process associated with the GeoServer tile caching stack that we'll be using in this section.
Tile caching is very useful when you are working with background maps, or for static content that does not change very often or involve much interaction. An example of tile caching is OpenStreetMap (https://www.openstreetmap.org/#map=10/21.2516/86.7343), and you can see tile caching at work as we zoom in and out of the map; portions of the map are pre-rendered as tiles, little images, and then they're stitched together by the frontend OpenLayers client, as shown in the following screenshot:
This allows very complex cartography to be delivered very quickly to our browser, avoiding the need to render that information in the browser or on the server in real time. Here, you can see two major title caching schemes in GeoServer (WMS-C service and TMS service), which are shown in the following screenshot:
Tile caching schemes use different ways to refer to tiles and allow web clients to communicate with servers such as GeoServer to retrieve tiles in the appropriate way. While WMS-C is used for geographic coordinate bounding boxes to identify tiles, the new schemes, TMS and WMS-T, used coordinates based on the tile scheme itself, each tile numbered by its place on a grid, which is defined by variable grader origins. We can disable all of these other tile caching schemes as we are going to be using WMS-C, which is enabled separately, in its own section under Services.
The following example illustrates tile-based coordinates (https://openlayers.org/en/latest/examples/canvas-tiles.html):
So, instead of seeing the typical latitude/longitude coordinates that you would be used to, you can see coordinates based on their place within the tile grid.
It's important to think about tile caches in terms of fixed parameters. Although there is some wiggle room for allowing the client to select variable parameters, you will get the best results by sticking to minimal parameters that fit your use case.
For example, in the case of image formats, select JPEG if compression is desired – this is a great option for imagery – and PNG if using vector data. Select PNG 8 if 256 colors are adequate, and simply PNG if a wider color palette is necessary; PNG 8 uses eight bits.
There are a number of software options for tile caching with your map app; these include GDAL or GDAL, the popular command-line geospatial utility Mapnik, which is the backend for the map box project and others, and GeoWebCache. GeoWebCache is available as a standalone instance, but is also embedded in GeoServer, so we will focus on GeoWebCache. Unlike file-based tile caches running on HTTP servers, GeoServer via GeoWebCache provides web service endpoints for map tiles for static and dynamic tile caching and additional capabilities.
The following diagram explains how tile caching works with GeoServer:
On the Browser side, we have a map client such as OpenLayers, and in the OpenLayers source there's a WMST class – our WMTS class that we can use for WMTS-type tile cache schemes. There are other classes available for other schemes. The OpenLayers app first checks on the browser cache to see if a tile is already available for the tile that's needed in the map view. Note that this max age of the Image File can be set in GeoServer to limit the caching of tiles. This is especially useful for debugging purposes.
On the Geoserver side, when a tile is not found in the cache, contact will be made with the GeoServer endpoint for that layer, which refers to GeoWebCache. It can directly refer to the GeoWebCache endpoint or to the WMS endpoint with a tiled parameter. We'll be using the direct GeoWebCache endpoint. When contact is made with the GeoWebCache endpoint, GeoWebCache will check on the filesystem to see if that image tile has already been created, and if so, that tile will be served back to the browser as a response to the HTTP request. If that tile is not already available, GeoWebCache will dynamically render that tile.
In this section, you learned the basics of tile caching. In the next section, you'll learn how to configure tile caching in GeoServer.
In the last section, you learned the fundamentals of tile caching. In this section, you'll learn the ins and outs of setting up tile caching in GeoServer. First, you'll learn all the global and layer options for tile caching in the GeoServer web administration interface. Then, we'll look at these settings in the XML files under the GeoServer directory. Finally, you'll learn about gridsets and why these are important in building your tile-cache-backed app.
We disabled all of these other services in the previous section, as shown in the following screenshot, since we'll be using WMST, which is enabled in the Services portion of the sidebar:
You can set a number of global options that will be available on all new layers or layer groups that will be tiled. Let's look at a couple of these options:
The metatile size and gutter size refer to ways of eliminating repeating labels or other tile artifacts that may appear during tile rendering. We'll talk more about this in the final section of this chapter.
There are a number of tile formats available; we covered this a little bit in the previous section, and we'll also cover this again in the last section of this chapter.
Another area to pay attention to here is the gridsets that are available, shown in the following screenshot, and we'll talk about those later in this section:
Formats are important for minimizing the disk space that is taken up, as well as improving the performance of GeoServer rendering and the transmission of tiles to the client app. Let's look at local options.
Options for particular layers or layer groups will also be defined on the layers themselves, or under the tile caching tab within a layer. As you can see in the following screenshot, caching is enabled for this layer:
We're using the same default on the metatile. We have both of these formats, JPEG and PNG, enabled by default. A better option here would be to have just a singl...