| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<String> 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<String> 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<String> 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<String, Object> responseMap = gson.fromJson(responseEntity, Map.class);
- Map<String, Object> styles = (Map<String, Object>) responseMap.get("styles");
- List<Map> stylelist = (List<Map>) styles.get("style");
- System.out.println(stylelist);
- for (int i = 0; i < stylelist.size(); i++) {
- Map<String, Object> 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 = "<dataStore>" +
- "<name>" + datasource + "</name>" +
- "<connectionParameters>" +
- "<entry key=\"url\">file:" + filepath + "</entry>" +
- "<entry key=\"connector\">Shapefile</entry>" +
- "<entry key=\"charset\">" + charset + "</entry>" +
- "</connectionParameters>" +
- "</dataStore>";
- 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 = "<featureType>" +
- "<title>" + datasource + "</title>" +
- "<name>" + datasource + "</name>" +
- "</featureType>";
- 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 = "<layer><defaultStyle>" +
- "<name>" + stylename + "</name>" +
- "</defaultStyle></layer>";
- 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";
- }
- }
|