Skip to content

Commit 015bfc5

Browse files
Enable relative URL on SLD ExternalGraphic
1 parent 9919ed8 commit 015bfc5

File tree

7 files changed

+115
-3
lines changed

7 files changed

+115
-3
lines changed

mapcopy.c

+1
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ int msCopyMap(mapObj *dst, mapObj *src)
12171217
MS_COPYSTELEM(resolution);
12181218
MS_COPYSTRING(dst->shapepath, src->shapepath);
12191219
MS_COPYSTRING(dst->mappath, src->mappath);
1220+
MS_COPYSTELEM(sldurl);
12201221

12211222
MS_COPYCOLOR(&(dst->imagecolor), &(src->imagecolor));
12221223

mapfile.c

+1
Original file line numberDiff line numberDiff line change
@@ -5890,6 +5890,7 @@ int initMap(mapObj *map)
58905890
map->cellsize = 0;
58915891
map->shapepath = NULL;
58925892
map->mappath = NULL;
5893+
map->sldurl = NULL;
58935894

58945895
MS_INIT_COLOR(map->imagecolor, 255,255,255,255); /* white */
58955896

mapogcsld.c

+49-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int msSLDApplySLDURL(mapObj *map, const char *szURL, int iLayer,
7272
int nStatus = MS_FAILURE;
7373

7474
if (map && szURL) {
75+
map->sldurl = (char*)szURL;
7576
pszSLDTmpFile = msTmpFile(map, map->mappath, NULL, "sld.xml");
7677
if (pszSLDTmpFile == NULL) {
7778
pszSLDTmpFile = msTmpFile(map, NULL, NULL, "sld.xml" );
@@ -110,6 +111,7 @@ int msSLDApplySLDURL(mapObj *map, const char *szURL, int iLayer,
110111
if (pszSLDbuf)
111112
nStatus = msSLDApplySLD(map, pszSLDbuf, iLayer, pszStyleLayerName, ppszLayerNames);
112113
}
114+
map->sldurl = NULL;
113115
}
114116

115117
msFree(pszSLDbuf);
@@ -2255,19 +2257,64 @@ int msSLDParseExternalGraphic(CPLXMLNode *psExternalGraphic,
22552257
if (psTmp && psTmp->psChild) {
22562258
pszURL = (char*)psTmp->psChild->pszValue;
22572259

2260+
char *symbolurl = NULL;
2261+
// Handle relative URL for ExternalGraphic
2262+
if (map->sldurl && !strstr(pszURL,"://"))
2263+
{
2264+
char *baseurl = NULL;
2265+
char *relpath = NULL;
2266+
symbolurl = malloc(sizeof(char)*MS_MAXPATHLEN);
2267+
if (pszURL[0] != '/') {
2268+
// Symbol file is relative to SLD file
2269+
// e.g. SLD : http://example.com/path/to/sld.xml
2270+
// and symbol: assets/symbol.svg
2271+
// lead to: http://example.com/path/to/assets/symbol.svg
2272+
baseurl = msGetPath(map->sldurl);
2273+
relpath = pszURL;
2274+
}
2275+
else
2276+
{
2277+
// Symbol file is relative to the root of SLD server
2278+
// e.g. SLD : http://example.com/path/to/sld.xml
2279+
// and symbol: /path/to/assets/symbol.svg
2280+
// lead to: http://example.com/path/to/assets/symbol.svg
2281+
baseurl = msStrdup(map->sldurl);
2282+
relpath = pszURL+1;
2283+
char * sep = strstr(baseurl,"://");
2284+
if (sep)
2285+
sep += 3;
2286+
else
2287+
sep = baseurl;
2288+
sep = strchr(sep,'/');
2289+
if (!sep)
2290+
sep = baseurl + strlen(baseurl);
2291+
sep[1] = '\0';
2292+
}
2293+
msBuildPath(symbolurl,baseurl,relpath);
2294+
msFree(baseurl);
2295+
}
2296+
else
2297+
{
2298+
// Absolute URL
2299+
// e.g. symbol: http://example.com/path/to/assets/symbol.svg
2300+
symbolurl = msStrdup(pszURL);
2301+
}
2302+
22582303
/* validate the ExternalGraphic parameter */
2259-
if(msValidateParameter(pszURL, msLookupHashTable(&(map->web.validation), "sld_external_graphic"),
2304+
if(msValidateParameter(symbolurl, msLookupHashTable(&(map->web.validation), "sld_external_graphic"),
22602305
NULL, NULL, NULL) != MS_SUCCESS) {
22612306
msSetError(MS_WEBERR, "SLD ExternalGraphic OnlineResource value fails to validate against sld_external_graphic VALIDATION", "mapserv()");
2307+
msFree(symbolurl);
22622308
return MS_FAILURE;
22632309
}
22642310

22652311

22662312
/*external symbols using http will be automaticallly downloaded. The file should be
22672313
saved in a temporary directory (msAddImageSymbol) #2305*/
22682314
psStyle->symbol = msGetSymbolIndex(&map->symbolset,
2269-
pszURL,
2315+
symbolurl,
22702316
MS_TRUE);
2317+
msFree(symbolurl);
22712318

22722319
if (psStyle->symbol > 0 && psStyle->symbol < map->symbolset.numsymbols)
22732320
psStyle->symbolname = msStrdup(map->symbolset.symbol[psStyle->symbol]->name);

mapserver.h

+4
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,10 @@ void msPopulateTextSymbolForLabelAndString(textSymbolObj *ts, labelObj *l, char
19021902

19031903
char *shapepath; /* where are the shape files located */
19041904
char *mappath; /* path of the mapfile, all path are relative to this path */
1905+
char *sldurl; // URL of SLD document as specified with "&SLD=..."
1906+
// WMS parameter. Currently this reference is used
1907+
// only in mapogcsld.c and has a NULL value
1908+
// outside that context.
19051909

19061910
#ifndef SWIG
19071911
paletteObj palette; /* holds a map palette */

msautotest/sld/data/danube.sld

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<StyledLayerDescriptor version="1.1.0">
2+
<NamedLayer>
3+
<Name>danube</Name>
4+
<UserStyle>
5+
<FeatureTypeStyle>
6+
<Rule>
7+
<LineSymbolizer>
8+
<Stroke>
9+
<SvgParameter name="stroke-width">5</SvgParameter>
10+
<SvgParameter name="stroke">#0000FF</SvgParameter>
11+
</Stroke>
12+
</LineSymbolizer>
13+
<LineSymbolizer>
14+
<Stroke>
15+
<GraphicStroke>
16+
<Graphic>
17+
<Size>10</Size>
18+
<Mark>
19+
<WellKnownName>circle</WellKnownName>
20+
<Stroke>
21+
<SvgParameter name="stroke">#FFFF00</SvgParameter>
22+
<SvgParameter name="stroke-width">2</SvgParameter>
23+
</Stroke>
24+
</Mark>
25+
</Graphic>
26+
<Gap>80</Gap>
27+
<InitialGap>40</InitialGap>
28+
</GraphicStroke>
29+
</Stroke>
30+
</LineSymbolizer>
31+
<LineSymbolizer>
32+
<Stroke>
33+
<GraphicStroke>
34+
<Graphic>
35+
<Size>30</Size>
36+
<ExternalGraphic>
37+
<OnlineResource xlink:type="simple" xlink:href="/sld/data/ship.svg" />
38+
<!--
39+
<OnlineResource xlink:type="simple" xlink:href="ship.svg" />
40+
<OnlineResource xlink:type="simple" xlink:href="http://localhost:8000/sld/data/ship.svg" />
41+
<OnlineResource xlink:type="simple" xlink:href="/sld/data/ship.svg" />
42+
-->
43+
<Format>image/svg+xml</Format>
44+
</ExternalGraphic>
45+
</Graphic>
46+
<Gap>80</Gap>
47+
<InitialGap>0</InitialGap>
48+
</GraphicStroke>
49+
</Stroke>
50+
</LineSymbolizer>
51+
</Rule>
52+
</FeatureTypeStyle>
53+
</UserStyle>
54+
</NamedLayer>
55+
</StyledLayerDescriptor>
47.3 KB
Loading

msautotest/sld/linemark.map

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
# RUN_PARMS: map_linemark.png [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3&REQUEST=GetMap&CRS=EPSG:4326&BBOX=40,7,50,31&FORMAT=image/png&WIDTH=720&HEIGHT=300&LAYERS=bg,danube" > [RESULT_DEMIME]
1010
#
1111
#
12-
# --- Reference output rendered with SLD
12+
# --- Reference output rendered with SLD body
1313
#
1414
# RUN_PARMS: sld_linemark.png [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3&REQUEST=GetMap&CRS=EPSG:4326&BBOX=40,7,50,31&FORMAT=image/png&WIDTH=720&HEIGHT=300&LAYERS=bg,danube&SLD_BODY=<StyledLayerDescriptor version=%221.1.0%22><NamedLayer><Name>danube</Name><UserStyle><FeatureTypeStyle><Rule><LineSymbolizer><Stroke><SvgParameter name=%22stroke-width%22>5</SvgParameter><SvgParameter name=%22stroke%22>#0000FF</SvgParameter></Stroke></LineSymbolizer><LineSymbolizer><Stroke><GraphicStroke><Graphic><Size>10</Size><Mark><WellKnownName>circle</WellKnownName><Stroke><SvgParameter name=%22stroke%22>#FFFF00</SvgParameter><SvgParameter name=%22stroke-width%22>2</SvgParameter></Stroke></Mark></Graphic><Gap>80</Gap><InitialGap>40</InitialGap></GraphicStroke></Stroke></LineSymbolizer><LineSymbolizer><Stroke><GraphicStroke><Graphic><Size>30</Size><ExternalGraphic><OnlineResource xlink:type=%22simple%22 xlink:href=%22http://localhost:8000/sld/data/ship.svg%22 /><Format>image/svg%2Bxml</Format></ExternalGraphic></Graphic><Gap>80</Gap><InitialGap>0</InitialGap></GraphicStroke></Stroke></LineSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>" > [RESULT_DEMIME]
1515
#
16+
# --- Reference output rendered with SLD URL
17+
#
18+
# RUN_PARMS: sld_url_linemark.png [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3&REQUEST=GetMap&CRS=EPSG:4326&BBOX=40,7,50,31&FORMAT=image/png&WIDTH=720&HEIGHT=300&LAYERS=bg,danube&SLD=http://localhost:8000/sld/data/danube.sld" > [RESULT_DEMIME]
19+
#
1620

1721

1822

0 commit comments

Comments
 (0)