# GeoServer使用geoserver-manager

外部系统操作GeoServer使用的是curl命令

# curl命令

/**
 * 新增工作空间
 *
 * @param workspace 工作空间名
 * @return boolean
 */
public static boolean addWorkspaceByCurl(String workspace) {
    String cmd = "curl -v -u " + geoUsername + ":" + geoPassword + " -XPOST -H \"Content-type: text/xml\"\n" +
            "  -d \"<workspace><name>" + workspace + "</name></workspace>\"\n" +
            "  " + url + "/rest/workspaces";
    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

# geoserver-manager

推荐java是用geoserver-manager maven

<dependency>
    <groupId>nl.pdok</groupId>
    <artifactId>geoserver-manager</artifactId>
    <version>1.7.0-pdok2</version>
</dependency>
1
2
3
4
5

application.yml中的配置

geoserver:
  url: http://localhost:8080/geoserver
  username: admin
  password: geoserver
  shpworkspace: shp
  imageworkspace: image
1
2
3
4
5
6

java核心代码

import com.zykj.didiao.common.util.common.FileUtil;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSGeoTIFFDatastoreEncoder;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import static org.toilelibre.libe.curl.Curl.curl;

@Component
public class GeoServer {

    private static Logger logger = LoggerFactory.getLogger(GeoServer.class);

    /**
     * geoServer配置
     */
    private static String url;
    private static String geoUsername;
    private static String geoPassword;
    //待发布矢量图层的工作区间
    public static String shpWorkspace;
    //待发布影像图层的工作空间
    public static String imageWorkspace;

    public static String stylePath;

    @Value("${geoserver.url}")
    public void setUrl(String url) {
        GeoServer.url = url;
    }

    @Value("${geoserver.username}")
    public void setGeoUsername(String geoUsername) {
        GeoServer.geoUsername = geoUsername;
    }

    @Value("${geoserver.password}")
    public void setGeoPassword(String geoPassword) {
        GeoServer.geoPassword = geoPassword;
    }

    @Value("${geoserver.shpworkspace}")
    public void setShpWorkspace(String shpWorkspace) {
        GeoServer.shpWorkspace = shpWorkspace;
    }

    @Value("${geoserver.imageworkspace}")
    public void setImageWorkspace(String imageWorkspace) {
        GeoServer.imageWorkspace = imageWorkspace;
    }

    @Value("${localdir.style}")
    public void setStylePath(String stylePath) {
        GeoServer.stylePath = stylePath;
    }

    /**
     * 判断工作区(workspace)是否存在,不存在则创建
     */
    public static void judgeWorkSpace(String workspace) throws MalformedURLException {
        URL u = new URL(url);
        GeoServerRESTManager manager = new GeoServerRESTManager(u, geoUsername, geoPassword);
        GeoServerRESTPublisher publisher = manager.getPublisher();
        List<String> workspaces = manager.getReader().getWorkspaceNames();
        if (!workspaces.contains(workspace)) {
            boolean createWorkspace = publisher.createWorkspace(workspace);
            logger.info("create workspace : " + createWorkspace);
        } else {
            logger.info("workspace已经存在了,workspace :" + workspace);
        }
    }

    /**
     * 判断存储是否存在
     *
     * @param store 存储名
     * @return boolean
     */
    public static boolean shpJudgeDatabase(String store) {
        try {
            URL u = new URL(url);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, geoUsername, geoPassword);
            RESTDataStore restStore = manager.getReader().getDatastore(shpWorkspace, store);
            if (restStore == null) {
                logger.info("数据存储不存在,可以创建!");
                return true;
            } else {
                logger.info("数据存储已经存在了,store:" + store);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 直接发布tiff影像到geoServer
     * 将遥感影像经过http传递过去对应的文件及
     *
     * @param storeName 数据存储名/图层名
     * @param fileUrl   本地文件地址
     */
    public static boolean releaseTiff(String storeName, String fileUrl) throws MalformedURLException, FileNotFoundException {
        URL u = new URL(url);
        //创建一个geoServer rest连接对象
        GeoServerRESTManager manager = new GeoServerRESTManager(u, geoUsername, geoPassword);
        //判断数据存储桶是否存在
        RESTDataStore restStore = manager.getReader().getDatastore(imageWorkspace, storeName);
        //如果不存在就创建一个数据存储,并发布
        if (restStore == null) {
            GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(storeName);
            gsGeoTIFFDatastoreEncoder.setWorkspaceName(imageWorkspace);
            //不确定是否有用
//            gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + fileUrl));
//            gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + "test.tif"));
            boolean createStore = manager.getStoreManager().create(imageWorkspace, gsGeoTIFFDatastoreEncoder);
            logger.info("create store (TIFF文件创建状态) : " + createStore);
            boolean publish = false;
            publish = manager.getPublisher().publishGeoTIFF(imageWorkspace, storeName, storeName, new File(fileUrl));


            logger.info("publish (TIFF文件发布状态) : " + publish);
            if (publish) {
                return true;
            }
        } else {
            logger.info("数据存储已经存在了,store:" + storeName);
        }
        return false;
    }


    /**
     * 方法一
     * 直接发布shp文件到geoServer
     * 将shp.zip通过http传递过去
     * 不主动设置样式和编码
     * <p>
     *
     * @param fileUrl 本地文件地址 zip格式
     * @param geocode 地理编码
     * @return boolean
     */
    public static boolean releaseShpByHttp(String fileUrl, String geocode) {
        try {
            File zipFile = new File(fileUrl);
            //存储名/图层名
            String storeName = FileUtil.getFileNameNoEx(zipFile.getName());
            GeoServerRESTReader reader = new GeoServerRESTReader(url, geoUsername, geoPassword);
            GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(url, geoUsername, geoPassword);
            RESTLayer layer = reader.getLayer(shpWorkspace, storeName);
            if (layer == null) {
                if (publisher.publishShp(shpWorkspace, storeName, storeName, zipFile, geocode)) {
                    logger.info("图层发布成功:" + storeName);
                    return true;
                } else {
                    logger.info("图层发布失败:" + storeName);
                }
            } else {
                logger.info("图层已经发布过了:" + storeName);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return false;

    }


    /**
     * 方法二
     * 向geoServer上传shp,并设置存储、图层名、样式和坐标系
     *
     * @param zipFilePath      压缩文件夹位置 完整文件地址
     * @param storeName        存储、图层名 英文
     * @param styleType        图层样式 shp工作空间下的对应样式
     * @param coordinateSystem 坐标系  EPSG:4326
     * @return boolean
     */
    public static boolean publishShp(String zipFilePath, String storeName, String styleType, String coordinateSystem) {
        if (coordinateSystem == null) {
            coordinateSystem = GeoServerRESTPublisher.DEFAULT_CRS;
        }
        try {
            //创建发布类,放入用户名密码和url
            GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(url, geoUsername, geoPassword);
            boolean b = geoServerRESTPublisher.publishShp(shpWorkspace, storeName,
                    new NameValuePair[]{new NameValuePair("charset", "GBK")},
                    //图层名称               指定用于发布资源的方法
                    storeName, GeoServerRESTPublisher.UploadMethod.FILE,
                    //zip图集的地址,直接压缩不要文件夹      坐标系         样式
                    new File(zipFilePath).toURI(), coordinateSystem, styleType);
            if (b) {
                return true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return false;
    }


    /**
     * 上传图层样式文件到geoServer,sld文件
     * 需要放入指定文件夹下
     *
     * @param styleType 样式文件名(不包含sld后缀)
     * @return boolean
     */
    public static boolean publishStyle(String styleType) {
        try {
            URL u = new URL(url);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, geoUsername, geoPassword);
            GeoServerRESTReader reader = manager.getReader();
            GeoServerRESTPublisher publisher = manager.getPublisher();
            //读取style文件
            String styleFile = stylePath + File.separator + styleType + ".sld";
            File file = new File(styleFile);
            //是否已经发布了改style
            if (!reader.existsStyle(shpWorkspace, styleType)) {
                publisher.publishStyleInWorkspace(shpWorkspace, file, styleType);
            }
            return true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 删除指定的数据存储
     *
     * @param workspace 工作空间
     * @param storeName 存储名
     * @return boolean
     */
    public static boolean removeStore(String workspace, String storeName) {
        URL u = null;
        try {
            u = new URL(url);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, geoUsername, geoPassword);
            GeoServerRESTStoreManager storeManager = manager.getStoreManager();
            GSAbstractStoreEncoder gsAbstractStoreEncoder;
            if (shpWorkspace.equals(workspace)) {
                gsAbstractStoreEncoder = new GSAbstractStoreEncoder(GeoServerRESTPublisher.StoreType.DATASTORES, storeName) {
                    @Override
                    protected String getValidType() {
                        return null;
                    }
                };
                gsAbstractStoreEncoder.setName(storeName);
                return storeManager.remove(workspace, gsAbstractStoreEncoder, true);
            }

            if (imageWorkspace.equals(workspace)) {
                gsAbstractStoreEncoder = new GSAbstractStoreEncoder(GeoServerRESTPublisher.StoreType.COVERAGESTORES, storeName) {
                    @Override
                    protected String getValidType() {
                        return null;
                    }
                };
                gsAbstractStoreEncoder.setName(storeName);
                return storeManager.remove(workspace, gsAbstractStoreEncoder, true);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
上次更新时间: 2022年5月20日星期五上午11点16分