GeoServer.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.onemap.spotoverlap.utils;
  2. import com.google.gson.Gson;
  3. import org.apache.http.client.methods.CloseableHttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.client.methods.HttpPut;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.util.EntityUtils;
  11. import org.geotools.data.FileDataStore;
  12. import org.geotools.data.shapefile.ShapefileDataStoreFactory;
  13. import org.geotools.referencing.CRS;
  14. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.nio.charset.StandardCharsets;
  18. import java.util.ArrayList;
  19. import java.util.Base64;
  20. import java.util.List;
  21. import java.util.Map;
  22. public class GeoServer {
  23. public static void main(String[] args) {
  24. try {
  25. List<String> styles = getStyles("http://127.0.0.1:28085/geoserver", "admin", "geoserver", "spot");
  26. System.out.println(styles);
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. /**
  32. * 获取geoserver工作空间下的所有样式
  33. *
  34. * @param geoserverurl
  35. * @param username
  36. * @param password
  37. * @param workspace
  38. * @return
  39. * @throws IOException
  40. */
  41. public static List<String> getStyles(String geoserverurl, String username, String password, String workspace) throws IOException {
  42. CloseableHttpClient client = HttpClients.createDefault();
  43. // TODO 设置 Basic Authentication 头
  44. String auth = username + ":" + password;
  45. String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
  46. HttpGet getRequest = new HttpGet(geoserverurl + "/rest/workspaces/" + workspace + "/styles");
  47. getRequest.setHeader("Authorization", "Basic " + encodedAuth);
  48. List<String> style = new ArrayList<>();
  49. try (CloseableHttpResponse response = client.execute(getRequest)) {
  50. String responseEntity = EntityUtils.toString(response.getEntity());
  51. System.out.println(responseEntity);
  52. Gson gson = new Gson();
  53. // 将 JSON 字符串转换为 Map
  54. Map<String, Object> responseMap = gson.fromJson(responseEntity, Map.class);
  55. Map<String, Object> styles = (Map<String, Object>) responseMap.get("styles");
  56. List<Map> stylelist = (List<Map>) styles.get("style");
  57. System.out.println(stylelist);
  58. for (int i = 0; i < stylelist.size(); i++) {
  59. Map<String, Object> cur = stylelist.get(i);
  60. style.add(workspace + ":" + cur.get("name"));
  61. }
  62. }
  63. return style;
  64. }
  65. /**
  66. * 将本地shp文件发布到geoserver并配置相应样式
  67. *
  68. * @param geoserverurl
  69. * @param username
  70. * @param password
  71. * @param workspace
  72. * @param filepath
  73. * @param stylename
  74. * @throws IOException
  75. */
  76. public static void publishShp(String geoserverurl, String username, String password, String workspace, String filepath, String stylename, String datasource, String charset) throws IOException {
  77. // File file = new File(filepath);
  78. // String fileName = file.getName();
  79. // int dotIndex = fileName.lastIndexOf('.');
  80. // String datasource = dotIndex != -1 ? fileName.substring(0, dotIndex) : fileName;
  81. CloseableHttpClient client = HttpClients.createDefault();
  82. // TODO 设置 Basic Authentication 头
  83. String auth = username + ":" + password;
  84. String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
  85. // TODO 创建数据存储时的XML请求体,指定文件路径
  86. String xmlRequest = "<dataStore>" +
  87. "<name>" + datasource + "</name>" +
  88. "<connectionParameters>" +
  89. "<entry key=\"url\">file:" + filepath + "</entry>" +
  90. "<entry key=\"connector\">Shapefile</entry>" +
  91. "<entry key=\"charset\">" + charset + "</entry>" +
  92. "</connectionParameters>" +
  93. "</dataStore>";
  94. HttpPost postRequest = new HttpPost(geoserverurl + "/rest/workspaces/" + workspace + "/datastores");
  95. postRequest.setHeader("Authorization", "Basic " + encodedAuth);
  96. postRequest.setEntity(new StringEntity(xmlRequest));
  97. postRequest.setHeader("Content-Type", "application/xml");
  98. try (CloseableHttpResponse response = client.execute(postRequest)) {
  99. String responseEntity = EntityUtils.toString(response.getEntity());
  100. System.out.println(responseEntity);
  101. }
  102. // TODO 创建发布图层的请求体
  103. String publishRequest = "<featureType>" +
  104. "<title>" + datasource + "</title>" +
  105. "<name>" + datasource + "</name>" +
  106. "</featureType>";
  107. postRequest = new HttpPost(geoserverurl + "/rest/workspaces/" + workspace + "/datastores/" + datasource + "/featuretypes");
  108. postRequest.setEntity(new StringEntity(publishRequest));
  109. postRequest.setHeader("Authorization", "Basic " + encodedAuth);
  110. postRequest.setHeader("Content-Type", "application/xml");
  111. try (CloseableHttpResponse response = client.execute(postRequest)) {
  112. String responseEntity = EntityUtils.toString(response.getEntity());
  113. System.out.println(responseEntity);
  114. }
  115. updateStyle(stylename, geoserverurl, username, password, workspace + ":" + datasource);
  116. client.close();
  117. }
  118. public static void updateStyle(String stylename, String geoserverurl, String username, String password, String datasource) {
  119. try {
  120. CloseableHttpClient client = HttpClients.createDefault();
  121. // TODO 创建图层样式的请求体
  122. String styleRequest = "<layer><defaultStyle>" +
  123. "<name>" + stylename + "</name>" +
  124. "</defaultStyle></layer>";
  125. String auth = username + ":" + password;
  126. String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
  127. HttpPut putRequest = new HttpPut(geoserverurl + "/rest/layers/" + datasource);
  128. putRequest.setEntity(new StringEntity(styleRequest));
  129. putRequest.setHeader("Authorization", "Basic " + encodedAuth);
  130. putRequest.setHeader("Content-Type", "application/xml");
  131. try (CloseableHttpResponse response = client.execute(putRequest)) {
  132. String responseEntity = EntityUtils.toString(response.getEntity());
  133. System.out.println(responseEntity);
  134. }
  135. client.close();
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. /**
  141. * 获取shp文件的epsg编码
  142. *
  143. * @param shpFilePath
  144. * @return
  145. */
  146. public static String getShpEPSG(String shpFilePath) {
  147. try {
  148. File shpFile = new File(shpFilePath);
  149. // 通过Shapefile加载数据
  150. FileDataStore store = new ShapefileDataStoreFactory().createDataStore(shpFile.toURI().toURL());
  151. // 获取CoordinateReferenceSystem
  152. CoordinateReferenceSystem crs = store.getSchema().getCoordinateReferenceSystem();
  153. // 获取SRID
  154. Integer srid = CRS.lookupEpsgCode(crs, false);
  155. return "EPSG:" + srid;
  156. } catch (Exception e) {
  157. e.printStackTrace();
  158. }
  159. return "EPSG:4326";
  160. }
  161. }