diff --git a/lib/OpenLayers/Format/Text.js b/lib/OpenLayers/Format/Text.js index 13310364c8..3277fbf78d 100644 --- a/lib/OpenLayers/Format/Text.js +++ b/lib/OpenLayers/Format/Text.js @@ -20,6 +20,25 @@ */ 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 . 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 * Create a new parser for TSV Text. @@ -29,6 +48,19 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { * this instance. */ 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]); }, @@ -59,9 +91,12 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { var vals = currLine.split('\t'); var geometry = new OpenLayers.Geometry.Point(0,0); var attributes = {}; - var style = {}; + var style = this.defaultStyle ? + OpenLayers.Util.applyDefaults({}, this.defaultStyle) : + null; var icon, iconSize, iconOffset, overflow; var set = false; + var styleSet = false; for (var valIndex = 0; valIndex < vals.length; valIndex++) { if (vals[valIndex]) { if (columns[valIndex] == 'point') { @@ -78,20 +113,27 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { } else if (columns[valIndex] == 'title') attributes['title'] = vals[valIndex]; else if (columns[valIndex] == 'image' || - columns[valIndex] == 'icon') + columns[valIndex] == 'icon' && style) { style['externalGraphic'] = vals[valIndex]; - else if (columns[valIndex] == 'iconSize') { + styleSet = true; + } else if (columns[valIndex] == 'iconSize' && style) { var size = vals[valIndex].split(','); style['graphicWidth'] = parseFloat(size[0]); style['graphicHeight'] = parseFloat(size[1]); - } else if (columns[valIndex] == 'iconOffset') { + styleSet = true; + } else if (columns[valIndex] == 'iconOffset' && style) { var offset = vals[valIndex].split(','); style['graphicXOffset'] = parseFloat(offset[0]); style['graphicYOffset'] = parseFloat(offset[1]); + styleSet = true; } else if (columns[valIndex] == 'description') { attributes['description'] = vals[valIndex]; } else if (columns[valIndex] == 'overflow') { attributes['overflow'] = vals[valIndex]; + } else { + // For StyleMap filtering, allow additional + // columns to be stored as attributes. + attributes[columns[valIndex]] = vals[valIndex]; } } } @@ -99,7 +141,8 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { if (this.internalProjection && this.externalProjection) { geometry.transform(this.externalProjection, this.internalProjection); - } + } + style = styleSet ? style : null; var feature = new OpenLayers.Feature.Vector(geometry, attributes, style); features.push(feature); } diff --git a/tests/Format/Text.html b/tests/Format/Text.html new file mode 100644 index 0000000000..84b50f0694 --- /dev/null +++ b/tests/Format/Text.html @@ -0,0 +1,49 @@ + + + + + + + + + + + + diff --git a/tests/list-tests.html b/tests/list-tests.html index 7ca7aae739..52b23c84de 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -56,6 +56,7 @@
  • Format/JSON.html
  • Format/KML.html
  • Format/OSM.html
  • +
  • Format/Text.html
  • Format/SLD.html
  • Format/SLD/v1_0_0.html
  • Format/Filter.html