|
|
@@ -1,10 +1,7 @@
|
|
|
package com.onemap.spotoverlap.utils;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
-import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
-import org.apache.http.client.methods.HttpGet;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
-import org.apache.http.client.methods.HttpPut;
|
|
|
+import org.apache.http.client.methods.*;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
@@ -81,12 +78,8 @@ public class GeoServer {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static void publishShp(String geoserverurl, String username, String password, String workspace, String filepath, String stylename, String datasource, String charset) throws IOException {
|
|
|
-// File file = new File(filepath);
|
|
|
-// String fileName = file.getName();
|
|
|
-// int dotIndex = fileName.lastIndexOf('.');
|
|
|
-// String datasource = dotIndex != -1 ? fileName.substring(0, dotIndex) : fileName;
|
|
|
-
|
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+
|
|
|
// TODO 设置 Basic Authentication 头
|
|
|
String auth = username + ":" + password;
|
|
|
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
@@ -122,13 +115,26 @@ public class GeoServer {
|
|
|
String responseEntity = EntityUtils.toString(response.getEntity());
|
|
|
System.out.println(responseEntity);
|
|
|
}
|
|
|
+
|
|
|
+ // TODO 更新符号样式配置
|
|
|
updateStyle(stylename, geoserverurl, username, password, workspace + ":" + datasource);
|
|
|
+
|
|
|
+ //TODO wanger 是否发布为WMTS服务 调用切片
|
|
|
client.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新服务的样式配置
|
|
|
+ *
|
|
|
+ * @param stylename
|
|
|
+ * @param geoserverurl
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @param datasource
|
|
|
+ */
|
|
|
public static void updateStyle(String stylename, String geoserverurl, String username, String password, String datasource) {
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
try {
|
|
|
- CloseableHttpClient client = HttpClients.createDefault();
|
|
|
// TODO 创建图层样式的请求体
|
|
|
String styleRequest = "<layer><defaultStyle>" +
|
|
|
"<name>" + stylename + "</name>" +
|
|
|
@@ -145,6 +151,11 @@ public class GeoServer {
|
|
|
}
|
|
|
client.close();
|
|
|
} catch (Exception e) {
|
|
|
+ try {
|
|
|
+ client.close();
|
|
|
+ } catch (IOException ioException) {
|
|
|
+ ioException.printStackTrace();
|
|
|
+ }
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
@@ -158,11 +169,8 @@ public class GeoServer {
|
|
|
public static String getShpEPSG(String shpFilePath) {
|
|
|
try {
|
|
|
File shpFile = new File(shpFilePath);
|
|
|
- // 通过Shapefile加载数据
|
|
|
FileDataStore store = new ShapefileDataStoreFactory().createDataStore(shpFile.toURI().toURL());
|
|
|
- // 获取CoordinateReferenceSystem
|
|
|
CoordinateReferenceSystem crs = store.getSchema().getCoordinateReferenceSystem();
|
|
|
- // 获取SRID
|
|
|
Integer srid = CRS.lookupEpsgCode(crs, false);
|
|
|
return "EPSG:" + srid;
|
|
|
} catch (Exception e) {
|
|
|
@@ -170,4 +178,175 @@ public class GeoServer {
|
|
|
}
|
|
|
return "EPSG:4326";
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建服务的切片配置
|
|
|
+ *
|
|
|
+ * @param geoserverurl
|
|
|
+ * @param geoserverusername
|
|
|
+ * @param geoserverpassword
|
|
|
+ * @param geoserverworkspace
|
|
|
+ * @param datasource
|
|
|
+ * @param wmtsgridset
|
|
|
+ * @param wmtsseedtype
|
|
|
+ * @param wmtszoomstart
|
|
|
+ * @param wmtszoomend
|
|
|
+ */
|
|
|
+ public static void caching_layer(String geoserverurl,
|
|
|
+ String geoserverusername,
|
|
|
+ String geoserverpassword,
|
|
|
+ String geoserverworkspace,
|
|
|
+ String datasource,
|
|
|
+ String wmtsgridset,
|
|
|
+ String wmtsseedtype,
|
|
|
+ Integer wmtszoomstart,
|
|
|
+ Integer wmtszoomend) {
|
|
|
+
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ try {
|
|
|
+ String layer_name = geoserverworkspace + ":" + datasource;
|
|
|
+ String url = String.format("%s/gwc/rest/layers/%s", geoserverurl, layer_name);
|
|
|
+ // TODO 创建图层样式的请求体
|
|
|
+ String styleRequest = "<GeoServerLayer>" +
|
|
|
+ " <enabled>true</enabled>" +
|
|
|
+ " <inMemoryCached>true</inMemoryCached>" +
|
|
|
+ " <name>" + layer_name + "</name>" +
|
|
|
+ " <mimeFormats>" +
|
|
|
+ " <string>image/png</string>" +
|
|
|
+ " <string>image/jpeg</string>" +
|
|
|
+ " </mimeFormats>" +
|
|
|
+ " <gridSubsets>" +
|
|
|
+ " <gridSubset>" +
|
|
|
+ " <gridSetName>" + wmtsgridset + "</gridSetName>" +
|
|
|
+ " <zoomStart>" + wmtszoomstart + "</zoomStart>" +
|
|
|
+ " <zoomStop>" + wmtszoomend + "</zoomStop>" +
|
|
|
+ " <minCachedLevel>" + wmtszoomstart + "</minCachedLevel>" +
|
|
|
+ " <maxCachedLevel>" + wmtszoomend + "</maxCachedLevel>" +
|
|
|
+ " </gridSubset>" +
|
|
|
+ " </gridSubsets>" +
|
|
|
+ " <metaWidthHeight>" +
|
|
|
+ " <int>4</int>" +
|
|
|
+ " <int>4</int>" +
|
|
|
+ " </metaWidthHeight>" +
|
|
|
+ " <expireCache>0</expireCache>" +
|
|
|
+ " <expireClients>0</expireClients>" +
|
|
|
+ " <parameterFilters>" +
|
|
|
+ " </parameterFilters>" +
|
|
|
+ " <gutter>0</gutter>" +
|
|
|
+ " <autoCacheStyles>true</autoCacheStyles>" +
|
|
|
+ " </GeoServerLayer>";
|
|
|
+ String auth = geoserverusername + ":" + geoserverpassword;
|
|
|
+ String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
+ HttpPost putRequest = new HttpPost(url);
|
|
|
+ putRequest.setEntity(new StringEntity(styleRequest));
|
|
|
+ putRequest.setHeader("Authorization", "Basic " + encodedAuth);
|
|
|
+ putRequest.setHeader("Content-Type", "application/xml");
|
|
|
+ try (CloseableHttpResponse response = client.execute(putRequest)) {
|
|
|
+ String responseEntity = EntityUtils.toString(response.getEntity());
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ seed_caching_layer(geoserverurl, geoserverusername, geoserverpassword, geoserverworkspace, datasource, wmtsgridset, wmtsseedtype, wmtszoomstart, wmtszoomend);
|
|
|
+ }
|
|
|
+ client.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ try {
|
|
|
+ client.close();
|
|
|
+ } catch (IOException ioException) {
|
|
|
+ ioException.printStackTrace();
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送切片指令 seed
|
|
|
+ *
|
|
|
+ * @param geoserverurl
|
|
|
+ * @param geoserverusername
|
|
|
+ * @param geoserverpassword
|
|
|
+ * @param geoserverworkspace
|
|
|
+ * @param datasource
|
|
|
+ * @param wmtsgridset
|
|
|
+ * @param wmtsseedtype
|
|
|
+ * @param wmtszoomstart
|
|
|
+ * @param wmtszoomend
|
|
|
+ */
|
|
|
+ private static void seed_caching_layer(String geoserverurl,
|
|
|
+ String geoserverusername,
|
|
|
+ String geoserverpassword,
|
|
|
+ String geoserverworkspace,
|
|
|
+ String datasource,
|
|
|
+ String wmtsgridset,
|
|
|
+ String wmtsseedtype,
|
|
|
+ Integer wmtszoomstart,
|
|
|
+ Integer wmtszoomend) {
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ try {
|
|
|
+ String layer_name = geoserverworkspace + ":" + datasource;
|
|
|
+ String url = String.format("%s/gwc/rest/seed/%s?threadCount=4&" +
|
|
|
+ "type=%s" +
|
|
|
+ "&gridSetId=%s" +
|
|
|
+ "&tileFormat=image/png" +
|
|
|
+ "&zoomStart=%s" +
|
|
|
+ "&zoomStop=%s" +
|
|
|
+ "¶meter_STYLES=" +
|
|
|
+ "&minX=&minY=&maxX=&maxY=" +
|
|
|
+ "&tileFailureRetryCount=-1" +
|
|
|
+ "&tileFailureRetryWaitTime=100" +
|
|
|
+ "&totalFailuresBeforeAborting=1000", geoserverurl, layer_name, wmtsseedtype, wmtsgridset, wmtszoomstart, wmtszoomend);
|
|
|
+ String auth = geoserverusername + ":" + geoserverpassword;
|
|
|
+ String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
+ HttpPost putRequest = new HttpPost(url);
|
|
|
+ putRequest.setHeader("Authorization", "Basic " + encodedAuth);
|
|
|
+ putRequest.setHeader("Content-Type", "application/json");
|
|
|
+ try (CloseableHttpResponse response = client.execute(putRequest)) {
|
|
|
+ String responseEntity = EntityUtils.toString(response.getEntity());
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ }
|
|
|
+ client.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ try {
|
|
|
+ client.close();
|
|
|
+ } catch (IOException ioException) {
|
|
|
+ ioException.printStackTrace();
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除指定数据存储
|
|
|
+ *
|
|
|
+ * @param geoserverurl
|
|
|
+ * @param geoserverusername
|
|
|
+ * @param geoserverpassword
|
|
|
+ * @param geoserverworkspace
|
|
|
+ * @param datasource
|
|
|
+ */
|
|
|
+ public static void delete_datasource(String geoserverurl,
|
|
|
+ String geoserverusername,
|
|
|
+ String geoserverpassword,
|
|
|
+ String geoserverworkspace,
|
|
|
+ String datasource) {
|
|
|
+
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ try {
|
|
|
+ String url = String.format("%s/rest/workspaces/%s/datastores/%s?recurse=true", geoserverurl, geoserverworkspace, datasource);
|
|
|
+ String auth = geoserverusername + ":" + geoserverpassword;
|
|
|
+ String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
+ HttpDelete putRequest = new HttpDelete(url);
|
|
|
+ putRequest.setHeader("Authorization", "Basic " + encodedAuth);
|
|
|
+ try (CloseableHttpResponse response = client.execute(putRequest)) {
|
|
|
+ String responseEntity = EntityUtils.toString(response.getEntity());
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ }
|
|
|
+ client.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ try {
|
|
|
+ client.close();
|
|
|
+ } catch (IOException ioException) {
|
|
|
+ ioException.printStackTrace();
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|