Managing layers with the GWC REST API

You were already in contact with cached layers in Chapter 5, Advanced Configurations. You had a quick tour of GeoWebCache (http://geowebcache.org/), which is a Java open source project. Like any caching system, it acts as a proxy between the clients and the map server. There is a standalone version that works with any map server being compliant with the WMS standard.

Indeed, GeoWebCache uses the WMS syntax to retrieve tiles from the map server. It exposes the tiles in several ways, with the GeoServer integrated version you can use:

  • Web Map Service (WMS)
  • WMS Tiling Client Recommendation (WMS-C)
  • Web Map Tiling Service (WMTS)
  • Tile Map Service (TMS)

You used the integrated version of GeoWebCache and it is a good choice as there are many advantages in using the internal one. You can use a single interface to administer both GeoServer and GeoWebCache, and you don't have to use a custom URL or a special endpoint. Also, all the layers you publish on GeoServer are automatically configured as cached. You just have to set the caching properties on layers and layer groups.

Of course, you can use REST operations to control GeoWebCache settings and behavior.

How to do it …

  1. First of all, have a look at what layers are cached in your GeoServer. As usual, we start with cURL:
    $ curl -u admin:geoserver -XGET -H 'Accept: text/xml' http://localhost:8080/geoserver/gwc/rest/layers -o gwc_layers.xml
    
  2. Then perform the same operation with Python:
    >>> myUrl = 'http://localhost:8080/geoserver/gwc/rest/layers'
    >>> headers = {'Accept':'text/xml'}
    >>> resp = requests.get(myUrl, auth=('admin','geoserver'), headers=headers)
    >>> file = open('gwc_layers_py.xml','w')
    >>> file.write(resp.text)
    >>> file.close()
    
  3. Now examine the result:
    <layers>
      <layer>
        <name>NaturalEarth:Coastline</name>
        <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth%3ACoastline.xml" type="text/xml"/>
      </layer>
    …
    </layers>
  4. Now, retrieve the information of a single layer:
    $ curl -u admin:geoserver -XGET -H 'Accept: text/xml' http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml -o gwc_coastline.xml
    
  5. Again, do the same with Python:
    >>> myUrl = 'http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml'
    >>> headers = {'Accept':'text/xml'}
    >>> resp = requests.get(myUrl, auth=('admin','geoserver'), headers=headers)
    >>> file = open('gwc_coastline_py.xml','w')
    >>> file.write(resp.text)
    >>> file.close()
    
  6. Open the XML file retrieved. There is a lot of information:
    <?xml version="1.0" encoding="UTF-8"?>
    <GeoServerLayer>
      <id>LayerInfoImpl--5b67a624:145e68c7cdc:-7ffc</id>
      <enabled>true</enabled>
      <name>NaturalEarth:Coastline</name>
      <mimeFormats>
        <string>image/jpeg</string>
        <string>image/png</string>
      </mimeFormats>
      <gridSubsets>
        <gridSubset>
          <gridSetName>EPSG:900913</gridSetName>
          <extent>
            <coords>
              <double>-2.003750834E7</double>
              <double>-2.003750834E7</double>
              <double>2.003750834E7</double>
              <double>1.8440002895114224E7</double>
            </coords>
          </extent>
        </gridSubset>
        <gridSubset>
          <gridSetName>EPSG:4326</gridSetName>
          <extent>
            <coords>
              <double>-180.0</double>
              <double>-85.60903777459774</double>
              <double>180.0</double>
              <double>83.64513</double>
            </coords>
          </extent>
        </gridSubset>
      </gridSubsets>
      <metaWidthHeight>
        <int>4</int>
        <int>4</int>
      </metaWidthHeight>
      <expireCache>0</expireCache>
      <expireClients>0</expireClients>
      <parameterFilters>
        <styleParameterFilter>
          <key>STYLES</key>
          <defaultValue></defaultValue>
        </styleParameterFilter>
      </parameterFilters>
      <gutter>0</gutter>
    </GeoServerLayer>
  7. Now you will modify the layer, removing the grid set in CRS EPSG:900913. From the XML file saved, remove the following lines:
        <gridSubset>
          <gridSetName>EPSG:900913</gridSetName>
          <extent>
            <coords>
              <double>-2.003750834E7</double>
              <double>-2.003750834E7</double>
              <double>2.003750834E7</double>
              <double>1.8440002895114224E7</double>
            </coords>
          </extent>
        </gridSubset>
  8. Save the file and send this request to update the layer configuration:
    $ curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d @gwc_coastline.xml http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml
    
  9. Again, do the same with the following Python code:
    >>> myUrl = 'http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml'
    >>> file = open('gwc_coastline.xml','r')
    >>> payload = file.read()
    >>> headers = {'Content-type': 'text/xml','Accept': 'text/xml'}
    >>> resp = requests.post(myUrl, auth=('admin','geoserver'), data=payload, headers=headers)
    >>> resp.status_code
    200
    
  10. Check the configuration on the web interface.
    How to do it …

How it works …

Configuring cached layers is really similar to performing operations on GeoServer layers. In the web interface, the configuration is indeed perfectly integrated and only on the REST interface you have a different entry point for your operations.

Please note that, by default, when you add a new layer on GeoServer, it is also automatically added to the GeoWebCache configuration.

In the first request of this recipe, you retrieved the layers' list and it should contain all layers unless you changed the GeoServer's default behavior.

However, any action you perform on a layer from the GWC REST interface does work on the uncached layer. Let's see, for instance, what happens when we delete the coastline layer using the GWC REST interface.

As usual, we firstly used cURL to do it:

$ curl -u admin:geoserver -XDELETE -H 'Content-type: text/xml' http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml

Then switched to Python code:

>>> myUrl = 'http://localhost:8080/geoserver/gwc/rest/layers/NaturalEarth:Coastline.xml'
>>> headers = {'Content-type': 'text/xml'}
>>> resp = requests.delete(myUrl, auth=('admin','geoserver'), headers=headers)
>>> resp.status_code
200

If you now open the layer configuration and go to the Tile Caching tab, you can see it's empty.

How it works …
..................Content has been hidden....................

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