Geoserver GWC preseed scripts Source

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---
11
12![](/assets/imported/2011-07-04-geoserver-gwc-preseed-scripts/image-1.png)
13
14GIS 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:
15
16```
17#This config file contains options for the gwc preseeding scripts
18geoserver_userpass = "admin:XXXXX"
19geoserver_url = "http://GEOSERVER_URL"
20
21#Zoom levels to create tiles for
22zoom_start = "0"
23zoom_stop = "10"
24
25#Geoserver layers to perform preseeding operation on
26
27layers = ["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```
29
30Listing 2 purge.py:
31
32```
33#!/usr/bin/python
34
35#This script removes tiles from the GWC cache
36#Ensure that seed_config.py has been configured before use
37
38import os
39import seed_config
40
41for 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 " + layer
45```
46
47Listing 3 seed.py:
48
49```
50#!/usr/bin/python
51
52#This script is used to preseed gwc layers
53#Ensure that seed_config.py has been configured before use
54
55import os
56import seed_config
57
58for 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 " + layer
62```
63
64After you initiate the preseed jobs, you can jump back into the GWC tile creation page to get an idea of the status.
65
66![](/assets/imported/2011-07-04-geoserver-gwc-preseed-scripts/image-2.png)
67
68All 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:
69
70```
71I finally managed to achieve some kind of preseed on 2.03 with a webservice in c#.
72
73The 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….
74
75[WebMethod]
76
77        public void reseed_Postgis_tbv_p2000_cad2010()
78        {
79
80             ASCIIEncoding encoding = new ASCIIEncoding();
81
82            string postData = "threadCount=12&type;=reseed&gridSetId;=EPSG%3A900913&format;=image%2Fpng&zoomStart;=00&zoomStop;=16&minX;=&minY;&maxX;=&maxY;=";
83
84            byte[] data = encoding.GetBytes(postData);
85
86            // Prepare web request...
87            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://SERVER:8081/geoserver/gwc/rest/seed/Postgis:tbv_p2000_cad2010");
88
89            myRequest.Method = "POST";
90
91            myRequest.ContentType = "Content-Type=application/x-www-form-urlencoded";
92
93            myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("admin:PASSWORD")));
94
95            myRequest.ContentLength = data.Length;
96
97            Stream newStream = myRequest.GetRequestStream();
98
99            // Send the data.
100
101            newStream.Write(data, 0, data.Length);
102
103            HttpWebResponse WebResp = (HttpWebResponse)myRequest.GetResponse();
104            newStream.Close();
105        }
106
107I just had to put my asmx file on a IIS server and automatically call it from a blank asp.net page
108
109After that I put some “explorer http://server/myasp.asp” on a task scheduler to execute daily
110
111Cheers,
112
113Arnaud
114```
115
116
117