OpenLayers OpenLayers

Ticket #1033: format.text.patch

File format.text.patch, 12.8 kB (added by crschmidt, 1 year ago)

new patch

  • 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  
    100100     */ 
    101101    parseData: function(ajaxRequest) { 
    102102        var text = ajaxRequest.responseText; 
    103         var lines = text.split('\n'); 
    104         var columns
    105         // length - 1 to allow for trailing new line 
    106         for (var lcv = 0; lcv < (lines.length - 1); lcv++) { 
    107             var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'')
    108          
    109             if (currLine.charAt(0) != '#') { /* not a comment */ 
     103        var parser = new OpenLayers.Format.Text(); 
     104        features = parser.read(text)
     105        for (var i = 0; i < features.length; i++) { 
     106            var data = {}; 
     107            var feature = features[i]
     108            var location; 
     109            var iconSize, iconOffset; 
    110110             
    111                 if (!columns) { 
    112                     //First line is columns 
    113                     columns = currLine.split('\t'); 
    114                 } else { 
    115                     var vals = currLine.split('\t'); 
    116                     var location = new OpenLayers.LonLat(0,0); 
    117                     var title, url, description; 
    118                     var icon, iconSize, iconOffset, overflow; 
    119                     var set = false; 
    120                     for (var valIndex = 0; valIndex < vals.length; valIndex++) { 
    121                         if (vals[valIndex]) { 
    122                             if (columns[valIndex] == 'point') { 
    123                                 var coords = vals[valIndex].split(','); 
    124                                 location.lat = parseFloat(coords[0]); 
    125                                 location.lon = parseFloat(coords[1]); 
    126                                 set = true; 
    127                             } else if (columns[valIndex] == 'lat') { 
    128                                 location.lat = parseFloat(vals[valIndex]); 
    129                                 set = true; 
    130                             } else if (columns[valIndex] == 'lon') { 
    131                                 location.lon = parseFloat(vals[valIndex]); 
    132                                 set = true; 
    133                             } else if (columns[valIndex] == 'title') { 
    134                                 title = vals[valIndex]; 
    135                             } else if (columns[valIndex] == 'image' || 
    136                                      columns[valIndex] == 'icon') { 
    137                                 url = vals[valIndex]; 
    138                             } else if (columns[valIndex] == 'iconSize') { 
    139                                 var size = vals[valIndex].split(','); 
    140                                 iconSize = new OpenLayers.Size(parseFloat(size[0]), 
    141                                                            parseFloat(size[1])); 
    142                             } else if (columns[valIndex] == 'iconOffset') { 
    143                                 var offset = vals[valIndex].split(','); 
    144                                 iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]), 
    145                                                            parseFloat(offset[1])); 
    146                             } else if (columns[valIndex] == 'title') { 
    147                                 title = vals[valIndex]; 
    148                             } else if (columns[valIndex] == 'description') { 
    149                                 description = vals[valIndex]; 
    150                             } else if (columns[valIndex] == 'overflow') { 
    151                                 overflow = vals[valIndex]; 
    152                             }     
    153                         } 
    154                     } 
    155                     if (set) { 
    156                       var data = {}; 
    157                       if (url != null) { 
    158                           data.icon = new OpenLayers.Icon(url,  
    159                                                           iconSize,  
    160                                                           iconOffset); 
    161                       } else { 
    162                           data.icon = OpenLayers.Marker.defaultIcon(); 
     111            location = new OpenLayers.LonLat(feature.geometry.x,  
     112                                             feature.geometry.y); 
     113             
     114            if (feature.style.graphicWidth  
     115                && feature.style.graphicHeight) { 
     116                iconSize = new OpenLayers.Size( 
     117                    feature.style.graphicWidth, 
     118                    feature.style.graphicHeight); 
     119            }         
     120             
     121            // FIXME: At the moment, we only use this if we have an  
     122            // externalGraphic, because icon has no setOffset API Method.   
     123            if (feature.style.graphicXOffset  
     124                && feature.style.graphicYOffset) { 
     125                iconOffset = new OpenLayers.Size( 
     126                    feature.style.graphicXOffset,  
     127                    feature.style.graphicYOffset); 
     128            } 
     129             
     130            if (feature.style.externalGraphic != null) { 
     131                data.icon = new OpenLayers.Icon(feature.style.externalGraphic,  
     132                                                iconSize,  
     133                                                iconOffset); 
     134            } else { 
     135                data.icon = OpenLayers.Marker.defaultIcon(); 
    163136 
    164                           //allows for the case where the image url is not  
    165                           // specified but the size is. use a default icon 
    166                           // but change the size 
    167                           if (iconSize != null) { 
    168                               data.icon.setSize(iconSize); 
    169                           } 
    170                          
    171                       } 
    172                       if ((title != null) && (description != null)) { 
    173                           data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>'; 
    174                       } 
    175                        
    176                       data['overflow'] = overflow || "auto";  
    177                        
    178                       var feature = new OpenLayers.Feature(this, location, data); 
    179                       this.features.push(feature); 
    180                       var marker = feature.createMarker(); 
    181                       if ((title != null) && (description != null)) { 
    182                         marker.events.register('click', feature, this.markerClick); 
    183                       } 
    184                       this.addMarker(marker); 
    185                     } 
     137                //allows for the case where the image url is not  
     138                // specified but the size is. use a default icon 
     139                // but change the size 
     140                if (iconSize != null) { 
     141                    data.icon.setSize(iconSize); 
    186142                } 
    187143            } 
     144             
     145            if ((feature.attributes.title != null)  
     146                && (feature.attributes.description != null)) { 
     147                data['popupContentHTML'] =  
     148                    '<h2>'+feature.attributes.title+'</h2>' +  
     149                    '<p>'+feature.attributes.description+'</p>'; 
     150            } 
     151             
     152            data['overflow'] = feature.attributes.overflow || "auto";  
     153             
     154            var markerFeature = new OpenLayers.Feature(this, location, data); 
     155            this.features.push(markerFeature); 
     156            var marker = markerFeature.createMarker(); 
     157            if ((feature.attributes.title != null)  
     158                && (feature.attributes.description != null)) { 
     159              marker.events.register('click', markerFeature, this.markerClick); 
     160            } 
     161            this.addMarker(marker); 
    188162        } 
    189163        this.events.triggerEvent("loadend"); 
    190164    }, 
  • lib/OpenLayers.js

    old new  
    171171            "OpenLayers/Format/GeoRSS.js", 
    172172            "OpenLayers/Format/WFS.js", 
    173173            "OpenLayers/Format/WKT.js", 
     174            "OpenLayers/Format/Text.js", 
    174175            "OpenLayers/Format/JSON.js", 
    175176            "OpenLayers/Format/GeoJSON.js", 
    176177            "OpenLayers/Layer/WFS.js",