OpenLayers OpenLayers

Ticket #1033: text_format.patch

File text_format.patch, 12.4 kB (added by crschmidt, 1 year ago)

Create Format.Text, and have Layer.Text use it.

  • lib/OpenLayers/Format/Text.js

    old new  
     1/* Copyright (c) 2006-2007 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 * Class: OpenLayers.Format.Text 
     10 * Read Text format. Create a new instance with the <OpenLayers.Format.Text> 
     11 *     constructor. This reads text which is formatted like CSV text, using 
     12 *     tabs as the seperator by default. It provides parsing of data originally 
     13 *     used in the MapViewerService, described on the wiki. This Format is used 
     14 *     by the <OpenLayers.Layer.Text> class. 
     15 * 
     16 * Inherits from: 
     17 *  - <OpenLayers.Format> 
     18 */ 
     19OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { 
     20     
     21    /** 
     22     * Constructor: OpenLayers.Format.Text 
     23     * Create a new parser for TSV Text. 
     24     * 
     25     * Parameters: 
     26     * options - {Object} An optional object whose properties will be set on 
     27     *     this instance. 
     28     */ 
     29    initialize: function(options) { 
     30        OpenLayers.Format.prototype.initialize.apply(this, [options]); 
     31    },  
     32 
     33    /** 
     34     * APIMethod: read 
     35     * Return a list of features from a Tab Seperated Values text string. 
     36     *  
     37     * Parameters: 
     38     * data - {String}  
     39     * 
     40     * Returns: 
     41     * An Array of <OpenLayers.Feature.Vector>s 
     42     */ 
     43    read: function(text) { 
     44        var lines = text.split('\n'); 
     45        var columns; 
     46        var features = []; 
     47        // length - 1 to allow for trailing new line 
     48        for (var lcv = 0; lcv < (lines.length - 1); lcv++) { 
     49            var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,''); 
     50         
     51            if (currLine.charAt(0) != '#') { /* not a comment */ 
     52             
     53                if (!columns) { 
     54                    //First line is columns 
     55                    columns = currLine.split('\t'); 
     56                } else { 
     57                    var vals = currLine.split('\t'); 
     58                    var geometry = new OpenLayers.Geometry.Point(0,0); 
     59                    var attributes = {}; 
     60                    var style = {}; 
     61                    var icon, iconSize, iconOffset, overflow; 
     62                    var set = false; 
     63                    for (var valIndex = 0; valIndex < vals.length; valIndex++) { 
     64                        if (vals[valIndex]) { 
     65                            if (columns[valIndex] == 'point') { 
     66                                var coords = vals[valIndex].split(','); 
     67                                geometry.y = parseFloat(coords[0]); 
     68                                geometry.x = parseFloat(coords[1]); 
     69                                set = true; 
     70                            } else if (columns[valIndex] == 'lat') { 
     71                                geometry.y = parseFloat(vals[valIndex]); 
     72                                set = true; 
     73                            } else if (columns[valIndex] == 'lon') { 
     74                                geometry.x = parseFloat(vals[valIndex]); 
     75                                set = true; 
     76                            } else if (columns[valIndex] == 'title') 
     77                                attributes['title'] = vals[valIndex]; 
     78                            else if (columns[valIndex] == 'image' || 
     79                                     columns[valIndex] == 'icon') 
     80                                style['externalGraphic'] = vals[valIndex]; 
     81                            else if (columns[valIndex] == 'iconSize') { 
     82                                var size = vals[valIndex].split(','); 
     83                                style['graphicWidth'] = parseFloat(size[0]); 
     84                                style['graphicHeight'] = parseFloat(size[1]); 
     85                            } else if (columns[valIndex] == 'iconOffset') { 
     86                                var offset = vals[valIndex].split(','); 
     87                                style['graphicXOffset'] = parseFloat(offset[0]); 
     88                                style['graphicYOffset'] = parseFloat(offset[1]); 
     89                            } else if (columns[valIndex] == 'description') { 
     90                                attributes['description'] = vals[valIndex]; 
     91                            } else if (columns[valIndex] == 'overflow') { 
     92                                attributes['overflow'] = vals[valIndex]; 
     93                            }     
     94                        } 
     95                    } 
     96                    if (set) { 
     97                      var feature = new OpenLayers.Feature.Vector(geometry, attributes, style); 
     98                      features.push(feature); 
     99                    } 
     100                } 
     101            } 
     102        } 
     103        return features; 
     104    },    
     105 
     106    CLASS_NAME: "OpenLayers.Format.Text"  
     107});     
  • lib/OpenLayers/Layer/Text.js

    old new  
    7676     */ 
    7777    parseData: function(ajaxRequest) { 
    7878        var text = ajaxRequest.responseText; 
    79         var lines = text.split('\n'); 
    80         var columns
    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 */ 
     79        var parser = new OpenLayers.Format.Text(); 
     80        features = parser.read(text)
     81        for (var i = 0; i < features.length; i++) { 
     82            var data = {}; 
     83            var feature = features[i]
     84            var location; 
     85            var iconSize, iconOffset; 
    8686             
    87                 if (!columns) { 
    88                     //First line is columns 
    89                     columns = currLine.split('\t'); 
    90                 } else { 
    91                     var vals = currLine.split('\t'); 
    92                     var location = new OpenLayers.LonLat(0,0); 
    93                     var title; var url; 
    94                     var icon, iconSize, iconOffset, overflow; 
    95                     var set = false; 
    96                     for (var valIndex = 0; valIndex < vals.length; valIndex++) { 
    97                         if (vals[valIndex]) { 
    98                             if (columns[valIndex] == 'point') { 
    99                                 var coords = vals[valIndex].split(','); 
    100                                 location.lat = parseFloat(coords[0]); 
    101                                 location.lon = parseFloat(coords[1]); 
    102                                 set = true; 
    103                             } else if (columns[valIndex] == 'lat') { 
    104                                 location.lat = parseFloat(vals[valIndex]); 
    105                                 set = true; 
    106                             } else if (columns[valIndex] == 'lon') { 
    107                                 location.lon = parseFloat(vals[valIndex]); 
    108                                 set = true; 
    109                             } else if (columns[valIndex] == 'title') 
    110                                 title = vals[valIndex]; 
    111                             else if (columns[valIndex] == 'image' || 
    112                                      columns[valIndex] == 'icon') 
    113                                 url = vals[valIndex]; 
    114                             else if (columns[valIndex] == 'iconSize') { 
    115                                 var size = vals[valIndex].split(','); 
    116                                 iconSize = new OpenLayers.Size(parseFloat(size[0]), 
    117                                                            parseFloat(size[1])); 
    118                             } else if (columns[valIndex] == 'iconOffset') { 
    119                                 var offset = vals[valIndex].split(','); 
    120                                 iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]), 
    121                                                            parseFloat(offset[1])); 
    122                             } else if (columns[valIndex] == 'title') { 
    123                                 title = vals[valIndex]; 
    124                             } else if (columns[valIndex] == 'description') { 
    125                                 description = vals[valIndex]; 
    126                             } else if (columns[valIndex] == 'overflow') { 
    127                                 overflow = vals[valIndex]; 
    128                             }     
    129                         } 
    130                     } 
    131                     if (set) { 
    132                       var data = {}; 
    133                       if (url != null) { 
    134                           data.icon = new OpenLayers.Icon(url,  
    135                                                           iconSize,  
    136                                                           iconOffset); 
    137                       } else { 
    138                           data.icon = OpenLayers.Marker.defaultIcon(); 
     87            location = new OpenLayers.LonLat(feature.geometry.x,  
     88                                             feature.geometry.y); 
     89             
     90            if (feature.style.graphicWidth  
     91                && feature.style.graphicHeight) { 
     92                iconSize = new OpenLayers.Size( 
     93                    feature.style.graphicWidth, 
     94                    feature.style.graphicHeight); 
     95            }         
     96             
     97            // FIXME: At the moment, we only use this if we have an  
     98            // externalGraphic, because icon has no setOffset API Method.   
     99            if (feature.style.graphicXOffset  
     100                && feature.style.graphicYOffset) { 
     101                iconOffset = new OpenLayers.Size( 
     102                    feature.style.graphicXOffset,  
     103                    feature.style.graphicYOffset); 
     104            } 
     105             
     106            if (feature.style.externalGraphic != null) { 
     107                data.icon = new OpenLayers.Icon(feature.style.externalGraphic,  
     108                                                iconSize,  
     109                                                iconOffset); 
     110            } else { 
     111                data.icon = OpenLayers.Marker.defaultIcon(); 
    139112 
    140                           //allows for the case where the image url is not  
    141                           // specified but the size is. use a default icon 
    142                           // but change the size 
    143                           if (iconSize != null) { 
    144                               data.icon.setSize(iconSize); 
    145                           } 
    146                          
    147                       } 
    148                       if ((title != null) && (description != null)) { 
    149                           data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>'; 
    150                       } 
    151                        
    152                       data['overflow'] = overflow || "auto";  
    153                        
    154                       var feature = new OpenLayers.Feature(this, location, data); 
    155                       this.features.push(feature); 
    156                       var marker = feature.createMarker(); 
    157                       if ((title != null) && (description != null)) { 
    158                         marker.events.register('click', feature, this.markerClick); 
    159                       } 
    160                       this.addMarker(marker); 
    161                     } 
     113                //allows for the case where the image url is not  
     114                // specified but the size is. use a default icon 
     115                // but change the size 
     116                if (iconSize != null) { 
     117                    data.icon.setSize(iconSize); 
    162118                } 
    163119            } 
     120             
     121            if ((feature.attributes.title != null)  
     122                && (feature.attributes.description != null)) { 
     123                data['popupContentHTML'] =  
     124                    '<h2>'+feature.attributes.title+'</h2>' +  
     125                    '<p>'+feature.attributes.description+'</p>'; 
     126            } 
     127             
     128            data['overflow'] = feature.attributes.overflow || "auto";  
     129             
     130            var markerFeature = new OpenLayers.Feature(this, location, data); 
     131            this.features.push(markerFeature); 
     132            var marker = markerFeature.createMarker(); 
     133            if ((feature.attributes.title != null)  
     134                && (feature.attributes.description != null)) { 
     135              marker.events.register('click', markerFeature, this.markerClick); 
     136            } 
     137            this.addMarker(marker); 
    164138        } 
    165139        this.events.triggerEvent("loadend"); 
    166140    },