FIx style support in format.text to allow a set of style defaults.
Also, add an extractStyles option that can be configured to false to allow th use of stylemaps. r=ahocevar (Closes #1844) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9271 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -20,6 +20,25 @@
|
|||||||
*/
|
*/
|
||||||
OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: defaultStyle
|
||||||
|
* defaultStyle allows one to control the default styling of the features.
|
||||||
|
* It should be a symbolizer hash. By default, this is set to match the
|
||||||
|
* Layer.Text behavior, which is to use the default OpenLayers Icon.
|
||||||
|
*/
|
||||||
|
defaultStyle: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: extractStyles
|
||||||
|
* set to true to extract styles from the TSV files, using information
|
||||||
|
* from the image or icon, iconSize and iconOffset fields. This will result
|
||||||
|
* in features with a symbolizer (style) property set, using the
|
||||||
|
* default symbolizer specified in <defaultStyle>. Set to false if you
|
||||||
|
* wish to use a styleMap or OpenLayers.Style options to style your
|
||||||
|
* layer instead.
|
||||||
|
*/
|
||||||
|
extractStyles: true,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor: OpenLayers.Format.Text
|
* Constructor: OpenLayers.Format.Text
|
||||||
* Create a new parser for TSV Text.
|
* Create a new parser for TSV Text.
|
||||||
@@ -29,6 +48,19 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
|||||||
* this instance.
|
* this instance.
|
||||||
*/
|
*/
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
|
options = options || {};
|
||||||
|
|
||||||
|
if(options.extractStyles == true) {
|
||||||
|
options.defaultStyle = {
|
||||||
|
'externalGraphic': OpenLayers.Util.getImagesLocation() +
|
||||||
|
"marker.png",
|
||||||
|
'graphicXSize': 21,
|
||||||
|
'graphicYSize': 25,
|
||||||
|
'graphicXOffset': -10.5,
|
||||||
|
'graphicYOffset': -12.5
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
OpenLayers.Format.prototype.initialize.apply(this, [options]);
|
OpenLayers.Format.prototype.initialize.apply(this, [options]);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -59,9 +91,12 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
|||||||
var vals = currLine.split('\t');
|
var vals = currLine.split('\t');
|
||||||
var geometry = new OpenLayers.Geometry.Point(0,0);
|
var geometry = new OpenLayers.Geometry.Point(0,0);
|
||||||
var attributes = {};
|
var attributes = {};
|
||||||
var style = {};
|
var style = this.defaultStyle ?
|
||||||
|
OpenLayers.Util.applyDefaults({}, this.defaultStyle) :
|
||||||
|
null;
|
||||||
var icon, iconSize, iconOffset, overflow;
|
var icon, iconSize, iconOffset, overflow;
|
||||||
var set = false;
|
var set = false;
|
||||||
|
var styleSet = false;
|
||||||
for (var valIndex = 0; valIndex < vals.length; valIndex++) {
|
for (var valIndex = 0; valIndex < vals.length; valIndex++) {
|
||||||
if (vals[valIndex]) {
|
if (vals[valIndex]) {
|
||||||
if (columns[valIndex] == 'point') {
|
if (columns[valIndex] == 'point') {
|
||||||
@@ -78,20 +113,27 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
|||||||
} else if (columns[valIndex] == 'title')
|
} else if (columns[valIndex] == 'title')
|
||||||
attributes['title'] = vals[valIndex];
|
attributes['title'] = vals[valIndex];
|
||||||
else if (columns[valIndex] == 'image' ||
|
else if (columns[valIndex] == 'image' ||
|
||||||
columns[valIndex] == 'icon')
|
columns[valIndex] == 'icon' && style) {
|
||||||
style['externalGraphic'] = vals[valIndex];
|
style['externalGraphic'] = vals[valIndex];
|
||||||
else if (columns[valIndex] == 'iconSize') {
|
styleSet = true;
|
||||||
|
} else if (columns[valIndex] == 'iconSize' && style) {
|
||||||
var size = vals[valIndex].split(',');
|
var size = vals[valIndex].split(',');
|
||||||
style['graphicWidth'] = parseFloat(size[0]);
|
style['graphicWidth'] = parseFloat(size[0]);
|
||||||
style['graphicHeight'] = parseFloat(size[1]);
|
style['graphicHeight'] = parseFloat(size[1]);
|
||||||
} else if (columns[valIndex] == 'iconOffset') {
|
styleSet = true;
|
||||||
|
} else if (columns[valIndex] == 'iconOffset' && style) {
|
||||||
var offset = vals[valIndex].split(',');
|
var offset = vals[valIndex].split(',');
|
||||||
style['graphicXOffset'] = parseFloat(offset[0]);
|
style['graphicXOffset'] = parseFloat(offset[0]);
|
||||||
style['graphicYOffset'] = parseFloat(offset[1]);
|
style['graphicYOffset'] = parseFloat(offset[1]);
|
||||||
|
styleSet = true;
|
||||||
} else if (columns[valIndex] == 'description') {
|
} else if (columns[valIndex] == 'description') {
|
||||||
attributes['description'] = vals[valIndex];
|
attributes['description'] = vals[valIndex];
|
||||||
} else if (columns[valIndex] == 'overflow') {
|
} else if (columns[valIndex] == 'overflow') {
|
||||||
attributes['overflow'] = vals[valIndex];
|
attributes['overflow'] = vals[valIndex];
|
||||||
|
} else {
|
||||||
|
// For StyleMap filtering, allow additional
|
||||||
|
// columns to be stored as attributes.
|
||||||
|
attributes[columns[valIndex]] = vals[valIndex];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,6 +142,7 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
|
|||||||
geometry.transform(this.externalProjection,
|
geometry.transform(this.externalProjection,
|
||||||
this.internalProjection);
|
this.internalProjection);
|
||||||
}
|
}
|
||||||
|
style = styleSet ? style : null;
|
||||||
var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
|
var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
|
||||||
features.push(feature);
|
features.push(feature);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="../../lib/OpenLayers.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
function test_basic(t) {
|
||||||
|
t.plan(5);
|
||||||
|
var format = new OpenLayers.Format.Text({extractStyles: true});
|
||||||
|
var features = format.read(OpenLayers.Util.getElement("content").value);
|
||||||
|
t.eq(features[0].style, null, "style is null if no style props set");
|
||||||
|
var features = format.read(OpenLayers.Util.getElement("contentMarker").value);
|
||||||
|
t.eq(features[0].style.externalGraphic, "../../img/marker.png", "marker set correctly by default.");
|
||||||
|
|
||||||
|
var features = format.read(OpenLayers.Util.getElement("content2").value);
|
||||||
|
t.eq(features.length, 2, "two features read");
|
||||||
|
t.eq(features[0].style.externalGraphic, "marker.png", "marker set correctly from data.");
|
||||||
|
// t.eq(format.defaultStyle.externalGraphic, "../../img/marker.png", "defaultStyle externalGraphic not changed by pulling from data");
|
||||||
|
|
||||||
|
var format = new OpenLayers.Format.Text();
|
||||||
|
var features = format.read(OpenLayers.Util.getElement("content2").value);
|
||||||
|
t.eq(features[0].style, null, "null default style results in null style property, even with style properties used");
|
||||||
|
}
|
||||||
|
function test_extra(t) {
|
||||||
|
t.plan(1);
|
||||||
|
var format = new OpenLayers.Format.Text();
|
||||||
|
var features = format.read(OpenLayers.Util.getElement("content3").value);
|
||||||
|
t.eq(features[0].attributes.whee, "chicken", "extra attributes are stored for later use");
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<textarea id="content">
|
||||||
|
point
|
||||||
|
5,5
|
||||||
|
</textarea>
|
||||||
|
<textarea id="contentMarker">
|
||||||
|
point iconSize
|
||||||
|
5,5 8,8
|
||||||
|
</textarea>
|
||||||
|
<textarea id="content2">
|
||||||
|
point icon
|
||||||
|
5,5 marker.png
|
||||||
|
10,10 marker2.png
|
||||||
|
</textarea>
|
||||||
|
<textarea id="content3">
|
||||||
|
point whee
|
||||||
|
5,5 chicken
|
||||||
|
</textarea>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -56,6 +56,7 @@
|
|||||||
<li>Format/JSON.html</li>
|
<li>Format/JSON.html</li>
|
||||||
<li>Format/KML.html</li>
|
<li>Format/KML.html</li>
|
||||||
<li>Format/OSM.html</li>
|
<li>Format/OSM.html</li>
|
||||||
|
<li>Format/Text.html</li>
|
||||||
<li>Format/SLD.html</li>
|
<li>Format/SLD.html</li>
|
||||||
<li>Format/SLD/v1_0_0.html</li>
|
<li>Format/SLD/v1_0_0.html</li>
|
||||||
<li>Format/Filter.html</li>
|
<li>Format/Filter.html</li>
|
||||||
|
|||||||
Reference in New Issue
Block a user