package com.onemap.spotoverlap.utils; import com.google.gson.Gson; 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; import org.apache.http.util.EntityUtils; import org.geotools.data.FileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.Map; public class GeoServer { public static void main(String[] args) { try { List styles = getStyles("http://127.0.0.1:28085/geoserver", "admin", "geoserver", "spot"); System.out.println(styles); } catch (IOException e) { e.printStackTrace(); } } /** * 获取geoserver工作空间下的所有样式 * * @param geoserverurl * @param username * @param password * @param workspace * @return * @throws IOException */ public static List getStyles(String geoserverurl, String username, String password, String workspace) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); // TODO 设置 Basic Authentication 头 String auth = username + ":" + password; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); HttpGet getRequest = new HttpGet(geoserverurl + "/rest/workspaces/" + workspace + "/styles"); getRequest.setHeader("Authorization", "Basic " + encodedAuth); List style = new ArrayList<>(); try (CloseableHttpResponse response = client.execute(getRequest)) { String responseEntity = EntityUtils.toString(response.getEntity()); System.out.println(responseEntity); Gson gson = new Gson(); // 将 JSON 字符串转换为 Map Map responseMap = gson.fromJson(responseEntity, Map.class); Map styles = (Map) responseMap.get("styles"); List stylelist = (List) styles.get("style"); System.out.println(stylelist); for (int i = 0; i < stylelist.size(); i++) { Map cur = stylelist.get(i); style.add(workspace + ":" + cur.get("name")); } } return style; } /** * 将本地shp文件发布到geoserver并配置相应样式 * * @param geoserverurl * @param username * @param password * @param workspace * @param filepath * @param stylename * @throws IOException */ public static void publishShp(String geoserverurl, String username, String password, String workspace, String filepath, String stylename, String datasource, String charset) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); // TODO 设置 Basic Authentication 头 String auth = username + ":" + password; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); // TODO 创建数据存储时的XML请求体,指定文件路径 String xmlRequest = "" + "" + datasource + "" + "" + "file:" + filepath + "" + "Shapefile" + "" + charset + "" + "" + ""; HttpPost postRequest = new HttpPost(geoserverurl + "/rest/workspaces/" + workspace + "/datastores"); postRequest.setHeader("Authorization", "Basic " + encodedAuth); postRequest.setEntity(new StringEntity(xmlRequest)); postRequest.setHeader("Content-Type", "application/xml"); try (CloseableHttpResponse response = client.execute(postRequest)) { String responseEntity = EntityUtils.toString(response.getEntity()); System.out.println(responseEntity); } // TODO 创建发布图层的请求体 String publishRequest = "" + "" + datasource + "" + "" + datasource + "" + ""; postRequest = new HttpPost(geoserverurl + "/rest/workspaces/" + workspace + "/datastores/" + datasource + "/featuretypes"); postRequest.setEntity(new StringEntity(publishRequest)); postRequest.setHeader("Authorization", "Basic " + encodedAuth); postRequest.setHeader("Content-Type", "application/xml"); try (CloseableHttpResponse response = client.execute(postRequest)) { 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 { // TODO 创建图层样式的请求体 String styleRequest = "" + "" + stylename + "" + ""; String auth = username + ":" + password; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); HttpPut putRequest = new HttpPut(geoserverurl + "/rest/layers/" + datasource); 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); } client.close(); } catch (Exception e) { try { client.close(); } catch (IOException ioException) { ioException.printStackTrace(); } e.printStackTrace(); } } /** * 获取shp文件的epsg编码 * * @param shpFilePath * @return */ public static String getShpEPSG(String shpFilePath) { try { File shpFile = new File(shpFilePath); FileDataStore store = new ShapefileDataStoreFactory().createDataStore(shpFile.toURI().toURL()); CoordinateReferenceSystem crs = store.getSchema().getCoordinateReferenceSystem(); Integer srid = CRS.lookupEpsgCode(crs, false); return "EPSG:" + srid; } catch (Exception e) { e.printStackTrace(); } 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 = "" + " true" + " true" + " " + layer_name + "" + " " + " image/png" + " image/jpeg" + " " + " " + " " + " " + wmtsgridset + "" + " " + wmtszoomstart + "" + " " + wmtszoomend + "" + " " + wmtszoomstart + "" + " " + wmtszoomend + "" + " " + " " + " " + " 4" + " 4" + " " + " 0" + " 0" + " " + " " + " 0" + " true" + " "; 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(); } } }