1---2title: 'Geoserver GWC preseed scripts'3date: '2011-07-04'4published_at: '2011-07-04T17:31:00.009+10:00'5tags: ['geoserver', 'geospatial', 'gis', 'gwc', 'performance']6author: 'Gavin Jackson'7excerpt: 'GIS servers can pre-generate tiles to vastly improve WMS performance. Geoserver provides a handy web interface for interacting with the geoserver web cache (GWC), but sometimes it is handy to script t...'8updated_at: '2011-07-08T14:53:27.597+10:00'9legacy_url: 'http://www.gavinj.net/2011/07/geoserver-gwc-preseed-scripts.html'10---11121314GIS servers can pre-generate tiles to vastly improve WMS performance. Geoserver provides a handy web interface for interacting with the geoserver web cache (GWC), but sometimes it is handy to script this process (particularly when you are working with hundreds of layers). Geoserver 2.1 provides a REST API that allows you to trigger tile preseed jobs. Here are a couple of python scripts that I wrote that use this API to perform a purge and seed operation. Change the following config file to include your desired geoserver layers and zoom levels (I assume that you want 900913 png tiles, and two threads per layer - the scripts can easily be modified to change this however). Listing 1 seed_config.py:1516```17#This config file contains options for the gwc preseeding scripts18geoserver_userpass = "admin:XXXXX"19geoserver_url = "http://GEOSERVER_URL"2021#Zoom levels to create tiles for22zoom_start = "0"23zoom_stop = "10"2425#Geoserver layers to perform preseeding operation on2627layers = ["ALA:Rees_CAAB37009001Polygon","ALA:Rees_CAAB37007002Polygon","ALA:Rees_CAAB37007001Polygon","ALA:Rees_CAAB37006001Polygon","ALA:Rees_CAAB37010004Polygon","ALA:Rees_CAAB37005005Polygon","ALA:Rees_CAAB37008003Polygon","ALA:Rees_CAAB37005002Polygon","ALA:Rees_CAAB37011001Polygon","ALA:Rees_CAAB37008001Polygon","ALA:Rees_CAAB37005004Polygon","ALA:Rees_CAAB37005001Polygon","ALA:Rees_CAAB37012002Polygon","ALA:Rees_CAAB37007003Polygon","ALA:Rees_CAAB37010002Polygon","ALA:Rees_CAAB37010003Polygon"]28```2930Listing 2 purge.py:3132```33#!/usr/bin/python3435#This script removes tiles from the GWC cache36#Ensure that seed_config.py has been configured before use3738import os39import seed_config4041for layer in seed_config.layers:42 curlstring="curl -u " + seed_config.geoserver_userpass + " -XPOST -H 'Content-type: text/xml' -d '" + layer + "900913" + seed_config.zoom_start + "" + seed_config.zoom_stop + "image/pngtruncate2' " + seed_config.geoserver_url + "/geoserver/gwc/rest/seed/" + layer + ".xml"43 os.system(curlstring)44 print "truncated " + layer45```4647Listing 3 seed.py:4849```50#!/usr/bin/python5152#This script is used to preseed gwc layers53#Ensure that seed_config.py has been configured before use5455import os56import seed_config5758for layer in seed_config.layers:59 curlstring="curl -u " + seed_config.geoserver_userpass + " -XPOST -H 'Content-type: text/xml' -d '" + layer + "900913" + seed_config.zoom_start+ "" + seed_config.zoom_stop + "image/pngseed2' " + seed_config.geoserver_url + "/geoserver/gwc/rest/seed/" + layer + ".xml"60 os.system(curlstring)61 print "seeded " + layer62```6364After you initiate the preseed jobs, you can jump back into the GWC tile creation page to get an idea of the status.65666768All generated tiles end up in your {geoserver_data_dir}/gwc/ folder. Update: The following solution for.net developers running an older version of Geoserver has been provided by Arnaud Buisson-Delandre below:6970```71I finally managed to achieve some kind of preseed on 2.03 with a webservice in c#.7273The tricks are : don’t use xml but plain html webrequest and encode the post data. Here are the “harsh” hard coded code ;) just in case….7475[WebMethod]7677 public void reseed_Postgis_tbv_p2000_cad2010()78 {7980 ASCIIEncoding encoding = new ASCIIEncoding();8182 string postData = "threadCount=12&type;=reseed&gridSetId;=EPSG%3A900913&format;=image%2Fpng&zoomStart;=00&zoomStop;=16&minX;=&minY;&maxX;=&maxY;=";8384 byte[] data = encoding.GetBytes(postData);8586 // Prepare web request...87 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://SERVER:8081/geoserver/gwc/rest/seed/Postgis:tbv_p2000_cad2010");8889 myRequest.Method = "POST";9091 myRequest.ContentType = "Content-Type=application/x-www-form-urlencoded";9293 myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("admin:PASSWORD")));9495 myRequest.ContentLength = data.Length;9697 Stream newStream = myRequest.GetRequestStream();9899 // Send the data.100101 newStream.Write(data, 0, data.Length);102103 HttpWebResponse WebResp = (HttpWebResponse)myRequest.GetResponse();104 newStream.Close();105 }106107I just had to put my asmx file on a IIS server and automatically call it from a blank asp.net page108109After that I put some “explorer http://server/myasp.asp” on a task scheduler to execute daily110111Cheers,112113Arnaud114```115116117