# GeoWebchache使用rest方式切片
需要引用的maven
<!-- curl工具 -->
<dependency>
<groupId>org.toile-libre.libe</groupId>
<artifactId>curl</artifactId>
<version>LATEST</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
用于执行curl命令
application.yml中的配置
geoserver:
url: http://localhost:8080/geoserver
username: admin
password: geoserver
shpworkspace: shp
imageworkspace: image
1
2
3
4
5
6
2
3
4
5
6
java核心代码
import net.sf.json.JSONArray;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.toilelibre.libe.curl.Curl.curl;
import static org.toilelibre.libe.curl.Curl.$;
@Component
public class GeoWebCache {
private static String url;
private static String geoUsername;
private static String geoPassword;
//待发布矢量图层的工作区间
public static String shpWorkspace;
//待发布影像图层的工作空间
public static String imageWorkspace;
@Value("${geoserver.url}")
public void setUrl(String url) {
GeoWebCache.url = url;
}
@Value("${geoserver.username}")
public void setGeoUsername(String geoUsername) {
GeoWebCache.geoUsername = geoUsername;
}
@Value("${geoserver.password}")
public void setGeoPassword(String geoPassword) {
GeoWebCache.geoPassword = geoPassword;
}
@Value("${geoserver.shpworkspace}")
public void setShpWorkspace(String shpWorkspace) {
GeoWebCache.shpWorkspace = shpWorkspace;
}
@Value("${geoserver.imageworkspace}")
public void setImageWorkspace(String imageWorkspace) {
GeoWebCache.imageWorkspace = imageWorkspace;
}
/**
* 获取geoWebCache中的图层
* 并按shp和image 分类
*
* @return Map
*/
public static Map<String, List<String>> getLayers() {
Map<String, List<String>> map = new HashMap();
String cmd = "curl -u " + geoUsername + ":" + geoPassword + " \"" + url + "/gwc/rest/layers\"";
List<String> shp = new ArrayList<>();
List<String> image = new ArrayList<>();
HttpResponse curl = curl(cmd);
HttpEntity entity = curl.getEntity();
if (entity != null) {
String result = null;
try {
result = EntityUtils.toString(entity, "UTF-8");
JSONArray jsonArray = JSONArray.fromObject(result);
for (Object o : jsonArray) {
String str = o.toString();
String str1 = str.substring(0, str.indexOf(":"));
if ("shp".equals(str1)) {
shp.add(str);
}
if ("image".equals(str1)) {
image.add(str);
}
}
map.put("shp", shp);
map.put("image", image);
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 指定图层进行切片操作
*
* @param layer 指定图层 shp:test
* @param zoomStart 1 切片开始层级
* @param zoomStop 15 切片结束层级
* @return boolean
*/
public static boolean slice(String layer, int zoomStart, int zoomStop) {
int threadCount = 2;
String res = "";
String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -XPOST -H \"Content-type: text/xml\" -d '<seedRequest><name>" + layer +
"</name><srs><number>4326</number></srs><zoomStart>" + zoomStart + "</zoomStart><zoomStop>" + zoomStop + "</zoomStop><format>image/png</format><type>" + layer + "</type><threadCount>" + threadCount + "</threadCount></seedRequest>' \""
+ url + "/gwc/rest/seed/" + layer + ".xml\"";
HttpResponse curl = curl(cmd);
StatusLine statusLine = curl.getStatusLine();
return "HTTP/1.1 200 ".equals(statusLine.toString());
}
/**
* 获取切片的情况
*
* @param layer 指定图层
* @return Map
*/
public static Map getSliceType(String layer) {
Map map = new HashMap();
//返回所有图层切片情况 curl -u <user>:<password> -XGET http://localhost:8080/geoserver/gwc/rest/seed.json
//返回指定图层的切片情况
String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -XGET " + url + "/gwc/rest/seed/" + layer + ".json";
HttpResponse curl = curl(cmd);
StatusLine statusLine = curl.getStatusLine();
if ("HTTP/1.1 200 ".equals(statusLine.toString())) {
HttpEntity entity = curl.getEntity();
try {
String result = EntityUtils.toString(entity, "UTF-8");
System.out.println(result);
JSONArray jsonArray = JSONArray.fromObject(result);
map.put("res", jsonArray);
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 停止所有正在进行的切片任务
*
* @return boolean
*/
public static boolean stopAllSlice() {
String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -d \"kill_all=all\" \"" + url + "/gwc/rest/seed\"";
HttpResponse curl = curl(cmd);
StatusLine statusLine = curl.getStatusLine();
return "HTTP/1.1 200 ".equals(statusLine.toString());
}
/**
* 停止指定图层的切片任务
*
* @return boolean
*/
public static boolean stopSliceByLayer(String layer) {
String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -d \"kill_all=all\" \"" + url + "/gwc/rest/seed/" + layer + "\"";
HttpResponse curl = curl(cmd);
StatusLine statusLine = curl.getStatusLine();
return "HTTP/1.1 200 ".equals(statusLine.toString());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
GeoWebchache修改缓存位置
<context-param>
<param-name>GEOWEBCACHE_CACHE_DIR</param-name>
<param-value>D:\test\gwc</param-value>
</context-param>
1
2
3
4
2
3
4