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.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 { // 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)); // 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); } updateStyle(stylename, geoserverurl, username, password, workspace + ":" + datasource); client.close(); } public static void updateStyle(String stylename, String geoserverurl, String username, String password, String datasource) { try { CloseableHttpClient client = HttpClients.createDefault(); // 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) { e.printStackTrace(); } } /** * 获取shp文件的epsg编码 * * @param shpFilePath * @return */ 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) { e.printStackTrace(); } return "EPSG:4326"; } }