I have worked with geoserver rest api and it is great, The only problem is that I have no idea how to post content of my sld using api, I have this code
public void AddNewStyle(String styledata, string workspace)
{
String requestUrl = RestServiceUrl + "/workspaces/" + workspace + "/styles";
String status = String.Empty;
object payload = styledata;
try
{
status = SendRestRequest(requestUrl, RequestMethod.Post, typeof(WorkspaceRequest), ref payload, ContentType.Html, AcceptType.Json);
}
catch (Exception ex)
{
throw new Exception("Adding datastores failed.", ex);
}
}
this code works fine with other requests. the only problem is I dont know what to send as styledata. I have set styledata as
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>Baft Farsode</Name>
<UserStyle>
<Name>BaftFarsoode</Name>
<Title>بافت فرسوده</Title>
<Abstract>BaftFarsoode area</Abstract>
<FeatureTypeStyle>
<FeatureTypeName>Feature</FeatureTypeName>
<Rule>
<Title>بافت فرسوده</Title>
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>Farsoode</ogc:PropertyName>
<ogc:Literal>1</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#4DFF4D</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>بدون داده</Title>
<ogc:Filter>
<ogc:PropertyIsNull>
<ogc:PropertyName>Farsoode</ogc:PropertyName>
</ogc:PropertyIsNull>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#ffffff</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>غیر فرسوده</Title>
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>Farsoode</ogc:PropertyName>
<ogc:Literal>0</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#FF4D4D</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>مرز املاک</Title>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke-width">0.2</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
but it gives error, I think there must be another way to add style data as raw xml using rest api.
Can someone help me with this?
here is an example but it uploads sld file into geoserver. I want to send data as a raw string not by uploading. http://docs.geoserver.org/latest/en/user/rest/examples/curl.html#creating-a-layer-style-sld-package
-
Did you ever get it solved?nathanvda– nathanvda2018年01月02日 16:37:45 +00:00Commented Jan 2, 2018 at 16:37
-
@nathanvda yes, Content-type: text/xml should make it workMajid Hojati– Majid Hojati2018年01月02日 17:41:03 +00:00Commented Jan 2, 2018 at 17:41
-
@MajidHojati Did you have any problem with unicode SLD expression ?Mostafa Fallah– Mostafa Fallah2020年08月11日 19:23:44 +00:00Commented Aug 11, 2020 at 19:23
-
1@MostafaFallah Geoserver is based on jetty(or tomcat) server which listens to port number 8080, if my memory serves me well, and both are java based. I guess in your configuration you are using windows server as proxy server for geoservices. so just configure your java version which is installed on the windows to add Unicode support.Majid Hojati– Majid Hojati2020年08月13日 01:16:25 +00:00Commented Aug 13, 2020 at 1:16
-
1@MostafaFallah nice, Good luck!Majid Hojati– Majid Hojati2020年08月14日 19:09:37 +00:00Commented Aug 14, 2020 at 19:09
2 Answers 2
It's not clear what libraries you are using but as the example you link to shows you need to send the sld file (zipped and encoded) to http://localhost:8080/geoserver/rest/styles
with the Content-type
set.
I've never tried but I expect you could send just the XML data with a Content-type: text/xml
but it will take longer than sending a compressed file.
-
Hi, I am using C#, I found the way, I am posting the answerMajid Hojati– Majid Hojati2016年12月06日 09:20:00 +00:00Commented Dec 6, 2016 at 9:20
If you're sure that .xml is valid (you can check its validation via this online tool) you need to add the following line to your request's header as mentioned in geoserver official documentation (here)
{content-type:'application/vnd.ogc.sld+xml'}
Here I give an example. First of all I created a sld string on my Node server using javascript:
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
xmlns="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<UserStyle>
<Name>sld_tit_12</Name>
<Title>No Title Existed.</Title>
<Abstract>No Abstract Existed.</Abstract>
<FeatureTypeStyle>
<Rule>
<Title>class_0</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>tit_12</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>0</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>281</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#fcd1a4</CssParameter>
<CssParameter name="fill">0.7</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#000</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>class_1</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>tit_12</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>281</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>547</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#fab072</CssParameter>
<CssParameter name="fill">0.7</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#000</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>class_2</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>tit_12</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>547</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>905</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#f6904c</CssParameter>
<CssParameter name="fill">0.7</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#000</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>class_3</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>tit_12</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>905</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>2075</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#d75538</CssParameter>
<CssParameter name="fill">0.7</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#000</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>class_4</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>tit_12</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>2075</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>2889820</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#914032</CssParameter>
<CssParameter name="fill">0.7</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#000</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
The above string is used to classify features on map. Then I made a request like this:
const geoserverRESTAPI = 'http://localhost:8080/geoserver/rest/';
const options = {
url: `${geoserverRESTAPI}styles`,
method: 'POST',
headers: { 'Content-type': 'application/vnd.ogc.sld+xml' },
body: sldString, //in string format
auth: {
user: '<username>',
pass: '<password>'
}
}
Don't forget to put your own username and password in auth.
If everything is OK, you will be received 201 - created response.
-
Did you know what changes needs for working this code on UTF-8 ?Mostafa Fallah– Mostafa Fallah2020年08月11日 19:43:10 +00:00Commented Aug 11, 2020 at 19:43
-
What problem do you have with UTF-8 encoding?ArashMad– ArashMad2020年08月12日 14:48:55 +00:00Commented Aug 12, 2020 at 14:48
-
I described here my question. this is main problem that I can not have persian characters on the geo server style part. gis.stackexchange.com/questions/371140/…Mostafa Fallah– Mostafa Fallah2020年08月12日 16:05:26 +00:00Commented Aug 12, 2020 at 16:05
-
Thanks Arash, My problem was fixed gis.stackexchange.com/questions/371140/…Mostafa Fallah– Mostafa Fallah2020年08月14日 06:00:31 +00:00Commented Aug 14, 2020 at 6:00