一、入门
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前端如何动态地根据样式改变显示图层接口。