Skip to content

Instantly share code, notes, and snippets.

@soiqualang
Forked from gordcorp/GeoWebCacheService.py
Created August 16, 2023 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soiqualang/ee48b7ef715782e3912b7e2553107b4d to your computer and use it in GitHub Desktop.
Save soiqualang/ee48b7ef715782e3912b7e2553107b4d to your computer and use it in GitHub Desktop.
Empty and reseed GeoServer GeoWebCache
import requests
class GeoWebCacheService(object):
"""Class to communicate with the geowebcache api.
Api doco here: http://geowebcache.org/docs/current/rest/seed.html
"""
_url = None
_username = None
_password = None
def __init__(self, url, username, password):
self._url = url
self._username = username
self._password = password
def _get_seed_url(self, layer):
if layer != None:
layer = urllib.parse.quote(layer)
url = urllib.parse.urljoin(self._url, "seed/%s.json" % layer)
else:
url = urllib.parse.urljoin(self._url, "seed.json")
return url
def get_tasks(self, layer=None):
url = self._get_seed_url(layer)
r = requests.get(url, auth=(self._username, self._password))
j = r.json()
return j["long-array-array"]
def empty(self, layer):
"""Empty the cache for this layer.
"""
url = self._get_seed_url(layer)
# There isn't a simple Empty call in the API, so instead I'm doing a separate truncate on each of
# the caches that are defined by default in geoserver.
grid_set_ids = ["EPSG:900913", "EPSG:4326"]
formats = ["image/jpeg", "image/png"]
for grid_set_id in grid_set_ids:
for fmt in formats:
data = {
"seedRequest": {
"type":"truncate",
"minX":[],
"minY":[],
"maxX":[],
"maxY":[],
"zoomStart":0,
"zoomStop":30,
"format":fmt,
"gridSetId":grid_set_id,
"threadCount":1
}
}
requests.post(url, auth=(self._username, self._password), json=data)
# With help from https://gist.githubusercontent.com/alexgleith/844146bb24b4f22d856e/raw/2dd2351558bf0a7cc7a800cf8dc4d4c11100cba1/Seed-Truncate%2520Python
def seed(self, layer):
"""Start seeding the given layer
"""
url = self._get_seed_url(layer)
# Seed with the national map parameters
data = {
"seedRequest": {
"type":"seed",
"minX":[],
"minY":[],
"maxX":[],
"maxY":[],
"gridSetId":"EPSG:900913",
"zoomStart":0,
"zoomStop":10,
"format":"image/png",
"threadCount":4
}
}
requests.post(url, auth=(self._username, self._password), json=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment