OpenLayers OpenLayers

Changeset 5795

Show
Ignore:
Timestamp:
01/17/08 02:20:49 (1 year ago)
Author:
rdewit
Message:

Some more cleaning up.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/rdewit/kml/lib/OpenLayers/Format/KML.js

    r5794 r5795  
    150150        } 
    151151 
     152 
     153        // Fetch external links <NetworkLink> and <Link> 
     154        this.fetchLinks(data); 
     155 
     156        // Parse internal Style information 
     157        this.parseStyles(data); 
     158 
     159        // Parse placemarks (features) 
     160        this.parseFeatures(data); 
     161 
     162        // Reverse drawing order of features (this seems to be standard in KML) 
     163        if (!recursion && this.reverseFeatures) { 
     164            this.features.reverse(); 
     165        } 
     166 
     167        return this.features; 
     168    }, 
     169 
     170    fetchLinks: function(data) { 
    152171         
    153172        var urlNodes = this.getElementsByTagNameNS(data, 
    154173                                                       this.internalns, 
    155                                                        "Url"); 
     174                                                       "NetworkLink"); 
    156175        var numurls = urlNodes.length; 
    157176        var urls = new Array(numurls); 
     
    160179                                   this.internalns,  
    161180                                   "href")[0]; 
    162             //console.info(url); 
    163181            if(hrefNode) { 
    164182                var href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
     
    168186                } 
    169187 
    170                 //console.info(href); 
    171188                if (OpenLayers.ProxyHost  
    172189                        && OpenLayers.String.startsWith(href, "http")) { 
     
    191208                                   this.internalns,  
    192209                                   "href")[0]; 
    193             //console.info(link); 
    194210            if(hrefNode) { 
    195211                var href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
     
    211227            }  
    212228        } 
    213  
    214         // 
    215         // Parse internal Style information 
    216         this.parseStyles(data); 
    217  
    218  
    219         // Loop through all Placemark nodes and parse them. 
    220         // This will create a list of features 
    221         var featureNodes = this.getElementsByTagNameNS(data, 
    222                                                        this.internalns, 
    223                                                        "Placemark"); 
    224         var numFeatures = featureNodes.length; 
    225         var features = new Array(numFeatures); 
    226         //console.info("read, numfeatures: " + numFeatures); 
    227  
    228         for(var i=0; i<numFeatures; i++) { 
    229             //var feature = this.parseFeature(featureNodes[i]); 
    230             var featureNode = featureNodes[i]; 
    231             var feature = OpenLayers.Format.GML.prototype.parseFeature.apply( 
    232                               this,[featureNode]) ; 
    233  
    234              //console.info(feature); 
    235             if(feature) { 
    236                 // give feature default style 
    237                 feature.style = OpenLayers.Util.extend({},  
    238                                     OpenLayers.Feature.Vector.style["default"]); 
    239                   
    240                 if (feature.attributes.styleUrl) { 
    241                     var newStyle = this.getStyle(feature.attributes.styleUrl); 
    242  
    243                     if (newStyle) { 
    244                         feature.style = OpenLayers.Util.extend(feature.style,  
    245                                             newStyle); 
    246                     } 
    247                     //console.info(feature.style); 
    248                 } 
    249  
    250                 // Make sure that <Style> nodes within a placemark are  
    251                 // processed as well: add a styleUrl attribute to the feature. 
    252                 var styleNode = this.getElementsByTagNameNS(featureNode, 
    253                                                        this.internalns, 
    254                                                        "Style")[0]; 
    255                 if (styleNode) { 
    256                     var style = this.parseStyle(styleNode); 
    257                     //console.info(style.name, style); 
    258                     if (style) { 
    259                         feature.style = OpenLayers.Util.extend(feature.style,  
    260                                             style); 
    261                     } 
    262                 } 
    263  
    264                 // add feature to list of features 
    265                 features[i] = feature; 
    266             } else { 
    267                 throw "Bad Placemark: " + i; 
    268             } 
    269         } 
    270  
    271  
    272         // add new features to existing feature list 
    273         this.features = this.features.concat(features); 
    274  
    275  
    276         // Reverse drawing order of features (this seems to be standard in KML) 
    277         if (!recursion && this.reverseFeatures) { 
    278             this.features.reverse(); 
    279         } 
    280  
    281  
    282         //this.applyStyles(); 
    283  
    284         //console.info(this.styles); 
    285         //console.info("*** RETURN FEATURES ***"); 
    286         //console.debug(this.styles); 
    287         return this.features; 
    288  
    289         //this.timer = setTimeout(this.read(), 200); 
    290         //return this.read(); 
    291229    }, 
    292230 
     
    327265     
    328266 
    329  
    330267    parseStyles: function(data, baseUrl) { 
    331         if (!data) { 
    332             return null; 
    333         } 
    334  
    335         if (!baseUrl) { 
    336             baseUrl = ""; 
    337         } 
    338  
    339         // Parse Styles 
     268        // Parse Style nodes first 
    340269        var styleNodes = this.getElementsByTagNameNS(data, 
    341270                                                       this.internalns, 
     
    344273        var numNodes = styleNodes.length; 
    345274        var styles = {}; 
    346         //console.info("numStyles: " + numStyles); 
    347275        for(var i=0; i<numNodes; i++) { 
    348276            var style = this.parseStyle(styleNodes[i]); 
     
    355283        } 
    356284 
    357         // Parse StyleMaps 
     285        // Parse StyleMaps afterwards 
    358286        // Only the default or "normal" part of the StyleMap is processed now 
    359287        // To do the select or "highlight" bit, we'd need to change lots more 
     
    371299            var id = node.getAttribute("id"); 
    372300            for (var j=0; j<pairs.length; j++) { 
    373                 pair = pairs[j]; 
     301                var pair = pairs[j]; 
    374302         
    375303                // Use the shortcut in the SLD format to quickly retrieve the  
    376304                // value of a node. Maybe it's good to have a method in Util  
    377305                // to do this 
    378                 key = OpenLayers.Format.SLD.prototype.parseProperty(pair, 
     306                var key = OpenLayers.Format.SLD.prototype.parseProperty(pair, 
    379307                          this.internalns, "key"); 
    380                 styleUrl = OpenLayers.Format.SLD.prototype.parseProperty(pair,  
     308                var styleUrl = OpenLayers.Format.SLD.prototype.parseProperty(pair,  
    381309                          this.internalns, "styleUrl"); 
    382310 
     
    393321        } 
    394322 
    395         //console.info("before", this.styles); 
    396323        OpenLayers.Util.extend(this.styles, styles); 
    397324 
    398         //console.info("after", this.styles); 
    399325    }, 
    400326 
     
    415341 
    416342            // only deal with first geometry of this type 
    417             //console.info(styleTypeNode); 
    418343            switch (type.toLowerCase()) { 
    419344                case "linestyle": 
    420                     colorNode = this.getElementsByTagNameNS(styleTypeNode,  
     345                    var colorNode = this.getElementsByTagNameNS(styleTypeNode,  
    421346                                               this.internalns,  
    422347                                               "color")[0]; 
     
    438363                    } 
    439364                     
    440                     widthNode = this.getElementsByTagNameNS(styleTypeNode,  
     365                    var widthNode = this.getElementsByTagNameNS(styleTypeNode,  
    441366                                               this.internalns, 
    442367                                               "width")[0]; 
     
    447372 
    448373                case "polystyle": 
    449                     colorNode = this.getElementsByTagNameNS(styleTypeNode,  
     374                    var colorNode = this.getElementsByTagNameNS(styleTypeNode,  
    450375                                               this.internalns,  
    451376                                               "color")[0]; 
     
    456381                                          this.regExes.kmlColor); 
    457382 
    458                         console.info(matches); 
    459383                        // transparency 
    460384                        var alpha = matches[1]; 
     
    470394                    break; 
    471395                case "iconstyle": 
    472                     iconNode = this.getElementsByTagNameNS(styleTypeNode,  
     396                    var iconNode = this.getElementsByTagNameNS(styleTypeNode,  
    473397                                               this.internalns,  
    474398                                               "Icon")[0]; 
     
    479403 
    480404                    if (iconNode) { 
    481                         hrefNode = this.getElementsByTagNameNS(iconNode,  
     405                        var hrefNode = this.getElementsByTagNameNS(iconNode,  
    482406                                               this.internalns,  
    483407                                               "href")[0]; 
     
    493417                            // list and request the appropriate icon from the  
    494418                            // google maps website 
    495                             matches = href.match(this.regExes.kmlIconPalette); 
     419                            var matches = href.match(this.regExes.kmlIconPalette); 
    496420                            if (matches)  { 
    497421                                var palette = matches[1]; 
     
    512436 
    513437                                var pos = posY * 8 + posX; 
    514                                 href = "http://maps.google.com/mapfiles/kml/pal"  
     438                                var href = "http://maps.google.com/mapfiles/kml/pal"  
    515439                                     + palette + "/icon" + pos + file_extension; 
    516                                 //console.info(x,y, pos, href); 
    517440                            } 
    518441 
     
    521444                                                   this.internalns,  
    522445                                                   "w")[0]; 
    523                             w = OpenLayers.Util.getXmlNodeValue(wNode); 
     446                            var w = OpenLayers.Util.getXmlNodeValue(wNode); 
    524447 
    525448                            var hNode = this.getElementsByTagNameNS(iconNode,  
    526449                                                   this.internalns,  
    527450                                                   "h")[0]; 
    528                             h = OpenLayers.Util.getXmlNodeValue(hNode); 
     451                            var h = OpenLayers.Util.getXmlNodeValue(hNode); 
    529452 
    530453                            if (w) { 
     
    544467 
    545468                    // hotSpots define the offset for an Icon 
    546                     hotSpotNode = this.getElementsByTagNameNS(styleTypeNode,  
     469                    var hotSpotNode = this.getElementsByTagNameNS(styleTypeNode,  
    547470                                               this.internalns,  
    548471                                               "hotSpot")[0]; 
     
    576499                                                          * parseFloat(y); 
    577500                        } 
    578                         //console.info(x, xUnits, y, yUnits, style["graphicXOffset"],  
    579                         //  style["graphicYOffset"]); 
    580501                    } 
    581502                    break; 
    582503                case "balloonstyle": 
    583                     balloonStyle = OpenLayers.Util.getXmlNodeValue(styleTypeNode); 
     504                    var balloonStyle = OpenLayers.Util.getXmlNodeValue( 
     505                                            styleTypeNode); 
    584506                    if (balloonStyle) { 
    585507                        style["balloonStyle"] = balloonStyle.replace( 
    586                                                     straightBracket, "${$1}"); 
    587                         //console.info(balloonStyle); 
     508                                       this.regExes.straightBracket, "${$1}"); 
    588509                    } 
    589510                    break; 
     
    603524        return style; 
    604525    }, 
     526 
     527    parseFeatures: function(data) { 
     528        // Loop through all Placemark nodes and parse them. 
     529        // This will create a list of features 
     530        var featureNodes = this.getElementsByTagNameNS(data, 
     531                                                       this.internalns, 
     532                                                       "Placemark"); 
     533        var numFeatures = featureNodes.length; 
     534        var features = new Array(numFeatures); 
     535 
     536        for(var i=0; i<numFeatures; i++) { 
     537            //var feature = this.parseFeature(featureNodes[i]); 
     538            var featureNode = featureNodes[i]; 
     539            var feature = OpenLayers.Format.GML.prototype.parseFeature.apply( 
     540                              this,[featureNode]) ; 
     541 
     542            if(feature) { 
     543                // give feature default style 
     544                feature.style = OpenLayers.Util.extend({},  
     545                                    OpenLayers.Feature.Vector.style["default"]); 
     546                  
     547                if (feature.attributes.styleUrl) { 
     548                    var newStyle = this.getStyle(feature.attributes.styleUrl); 
     549 
     550                    if (newStyle) { 
     551                        feature.style = OpenLayers.Util.extend(feature.style,  
     552                                            newStyle); 
     553                    } 
     554                } 
     555 
     556                // Make sure that <Style> nodes within a placemark are  
     557                // processed as well: add a styleUrl attribute to the feature. 
     558                var styleNode = this.getElementsByTagNameNS(featureNode, 
     559                                                       this.internalns, 
     560                                                       "Style")[0]; 
     561                if (styleNode) { 
     562                    var style = this.parseStyle(styleNode); 
     563                    if (style) { 
     564                        feature.style = OpenLayers.Util.extend(feature.style,  
     565                                            style); 
     566                    } 
     567                } 
     568 
     569                // add feature to list of features 
     570                features[i] = feature; 
     571            } else { 
     572                throw "Bad Placemark: " + i; 
     573            } 
     574        } 
     575 
     576 
     577        // add new features to existing feature list 
     578        this.features = this.features.concat(features); 
     579    }, 
     580 
     581 
    605582         
    606583    /** 
     
    622599            if(child.nodeType == 1) { 
    623600                grandchildren = child.childNodes; 
    624             //console.info(child, child.nodeType); 
    625             //console.info(grandchildren.length); 
    626601                if(grandchildren.length == 1 || grandchildren.length == 3) { 
    627602                    var grandchild;