1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under a modified BSD license.
2 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
3 * for the full text of the license. */
6 * @requires OpenLayers/Feature/Vector.js
7 * @requires OpenLayers/Geometry/Point.js
11 * Class: OpenLayers.Format.Text
12 * Read Text format. Create a new instance with the <OpenLayers.Format.Text>
13 * constructor. This reads text which is formatted like CSV text, using
14 * tabs as the seperator by default. It provides parsing of data originally
15 * used in the MapViewerService, described on the wiki. This Format is used
16 * by the <OpenLayers.Layer.Text> class.
19 * - <OpenLayers.Format>
21 OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
24 * APIProperty: defaultStyle
25 * defaultStyle allows one to control the default styling of the features.
26 * It should be a symbolizer hash. By default, this is set to match the
27 * Layer.Text behavior, which is to use the default OpenLayers Icon.
32 * APIProperty: extractStyles
33 * set to true to extract styles from the TSV files, using information
34 * from the image or icon, iconSize and iconOffset fields. This will result
35 * in features with a symbolizer (style) property set, using the
36 * default symbolizer specified in <defaultStyle>. Set to false if you
37 * wish to use a styleMap or OpenLayers.Style options to style your
43 * Constructor: OpenLayers.Format.Text
44 * Create a new parser for TSV Text.
47 * options - {Object} An optional object whose properties will be set on
50 initialize: function(options) {
51 options = options || {};
53 if(options.extractStyles !== false) {
54 options.defaultStyle = {
55 'externalGraphic': OpenLayers.Util.getImagesLocation() +
59 'graphicXOffset': -10.5,
60 'graphicYOffset': -12.5
64 OpenLayers.Format.prototype.initialize.apply(this, [options]);
69 * Return a list of features from a Tab Seperated Values text string.
75 * An Array of <OpenLayers.Feature.Vector>s
77 read: function(text) {
78 var lines = text.split('\n');
81 // length - 1 to allow for trailing new line
82 for (var lcv = 0; lcv < (lines.length - 1); lcv++) {
83 var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'');
85 if (currLine.charAt(0) != '#') { /* not a comment */
88 //First line is columns
89 columns = currLine.split('\t');
91 var vals = currLine.split('\t');
92 var geometry = new OpenLayers.Geometry.Point(0,0);
94 var style = this.defaultStyle ?
95 OpenLayers.Util.applyDefaults({}, this.defaultStyle) :
97 var icon, iconSize, iconOffset, overflow;
99 for (var valIndex = 0; valIndex < vals.length; valIndex++) {
100 if (vals[valIndex]) {
101 if (columns[valIndex] == 'point') {
102 var coords = vals[valIndex].split(',');
103 geometry.y = parseFloat(coords[0]);
104 geometry.x = parseFloat(coords[1]);
106 } else if (columns[valIndex] == 'lat') {
107 geometry.y = parseFloat(vals[valIndex]);
109 } else if (columns[valIndex] == 'lon') {
110 geometry.x = parseFloat(vals[valIndex]);
112 } else if (columns[valIndex] == 'title')
113 attributes['title'] = vals[valIndex];
114 else if (columns[valIndex] == 'image' ||
115 columns[valIndex] == 'icon' && style) {
116 style['externalGraphic'] = vals[valIndex];
117 } else if (columns[valIndex] == 'iconSize' && style) {
118 var size = vals[valIndex].split(',');
119 style['graphicWidth'] = parseFloat(size[0]);
120 style['graphicHeight'] = parseFloat(size[1]);
121 } else if (columns[valIndex] == 'iconOffset' && style) {
122 var offset = vals[valIndex].split(',');
123 style['graphicXOffset'] = parseFloat(offset[0]);
124 style['graphicYOffset'] = parseFloat(offset[1]);
125 } else if (columns[valIndex] == 'description') {
126 attributes['description'] = vals[valIndex];
127 } else if (columns[valIndex] == 'overflow') {
128 attributes['overflow'] = vals[valIndex];
130 // For StyleMap filtering, allow additional
131 // columns to be stored as attributes.
132 attributes[columns[valIndex]] = vals[valIndex];
137 if (this.internalProjection && this.externalProjection) {
138 geometry.transform(this.externalProjection,
139 this.internalProjection);
141 var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
142 features.push(feature);
150 CLASS_NAME: "OpenLayers.Format.Text"