c67c2d87ac8c93ece08a512015cc67ee.png

GeoWebCache是切片服务器。它作为地图客户端和地图服务器之间的代理运行,在请求时缓存(存储)切片,从而消除了多余的请求处理,从而节省了大量的处理时间。尽管GeoWebCache也可以作为独立产品与其他地图服务器一起使用,但它与GeoServer集成在一起。

Java使用curl命令去调用GeoWebCache接口

- 使用springboot项目

需要引用的maven,用于执行curl命令

<dependency>    <groupId>org.toile-libre.libegroupId>    <artifactId>curlartifactId>    <version>LATESTversion>dependency>

application.yml中的配置

geoserver:  url: http://localhost:8080/geoserver  username: admin  password: geoserver  shpworkspace: shp  imageworkspace: image

java核心代码

import net.sf.json.JSONArray;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.StatusLine;import org.apache.http.util.EntityUtils;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import static org.toilelibre.libe.curl.Curl.curl;import static org.toilelibre.libe.curl.Curl.$;@Componentpublic class GeoWebCache {    private static String url;    private static String geoUsername;    private static String geoPassword;    //待发布矢量图层的工作区间    public static String shpWorkspace;    //待发布影像图层的工作空间    public static String imageWorkspace;    @Value("${geoserver.url}")    public void setUrl(String url) {        GeoWebCache.url = url;    }    @Value("${geoserver.username}")    public void setGeoUsername(String geoUsername) {        GeoWebCache.geoUsername = geoUsername;    }    @Value("${geoserver.password}")    public void setGeoPassword(String geoPassword) {        GeoWebCache.geoPassword = geoPassword;    }    @Value("${geoserver.shpworkspace}")    public void setShpWorkspace(String shpWorkspace) {        GeoWebCache.shpWorkspace = shpWorkspace;    }    @Value("${geoserver.imageworkspace}")    public void setImageWorkspace(String imageWorkspace) {        GeoWebCache.imageWorkspace = imageWorkspace;    }    /**     * 获取geoWebCache中的图层     * 并按shp和image 分类     *     * @return Map     */    public static Map<String, List<String>> getLayers() {        Map<String, List<String>> map = new HashMap();        String cmd = "curl -u " + geoUsername + ":" + geoPassword + " \"" + url + "/gwc/rest/layers\"";        List<String> shp = new ArrayList<>();        List<String> image = new ArrayList<>();        HttpResponse curl = curl(cmd);        HttpEntity entity = curl.getEntity();        if (entity != null) {            String result = null;            try {                result = EntityUtils.toString(entity, "UTF-8");                JSONArray jsonArray = JSONArray.fromObject(result);                for (Object o : jsonArray) {                    String str = o.toString();                    String str1 = str.substring(0, str.indexOf(":"));                    if ("shp".equals(str1)) {                        shp.add(str);                    }                    if ("image".equals(str1)) {                        image.add(str);                    }                }                map.put("shp", shp);                map.put("image", image);            } catch (IOException e) {                e.printStackTrace();            }        }        return map;    }    /**     * 指定图层进行切片操作     *     * @param layer     指定图层  shp:test     * @param zoomStart 1 切片开始层级     * @param zoomStop  15 切片结束层级     * @return boolean     */    public static boolean slice(String layer, int zoomStart, int zoomStop) {        int threadCount = 2;        String res = "";        String cmd = "curl  -u " + geoUsername + ":" + geoPassword + " -XPOST -H \"Content-type: text/xml\" -d '" + layer +                "4326" + zoomStart + "" + zoomStop + "image/png" + layer + "" + threadCount + "'  \""                + url + "/gwc/rest/seed/" + layer + ".xml\"";        HttpResponse curl = curl(cmd);        StatusLine statusLine = curl.getStatusLine();        return "HTTP/1.1 200 ".equals(statusLine.toString());    }    /**     * 获取切片的情况     *     * @param layer 指定图层     * @return Map     */    public static Map getSliceType(String layer) {        Map map = new HashMap();        //返回所有图层切片情况   curl -u : -XGET http://localhost:8080/geoserver/gwc/rest/seed.json        //返回指定图层的切片情况        String cmd = "curl  -u " + geoUsername + ":" + geoPassword + " -XGET " + url + "/gwc/rest/seed/" + layer + ".json";        HttpResponse curl = curl(cmd);        StatusLine statusLine = curl.getStatusLine();        if ("HTTP/1.1 200 ".equals(statusLine.toString())) {            HttpEntity entity = curl.getEntity();            try {                String result = EntityUtils.toString(entity, "UTF-8");                System.out.println(result);                JSONArray jsonArray = JSONArray.fromObject(result);                map.put("res", jsonArray);            } catch (IOException e) {                e.printStackTrace();            }        }        return map;    }    /**     * 停止所有正在进行的切片任务     *     * @return boolean     */    public static boolean stopAllSlice() {        String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -d \"kill_all=all\"  \"" + url + "/gwc/rest/seed\"";        HttpResponse curl = curl(cmd);        StatusLine statusLine = curl.getStatusLine();        return "HTTP/1.1 200 ".equals(statusLine.toString());    }    /**     * 停止指定图层的切片任务     *     * @return boolean     */    public static boolean stopSliceByLayer(String layer) {        String cmd = "curl -u " + geoUsername + ":" + geoPassword + " -d \"kill_all=all\"  \"" + url + "/gwc/rest/seed/" + layer + "\"";        HttpResponse curl = curl(cmd);        StatusLine statusLine = curl.getStatusLine();        return "HTTP/1.1 200 ".equals(statusLine.toString());    }}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐