OpenLayers OpenLayers

Changeset 5412

Show
Ignore:
Timestamp:
12/14/07 17:02:00 (1 year ago)
Author:
crschmidt
Message:

Create Format.Text, and have Layer.Text use it. (Closes #1033)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers.js

    r5411 r5412  
    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", 
  • trunk/openlayers/lib/OpenLayers/Layer/Text.js

    r5289 r5412  
    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");