| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- 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<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 {
- 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);
- }
- // 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 = "<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) {
- 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 = "<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();
- }
- }
- }
|