[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Feature/Vector.js |
---|
| 8 | * @requires OpenLayers/Geometry/Point.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Format.Text |
---|
| 13 | * Read Text format. Create a new instance with the <OpenLayers.Format.Text> |
---|
| 14 | * constructor. This reads text which is formatted like CSV text, using |
---|
| 15 | * tabs as the seperator by default. It provides parsing of data originally |
---|
| 16 | * used in the MapViewerService, described on the wiki. This Format is used |
---|
| 17 | * by the <OpenLayers.Layer.Text> class. |
---|
| 18 | * |
---|
| 19 | * Inherits from: |
---|
| 20 | * - <OpenLayers.Format> |
---|
| 21 | */ |
---|
| 22 | OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * APIProperty: defaultStyle |
---|
| 26 | * defaultStyle allows one to control the default styling of the features. |
---|
| 27 | * It should be a symbolizer hash. By default, this is set to match the |
---|
| 28 | * Layer.Text behavior, which is to use the default OpenLayers Icon. |
---|
| 29 | */ |
---|
| 30 | defaultStyle: null, |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * APIProperty: extractStyles |
---|
| 34 | * set to true to extract styles from the TSV files, using information |
---|
| 35 | * from the image or icon, iconSize and iconOffset fields. This will result |
---|
| 36 | * in features with a symbolizer (style) property set, using the |
---|
| 37 | * default symbolizer specified in <defaultStyle>. Set to false if you |
---|
| 38 | * wish to use a styleMap or OpenLayers.Style options to style your |
---|
| 39 | * layer instead. |
---|
| 40 | */ |
---|
| 41 | extractStyles: true, |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Constructor: OpenLayers.Format.Text |
---|
| 45 | * Create a new parser for TSV Text. |
---|
| 46 | * |
---|
| 47 | * Parameters: |
---|
| 48 | * options - {Object} An optional object whose properties will be set on |
---|
| 49 | * this instance. |
---|
| 50 | */ |
---|
| 51 | initialize: function(options) { |
---|
| 52 | options = options || {}; |
---|
| 53 | |
---|
| 54 | if(options.extractStyles !== false) { |
---|
| 55 | options.defaultStyle = { |
---|
| 56 | 'externalGraphic': OpenLayers.Util.getImagesLocation() + |
---|
| 57 | "marker.png", |
---|
| 58 | 'graphicWidth': 21, |
---|
| 59 | 'graphicHeight': 25, |
---|
| 60 | 'graphicXOffset': -10.5, |
---|
| 61 | 'graphicYOffset': -12.5 |
---|
| 62 | }; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | OpenLayers.Format.prototype.initialize.apply(this, [options]); |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * APIMethod: read |
---|
| 70 | * Return a list of features from a Tab Seperated Values text string. |
---|
| 71 | * |
---|
| 72 | * Parameters: |
---|
| 73 | * data - {String} |
---|
| 74 | * |
---|
| 75 | * Returns: |
---|
| 76 | * An Array of <OpenLayers.Feature.Vector>s |
---|
| 77 | */ |
---|
| 78 | read: function(text) { |
---|
| 79 | var lines = text.split('\n'); |
---|
| 80 | var columns; |
---|
| 81 | var features = []; |
---|
| 82 | // length - 1 to allow for trailing new line |
---|
| 83 | for (var lcv = 0; lcv < (lines.length - 1); lcv++) { |
---|
| 84 | var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,''); |
---|
| 85 | |
---|
| 86 | if (currLine.charAt(0) != '#') { /* not a comment */ |
---|
| 87 | |
---|
| 88 | if (!columns) { |
---|
| 89 | //First line is columns |
---|
| 90 | columns = currLine.split('\t'); |
---|
| 91 | } else { |
---|
| 92 | var vals = currLine.split('\t'); |
---|
| 93 | var geometry = new OpenLayers.Geometry.Point(0,0); |
---|
| 94 | var attributes = {}; |
---|
| 95 | var style = this.defaultStyle ? |
---|
| 96 | OpenLayers.Util.applyDefaults({}, this.defaultStyle) : |
---|
| 97 | null; |
---|
| 98 | var icon, iconSize, iconOffset, overflow; |
---|
| 99 | var set = false; |
---|
| 100 | for (var valIndex = 0; valIndex < vals.length; valIndex++) { |
---|
| 101 | if (vals[valIndex]) { |
---|
| 102 | if (columns[valIndex] == 'point') { |
---|
| 103 | var coords = vals[valIndex].split(','); |
---|
| 104 | geometry.y = parseFloat(coords[0]); |
---|
| 105 | geometry.x = parseFloat(coords[1]); |
---|
| 106 | set = true; |
---|
| 107 | } else if (columns[valIndex] == 'lat') { |
---|
| 108 | geometry.y = parseFloat(vals[valIndex]); |
---|
| 109 | set = true; |
---|
| 110 | } else if (columns[valIndex] == 'lon') { |
---|
| 111 | geometry.x = parseFloat(vals[valIndex]); |
---|
| 112 | set = true; |
---|
| 113 | } else if (columns[valIndex] == 'title') |
---|
| 114 | attributes['title'] = vals[valIndex]; |
---|
| 115 | else if (columns[valIndex] == 'image' || |
---|
| 116 | columns[valIndex] == 'icon' && style) { |
---|
| 117 | style['externalGraphic'] = vals[valIndex]; |
---|
| 118 | } else if (columns[valIndex] == 'iconSize' && style) { |
---|
| 119 | var size = vals[valIndex].split(','); |
---|
| 120 | style['graphicWidth'] = parseFloat(size[0]); |
---|
| 121 | style['graphicHeight'] = parseFloat(size[1]); |
---|
| 122 | } else if (columns[valIndex] == 'iconOffset' && style) { |
---|
| 123 | var offset = vals[valIndex].split(','); |
---|
| 124 | style['graphicXOffset'] = parseFloat(offset[0]); |
---|
| 125 | style['graphicYOffset'] = parseFloat(offset[1]); |
---|
| 126 | } else if (columns[valIndex] == 'description') { |
---|
| 127 | attributes['description'] = vals[valIndex]; |
---|
| 128 | } else if (columns[valIndex] == 'overflow') { |
---|
| 129 | attributes['overflow'] = vals[valIndex]; |
---|
| 130 | } else { |
---|
| 131 | // For StyleMap filtering, allow additional |
---|
| 132 | // columns to be stored as attributes. |
---|
| 133 | attributes[columns[valIndex]] = vals[valIndex]; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | if (set) { |
---|
| 138 | if (this.internalProjection && this.externalProjection) { |
---|
| 139 | geometry.transform(this.externalProjection, |
---|
| 140 | this.internalProjection); |
---|
| 141 | } |
---|
| 142 | var feature = new OpenLayers.Feature.Vector(geometry, attributes, style); |
---|
| 143 | features.push(feature); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | return features; |
---|
| 149 | }, |
---|
| 150 | |
---|
| 151 | CLASS_NAME: "OpenLayers.Format.Text" |
---|
| 152 | }); |
---|