1、导入依赖
<dependency>
<groupId>nl.pdok</groupId>
<artifactId>geoserver-manager</artifactId>
<version>1.7.0-pdok2</version>
</dependency>
2、代码示例
/**
* @param: url 默认为"http://localhost:8080/geoserver"
* @param: username 默认为"admin"
* @param: password 默认为"geoserver"
**/
public static void geoServerPublishShapefileData(String url, String username, String password) throws Exception {
//工作空间名称
String workspace_name = "my_workspace_name ";
//存储仓库名称
String store_name = "my_store_name ";
File zipLayerPath = new File("C:\\Users\\Desktop\\GX_POINT.zip");
String layerPath = "C:\\Users\\Desktop\\GX_POINT.shp";
String layerName = "GX_POINT";
NameValuePair nameValuePair1 = new NameValuePair("charset", "GBK");
// NameValuePair nameValuePair2 = new NameValuePair("Format", "image/png8");
NameValuePair[] nameValuePairsArr = new NameValuePair[1];
nameValuePairsArr[0] = nameValuePair1;
String srs = "EPSG:4524";
GeoServerRESTManager manager = new GeoServerRESTManager(new URL(url), username, password);
GeoServerRESTPublisher publisher = manager.getPublisher();
GeoServerRESTReader reader = manager.getReader();
GeoServerRESTStoreManager storeManager = manager.getStoreManager();
//判断工作空间(workSpace)是否存在,不存在则创建
List workspaces = reader.getWorkspaceNames();
if (!workspaces.contains(workspace_name)) {
boolean flag = publisher.createWorkspace(workspace_name);
System.out.println("create 工作空间: " + flag);
} else {
System.out.println("工作空间已经存在了,workspace:" + workspace_name);
}
//判断数据存储(dataStore)是否存在,不存在则创建
RESTDataStore dataStores = reader.getDatastore(workspace_name, store_name);
URL shpFileUrl = new URL("file://C:\\Users\\Desktop\\\GX_LINE.shp");
if (dataStores == null) {
GSShapefileDatastoreEncoder store = new GSShapefileDatastoreEncoder(store_name, shpFileUrl);
boolean flag = storeManager.create(workspace_name, store);
System.out.println("create 数据存储: " + flag);
} else {
System.out.println("数据存储已经存在了,store:" + store_name);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = reader.getLayer(workspace_name, layerName);
if (layer == null) {
//发布图层
boolean publishShp = publisher.publishShp(workspace_name, store_name, layerName, zipLayerPath, srs, nameValuePairsArr);
if (publishShp) {
System.out.println("publish : 发布图层成功");
} else {
System.out.println("publish : 发布图层失败");
}
} else {
System.out.println("表已经发布过了,table:" + store_name);
}
}
1、新增工作空间
http://localhost:8000/geoserver/rest/workspaces/mytest
{
"workspace": {
"name": "mytest"
}
}
2、新增存储仓库
http://localhost:8000/geoserver/rest/workspaces/mytest/datastores
{
"dataStore": {
"name": "mytest",
"type": "Directory of spatial files (shapefiles)",
"enabled": true,
"workspace": {
"name": "mytest",
"href": "http://localhost:8000/geoserver/rest/workspaces/mytest.json"
},
"connectionParameters": {
"entry": [
{
"@key": "charset",
"$": "GBK"
},
{
"@key": "filetype",
"$": "shapefile"
},
{
"@key": "create spatial index",
"$": "true"
},
{
"@key": "memory mapped buffer",
"$": "false"
},
{
"@key": "enable spatial index",
"$": "true"
},
{
"@key": "namespace",
"$": "htttp://mytest"
},
{
"@key": "cache and reuse memory maps",
"$": "true"
},
{
"@key": "skipScan",
"$": "true"
},
{
"@key": "url",
"$": "file://C:\\Users\\suc\\Desktop\\管线\\管线"
}
]
}
}
}
3、新增图层
http://localhost:8000/geoserver/rest/workspaces/myTest123/datastores/myTest123/featuretypes
<featureType>
<name>GX_POINT</name>
<nativeName>GX_POINT</nativeName>
<title>GX_POINT</title>
<srs>EPSG:4524</srs>
<nativeCRS>EPSG:4326</nativeCRS>
<store class="dataStore">
<name>myTest123</name>
</store>
</featureType>
4、切片图层预览
http://localhost:8000/geoserver/myTest123/gwc/demo/myTest123:GX_POINT?gridSet=EPSG:4326&format=application/json;type=geojson
5、瓦片缓存 GWC信息
http://localhost:8000/geoserver/myTest123/gwc/service/wmts?REQUEST=GetCapabilities