]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/Text.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / Text.js
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. */
4
5 /**
6  * @requires OpenLayers/Feature/Vector.js
7  * @requires OpenLayers/Geometry/Point.js
8  */
9
10 /**
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.
17  *
18  * Inherits from:
19  *  - <OpenLayers.Format>
20  */
21 OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
22     
23     /**
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.
28      */
29     defaultStyle: null,
30      
31     /**
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
38      * layer instead.
39      */
40     extractStyles: true,
41
42     /**
43      * Constructor: OpenLayers.Format.Text
44      * Create a new parser for TSV Text.
45      *
46      * Parameters:
47      * options - {Object} An optional object whose properties will be set on
48      *     this instance.
49      */
50     initialize: function(options) {
51         options = options || {};
52
53         if(options.extractStyles !== false) {
54             options.defaultStyle = {
55                 'externalGraphic': OpenLayers.Util.getImagesLocation() +
56                                                                 "marker.png",
57                 'graphicWidth': 21,
58                 'graphicHeight': 25,
59                 'graphicXOffset': -10.5,
60                 'graphicYOffset': -12.5
61             };
62         }
63         
64         OpenLayers.Format.prototype.initialize.apply(this, [options]);
65     }, 
66
67     /**
68      * APIMethod: read
69      * Return a list of features from a Tab Seperated Values text string.
70      * 
71      * Parameters:
72      * data - {String} 
73      *
74      * Returns:
75      * An Array of <OpenLayers.Feature.Vector>s
76      */
77     read: function(text) {
78         var lines = text.split('\n');
79         var columns;
80         var features = [];
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*$/,'');
84         
85             if (currLine.charAt(0) != '#') { /* not a comment */
86             
87                 if (!columns) {
88                     //First line is columns
89                     columns = currLine.split('\t');
90                 } else {
91                     var vals = currLine.split('\t');
92                     var geometry = new OpenLayers.Geometry.Point(0,0);
93                     var attributes = {};
94                     var style = this.defaultStyle ? 
95                         OpenLayers.Util.applyDefaults({}, this.defaultStyle) :
96                         null;  
97                     var icon, iconSize, iconOffset, overflow;
98                     var set = false;
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]);
105                                 set = true;
106                             } else if (columns[valIndex] == 'lat') {
107                                 geometry.y = parseFloat(vals[valIndex]);
108                                 set = true;
109                             } else if (columns[valIndex] == 'lon') {
110                                 geometry.x = parseFloat(vals[valIndex]);
111                                 set = true;
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];
129                             } else {
130                                 // For StyleMap filtering, allow additional
131                                 // columns to be stored as attributes.
132                                 attributes[columns[valIndex]] = vals[valIndex];
133                             }    
134                         }
135                     }
136                     if (set) {
137                       if (this.internalProjection && this.externalProjection) {
138                           geometry.transform(this.externalProjection, 
139                                              this.internalProjection); 
140                       }
141                       var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
142                       features.push(feature);
143                     }
144                 }
145             }
146         }
147         return features;
148     },   
149
150     CLASS_NAME: "OpenLayers.Format.Text" 
151 });