参考大佬博文:https://blog.youkuaiyun.com/qq_42402783/article/details/102821601
自己进行了改动,增加了设置坐标系及style样式。
环境介绍:
需要的jar包有:commons-codec-1.15.jar commons-io-2.2.jar commons-logging-1.2.jar geoserver-manager-1.7.0-pdok2.jar jdom-1.1.3.jar org.apache.commons.httpclient-3.1.0.jar slf4j-api-1.7.9.jar
环境下载:https://download.youkuaiyun.com/download/zl15145428/18581025
示例代码:
// GeoServer的连接配置
String url = "http://localhost:18081/geoserver";
String username = "admin";
String passwd = "geoserver";
String workSpace = "zhangsan"; // 待创建和发布图层的工作区名称workspace
try {
// 判断工作区(workspace)是否存在,不存在则创建
URL u = new URL(url);
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
GeoServerRESTPublisher publisher = manager.getPublisher();
List<String> workspaces = manager.getReader().getWorkspaceNames();
if (!workspaces.contains(workSpace)) {
boolean createws = publisher.createWorkspace(workSpace);
System.out.println("create ws : " + createws);
} else {
System.out.println("workspace已经存在了,workSpace :" + workSpace);
}
// 判断数据存储(datastore)是否已经存在,不存在则创建
String fileName = "E:\\xxxx.tiff";
String layerName = "xxxx";
String store_name = "xxxx.tiff"; // 待创建和发布图层的数据存储名称store
RESTDataStore restStore = manager.getReader().getDatastore(workSpace, store_name);
if (restStore == null) {
GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(store_name);
gsGeoTIFFDatastoreEncoder.setWorkspaceName(workSpace);
gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + fileName));
boolean createStore = manager.getStoreManager().create(workSpace, gsGeoTIFFDatastoreEncoder);
System.out.println("create store (TIFF文件创建状态) : " + createStore);
GeoServerRESTStyleManager styleManager = manager.getStyleManager();
if (!styleManager.existsStyle("sst2")) {
// 向geoserver写入style
boolean publishStyleInWorkspace = styleManager.publishStyleInWorkspace(workSpace, "abc", "sst2");
System.out.println("publishStyleInWorkspace : "+publishStyleInWorkspace);
}
boolean publish = publisher.publishGeoTIFF(workSpace,store_name, layerName, new File(fileName), "EPSG:4326",
GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED, "sst1", null);
System.out.println("publish (TIFF文件发布状态) : " + publish);
} else {
System.out.println("数据存储已经存在了,store:" + store_name);
}
} catch (Exception e) {
e.printStackTrace();
}
设置坐标系是:“EPSG::4326”
设置syle是:sst1,我这里已经上传到geoserver了,如果style没有上传,就需要“abc”处编写自己的style样式上传即可。
好了文章介绍结束,有疑问可以留言,欢迎点赞关注,路转粉不迷路。