Adding support for TextSymbolizer writing in the SLD format. Thanks to Bart for the original patch. This provides basic expression handling for text labels. Read support later. r=ahocevar (closes #1542)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7333 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -425,7 +425,9 @@ OpenLayers.Format.SLD.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
|
||||
"stroke-width": "strokeWidth",
|
||||
"stroke-linecap": "strokeLinecap",
|
||||
"fill": "fillColor",
|
||||
"fill-opacity": "fillOpacity"
|
||||
"fill-opacity": "fillOpacity",
|
||||
"font-family": "fontFamily",
|
||||
"font-size": "fontSize"
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -732,6 +734,68 @@ OpenLayers.Format.SLD.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
|
||||
value: obj.symbolizer[obj.key]
|
||||
});
|
||||
},
|
||||
"TextSymbolizer": function(symbolizer) {
|
||||
var node = this.createElementNSPlus("TextSymbolizer");
|
||||
// add in optional Label
|
||||
if(symbolizer.label != null) {
|
||||
this.writeNode(node, "Label", symbolizer.label);
|
||||
}
|
||||
// add in optional Font
|
||||
if(symbolizer.fontFamily != null ||
|
||||
symbolizer.fontSize != null) {
|
||||
this.writeNode(node, "Font", symbolizer);
|
||||
}
|
||||
// add in optional Fill
|
||||
if(symbolizer.fillColor != null ||
|
||||
symbolizer.fillOpacity != null) {
|
||||
this.writeNode(node, "Fill", symbolizer);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
"Font": function(symbolizer) {
|
||||
var node = this.createElementNSPlus("Font");
|
||||
// add in CssParameters
|
||||
if(symbolizer.fontFamily) {
|
||||
this.writeNode(
|
||||
node, "CssParameter",
|
||||
{symbolizer: symbolizer, key: "fontFamily"}
|
||||
);
|
||||
}
|
||||
if(symbolizer.fontSize) {
|
||||
this.writeNode(
|
||||
node, "CssParameter",
|
||||
{symbolizer: symbolizer, key: "fontSize"}
|
||||
);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
"Label": function(label) {
|
||||
// only the simplest of ogc:expression handled
|
||||
// {label: "some text and a ${propertyName}"}
|
||||
var node = this.createElementNSPlus("Label");
|
||||
var tokens = label.split("${");
|
||||
node.appendChild(this.createTextNode(tokens[0]));
|
||||
var item, last;
|
||||
for(var i=1; i<tokens.length; i++) {
|
||||
item = tokens[i];
|
||||
last = item.indexOf("}");
|
||||
if(last > 0) {
|
||||
this.writeNode(
|
||||
node, "ogc:PropertyName",
|
||||
{property: item.substring(0, last)}
|
||||
);
|
||||
node.appendChild(
|
||||
this.createTextNode(item.substring(++last))
|
||||
);
|
||||
} else {
|
||||
// no ending }, so this is a literal ${
|
||||
node.appendChild(
|
||||
this.createTextNode("${" + item)
|
||||
);
|
||||
}
|
||||
}
|
||||
return node;
|
||||
},
|
||||
"PolygonSymbolizer": function(symbolizer) {
|
||||
var node = this.createElementNSPlus("PolygonSymbolizer");
|
||||
this.writeNode(node, "Fill", symbolizer);
|
||||
|
||||
@@ -353,4 +353,4 @@ OpenLayers.Style.createLiteral = function(value, context, feature) {
|
||||
* {Array} prefixes of the sld symbolizers. These are the
|
||||
* same as the main geometry types
|
||||
*/
|
||||
OpenLayers.Style.SYMBOLIZER_PREFIXES = ['Point', 'Line', 'Polygon'];
|
||||
OpenLayers.Style.SYMBOLIZER_PREFIXES = ['Point', 'Line', 'Polygon', 'Text'];
|
||||
|
||||
@@ -150,6 +150,42 @@
|
||||
|
||||
}
|
||||
|
||||
function test_writeTextSymbolizer(t) {
|
||||
t.plan(1);
|
||||
var parser = new OpenLayers.Format.SLD.v1_0_0();
|
||||
var symbolizer = {
|
||||
"Text": {
|
||||
"label": "This is the ${city} in ${state}.",
|
||||
"fontFamily": "Arial",
|
||||
"fontSize": 10,
|
||||
"fillColor": "blue"
|
||||
}
|
||||
};
|
||||
var node = parser.writers["sld"]["TextSymbolizer"].apply(
|
||||
parser, [symbolizer["Text"]]
|
||||
);
|
||||
|
||||
var expected =
|
||||
'<TextSymbolizer xmlns="http://www.opengis.net/sld">' +
|
||||
'<Label>' +
|
||||
'This is the ' +
|
||||
'<ogc:PropertyName xmlns:ogc="http://www.opengis.net/ogc">city</ogc:PropertyName>' +
|
||||
' in ' +
|
||||
'<ogc:PropertyName xmlns:ogc="http://www.opengis.net/ogc">state</ogc:PropertyName>' +
|
||||
'.' +
|
||||
'</Label>' +
|
||||
'<Font>' +
|
||||
'<CssParameter name="font-family">Arial</CssParameter>' +
|
||||
'<CssParameter name="font-size">10</CssParameter>' +
|
||||
'</Font>' +
|
||||
'<Fill>' +
|
||||
'<CssParameter name="fill">blue</CssParameter>' +
|
||||
'</Fill>' +
|
||||
'</TextSymbolizer>';
|
||||
|
||||
t.xml_eq(node, expected, "TextSymbolizer correctly written");
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user