通过geoserver的rest接口动态地新增样式和修改样式

一、入门

1.geoserver rest接口主页

REST — GeoServer 2.21.x User Manual

 2.geoserver rest关于style的接口地址

https://docs.geoserver.org/latest/en/api/#1.0.0/styles.yaml

 二、接口测试:

1.新增样式接口测试

在仔细分析上面提到的styles的接口介绍后,自己通过ApiPost工具测试,琢磨了好久,才测试成功。具体请求下图,每一要点都很重要!

新增样式的结果在geoserver主页面中样式栏里能看到新增的样式

 

 

 

 

 2.修改样式文件 ,使用场景为比如我们想把线样式的颜色和宽度调整,我们即可用到该接口,传递样式名称,和修改后的样式xml文件

 

 三、通过后台代码实现:

1.工具类,用于新增样式和修改样式

 /**
     * RestTemplate 带form参数和body参数的post方法
     * @param url post的url地址
     * @param bodyContent body里的内容
     * @param params query的多个参数对象
     * @param mediaType Content-Type值
     * @param userName 认证的用户名
     * @param password 认证的密码
     * @return
     */
    public static String httpPost(String url, String bodyContent,MultiValueMap<String, String> params,MediaType mediaType,String userName,String password) {
        RestTemplate restTemplate = new RestTemplate();
        //BasicAuth认证
        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(userName,password));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        HttpEntity<String> request = new HttpEntity<>(bodyContent, headers);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
        String responseEntity = restTemplate.postForObject(builder.toUriString(), request, String.class);
        return responseEntity;
    }

    /**
     *RestTemplate put方法,进行修改数据
     * @param  url put的url地址
     * @param bodyContent  body里的内容
     * @param mediaType Content-Type值
     * @param userName 认证的用户名
     * @param password 认证的密码
     */
    public static void httpPut(String url, String bodyContent,MediaType mediaType,String userName,String password) {
        RestTemplate restTemplate = new RestTemplate();
        //BasicAuth认证
        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(userName,password));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        HttpEntity<String> request = new HttpEntity<>(bodyContent, headers);
        restTemplate.put(url,request);
    }

2.在controller中封装实现新增和修改样式,前端只需要传递样式名称、线宽、线的颜色即可。

import com.jjgis.riskassess.utils.RestTemplateUtil;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author xulipeng  2022/9/22 17:16
 */
@RestController
public class GeoserverController {
    @CrossOrigin
    @PostMapping(value="addNewStyle")
    public String addNewStyle(String styleName,String strokeWidth,String strokeColor){
        MultiValueMap<String, String> params =new LinkedMultiValueMap<>();
        params.add("name",styleName);
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<StyledLayerDescriptor version=\"1.0.0\" \n" +
                " xsi:schemaLocation=\"http://www.opengis.net/sld StyledLayerDescriptor.xsd\" \n" +
                " xmlns=\"http://www.opengis.net/sld\" \n" +
                " xmlns:ogc=\"http://www.opengis.net/ogc\" \n" +
                " xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" +
                " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "  <!-- a Named Layer is the basic building block of an SLD document -->\n" +
                "  <NamedLayer>\n" +
                "    <Name>test_line1</Name>\n" +
                "    <UserStyle>\n" +
                "    <!-- Styles can have names, titles and abstracts -->\n" +
                "      <Title>Default Line</Title>\n" +
                "      <Abstract>A sample style that draws a line</Abstract>\n" +
                "      <!-- FeatureTypeStyles describe how to render different features -->\n" +
                "      <!-- A FeatureTypeStyle for rendering lines -->\n" +
                "      <FeatureTypeStyle>\n" +
                "        <Rule>\n" +
                "          <Name>rule1</Name>\n" +
                "          <Title>Blue Line</Title>\n" +
                "          <Abstract>A solid blue line with a 1 pixel width</Abstract>\n" +
                "          <LineSymbolizer>\n" +
                "            <Stroke>\n" +
                "              <CssParameter name=\"stroke\">"+strokeColor+"</CssParameter>\n" +
                "              <CssParameter name=\"width\">"+strokeWidth+"</CssParameter>\n"  +
                "            </Stroke>\n" +
                "          </LineSymbolizer>\n" +
                "        </Rule>\n" +
                "      </FeatureTypeStyle>\n" +
                "    </UserStyle>\n" +
                "  </NamedLayer>\n" +
                "</StyledLayerDescriptor>";
        return  RestTemplateUtil.httpPost("http://localhost:8080/geoserver/rest/styles",xml,params, MediaType.parseMediaType("application/vnd.ogc.sld+xml"),"admin","geoserver");

    }


    @CrossOrigin
    @PutMapping(value="modifyStyle")
    public void modifyStyle(String styleName,String strokeWidth,String strokeColor){
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<StyledLayerDescriptor version=\"1.0.0\" \n" +
                " xsi:schemaLocation=\"http://www.opengis.net/sld StyledLayerDescriptor.xsd\" \n" +
                " xmlns=\"http://www.opengis.net/sld\" \n" +
                " xmlns:ogc=\"http://www.opengis.net/ogc\" \n" +
                " xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" +
                " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "  <!-- a Named Layer is the basic building block of an SLD document -->\n" +
                "  <NamedLayer>\n" +
                "    <Name>test_line1</Name>\n" +
                "    <UserStyle>\n" +
                "    <!-- Styles can have names, titles and abstracts -->\n" +
                "      <Title>Default Line</Title>\n" +
                "      <Abstract>A sample style that draws a line</Abstract>\n" +
                "      <!-- FeatureTypeStyles describe how to render different features -->\n" +
                "      <!-- A FeatureTypeStyle for rendering lines -->\n" +
                "      <FeatureTypeStyle>\n" +
                "        <Rule>\n" +
                "          <Name>rule1</Name>\n" +
                "          <Title>Blue Line</Title>\n" +
                "          <Abstract>A solid blue line with a 1 pixel width</Abstract>\n" +
                "          <LineSymbolizer>\n" +
                "            <Stroke>\n" +
                "              <CssParameter name=\"stroke\">"+strokeColor+"</CssParameter>\n" +
                "              <CssParameter name=\"width\">"+strokeWidth+"</CssParameter>\n"  +
                "            </Stroke>\n" +
                "          </LineSymbolizer>\n" +
                "        </Rule>\n" +
                "      </FeatureTypeStyle>\n" +
                "    </UserStyle>\n" +
                "  </NamedLayer>\n" +
                "</StyledLayerDescriptor>";
          RestTemplateUtil.httpPut("http://localhost:8080/geoserver/rest/styles/"+styleName+"",xml,MediaType.parseMediaType("application/vnd.ogc.sld+xml"),"admin","geoserver");
    }
}

四、未完待续、、、、接下来会讲到arcgis js前端如何动态地根据样式改变显示图层接口。

GeoServer 是一个开源的地理数据服务软件,它支持 RESTful API 以及其他协议,可以通过这些接口来管理和发布地理数据。GeoServer RESTful API 提供了基于 HTTP 的接口,可以通过 HTTP GET, POST, PUT, DELETE 等方法来管理 GeoServer 中的数据。 下面是一些常用的 GeoServer RESTful API 接口: 1. 获取 GeoServer 版本信息:http://localhost:8080/geoserver/rest/about/version.xml 2. 获取所有工作空间信息:http://localhost:8080/geoserver/rest/workspaces.xml 3. 获取指定工作空间中的所有数据存储信息:http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores.xml 4. 获取指定数据存储中的所有图层信息:http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes.xml 5. 创建新的工作空间:POST http://localhost:8080/geoserver/rest/workspaces.xml 6. 创建新的数据存储:POST http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores.xml 7. 创建新的图层:POST http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes.xml 8. 删除工作空间:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}.xml 9. 删除数据存储:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}.xml 10. 删除图层:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes/{featureType}.xml 以上是一些常用的 GeoServer RESTful API 接口,你可以根据自己的需要进行调用。需要注意的是,在调用这些接口之前,需要先进行身份验证,获取到访问 GeoServer 的权限。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值