Skip to content

Instantly share code, notes, and snippets.

@alexgleith
Created March 18, 2015 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexgleith/c39c3f520102c30e4dec to your computer and use it in GitHub Desktop.
Save alexgleith/c39c3f520102c30e4dec to your computer and use it in GitHub Desktop.
Truncate and Seed GWC cached layers in GeoServer
import json
import urllib2
import base64
baseURL = "https://domain.example:8080/geoserver/gwc/rest/seed/"
descURL = "https://domain.example:8080/geoserver/gwc/rest/layers/"
un = 'username'
pw = 'biglongtrickypassword'
def truncateLayer(layer):
print 'TRUNCATING: ' + layer
url = baseURL + 'WORKSPACE:' + layer + '.json'
urlDesc = descURL + 'WORKSPACE:' + layer + '.json'
base64string = base64.encodestring('%s:%s' % (un, pw)).replace('\n', '')
gridsets = []
#try the creative commons workspace, if not found ,try planning schemes.
try:
desc = urllib2.Request(urlDesc)
desc.add_header("Authorization", "Basic %s" % base64string)
layerInfo = json.loads(urllib2.urlopen(desc).read())['GeoServerLayer']
imageFormats = layerInfo['mimeFormats']
for gridset in layerInfo['gridSubsets']:
gridsets.append(gridset['gridSetName'])
except:
print 'identifying gridsets failed...'
for gs in gridsets:
print 'removing gridset: ' + gs
for imf in imageFormats:
print ' removing image format: ' + imf
try:
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (un, pw)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header('Content-Type', 'application/json')
data = {'seedRequest':{'minX':[],'minY':[],'maxX':[],'maxY':[],'gridSetId':gs,'zoomStart':00,'zoomStop':20,'format':imf,'type':'truncate','threadCount':2}}
data = json.dumps(data)
request.add_data(data)
req = urllib2.urlopen(request)
print ' success'
except:
print 'removing gridset failed'
print data
pass
def seedLayer(layer):
url = baseURL + layer + '.json'
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (un, pw)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header('Content-Type', 'application/json')
data = {'seedRequest':{'minX':[],'minY':[],'maxX':[],'maxY':[],'gridSetId':'EPSG:900913','zoomStart':00,'zoomStop':18,'format':'image/png','type':'seed','threadCount':2}}
data = json.dumps(data)
request.add_data(data)
req = urllib2.urlopen(request)
#truncate layers
truncateLayer('examplelayer')
#seed high-use layers
seedLayer('GCC_cc:examplelayer')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment