OpenLayers OpenLayers

Changeset 5794

Show
Ignore:
Timestamp:
01/17/08 01:55:12 (11 months ago)
Author:
rdewit
Message:

Code cleanups

Files:

Legend:

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

    r5792 r5794  
    6666    /** 
    6767     * Property: gmlns 
    68      * {String} GML Namespace to use -- defaults to the namespace "*" 
     68     * {String} GML Namespace to use -- defaults to the namespace this.internalns 
    6969     *      
    7070     */ 
     
    8686 
    8787    /** 
    88      * Property: loadedKML 
     88     * Property: fetched 
    8989     * {Object} Storage of KML URLs that have been fetched before 
    9090     *     in order to prevent reloading them. 
    9191     */ 
    92     loadedKML: {}, 
     92    fetched: {}, 
    9393 
    9494    /** 
     
    101101     */ 
    102102    initialize: function(options) { 
     103        OpenLayers.Format.GML.prototype.initialize.apply(this, [options]); 
     104 
    103105        // compile regular expressions once instead of every time they are used 
    104106        this.regExes = { 
     
    106108            removeSpace: (/\s*/g), 
    107109            splitSpace: (/\s+/), 
    108             trimComma: (/\s*,\s*/g) 
     110            trimComma: (/\s*,\s*/g), 
     111            kmlColor: (/(\w{2})(\w{2})(\w{2})(\w{2})/), 
     112            kmlIconPalette: (/root:\/\/icons\/palette-(\d+)(\.\w+)/), 
     113            straightBracket: (/\$\[(.*?)\]/g) 
    109114        }; 
    110         OpenLayers.Format.GML.prototype.initialize.apply(this, [options]); 
    111115 
    112116    }, 
     
    122126     * {Array(<OpenLayers.Feature.Vector>)} List of features. 
    123127     */ 
    124     read: function(data) { 
    125          
     128    read: function(data, recursion) { 
    126129        if(typeof data == "string") { 
    127130            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
     
    133136        // Should not be in Format/KML.js. 
    134137        var serviceException = this.getElementsByTagNameNS(data,  
    135                                "*",  
     138                               this.internalns,  
    136139                               "ServiceException")[0]; 
    137140        if (serviceException) { 
     
    141144                message  += code + "\n"; 
    142145            } 
    143             message += OpenLayers.Util.getXmlNodeValue(serviceException).replace(this.regExes.trimSpace, ""); 
     146            message += OpenLayers.Util.getXmlNodeValue( 
     147                       serviceException).replace(this.regExes.trimSpace, ""); 
    144148            alert(message); 
    145149            return false; 
     
    148152         
    149153        var urlNodes = this.getElementsByTagNameNS(data, 
    150                                                        '*'
     154                                                       this.internalns
    151155                                                       "Url"); 
    152156        var numurls = urlNodes.length; 
     
    154158        for(var i=0; i<numurls; i++) { 
    155159            var hrefNode = this.getElementsByTagNameNS(urlNodes[i],  
    156                                    "*",  
     160                                   this.internalns,  
    157161                                   "href")[0]; 
    158162            //console.info(url); 
     
    160164                var href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
    161165 
    162                 if (this.loadedKML[href]) {  
     166                if (this.fetched[href]) {  
    163167                    continue; 
    164168                } 
    165169 
    166170                //console.info(href); 
    167                 if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(href, "http")) { 
     171                if (OpenLayers.ProxyHost  
     172                        && OpenLayers.String.startsWith(href, "http")) { 
    168173                    href = OpenLayers.ProxyHost + escape(href); 
    169174                } 
    170                 var request = new OpenLayers.Ajax.Request(href, {method: 'get', asynchronous: false }); 
    171                 this.loadedKML[href] = true; // prevent reloading the same urls 
     175                var request = new OpenLayers.Ajax.Request(href,  
     176                              {method: 'get', asynchronous: false }); 
     177                this.fetched[href] = true; // prevent reloading the same urls 
    172178                if (request && request.transport) { 
    173                     this.read(request.transport.responseText); 
     179                    this.read(request.transport.responseText, true); 
    174180                } 
    175181            }  
     
    177183 
    178184        var linkNodes = this.getElementsByTagNameNS(data, 
    179                                                        '*'
     185                                                       this.internalns
    180186                                                       "Link"); 
    181187        var numlinks = linkNodes.length; 
     
    183189        for(var i=0; i<numlinks; i++) { 
    184190            var hrefNode = this.getElementsByTagNameNS(linkNodes[i],  
    185                                    "*",  
     191                                   this.internalns,  
    186192                                   "href")[0]; 
    187193            //console.info(link); 
     
    189195                var href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
    190196 
    191                 if (this.loadedKML[href]) {  
     197                if (this.fetched[href]) {  
    192198                    continue; 
    193199                } 
    194200 
    195                 if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(href, "http")) { 
     201                if (OpenLayers.ProxyHost 
     202                        && OpenLayers.String.startsWith(href, "http")) { 
    196203                    href = OpenLayers.ProxyHost + escape(href); 
    197204                } 
    198                 var request = new OpenLayers.Ajax.Request(href, {method: 'get', asynchronous: false }); 
    199                 this.loadedKML[href] = true; // prevent reloading the same urls 
     205                var request = new OpenLayers.Ajax.Request(href,  
     206                              {method: 'get', asynchronous: false }); 
     207                this.fetched[href] = true; // prevent reloading the same urls 
    200208                if (request && request.transport) { 
    201                     this.read(request.transport.responseText); 
     209                    this.read(request.transport.responseText, true); 
    202210                } 
    203211            }  
     
    206214        // 
    207215        // Parse internal Style information 
    208         this.readStyles(data); 
     216        this.parseStyles(data); 
    209217 
    210218 
     
    221229            //var feature = this.parseFeature(featureNodes[i]); 
    222230            var featureNode = featureNodes[i]; 
    223             var feature = OpenLayers.Format.GML.prototype.parseFeature.apply(this,[featureNode]) ; 
     231            var feature = OpenLayers.Format.GML.prototype.parseFeature.apply( 
     232                              this,[featureNode]) ; 
    224233 
    225234             //console.info(feature); 
    226235            if(feature) { 
    227236                // give feature default style 
    228                 feature.style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style["default"]); 
     237                feature.style = OpenLayers.Util.extend({},  
     238                                    OpenLayers.Feature.Vector.style["default"]); 
    229239                  
    230240                if (feature.attributes.styleUrl) { 
    231                     //console.info(styleUrl, feature.style, this.getStyle(styleUrl)); 
    232241                    var newStyle = this.getStyle(feature.attributes.styleUrl); 
    233242 
    234243                    if (newStyle) { 
    235                         feature.style = OpenLayers.Util.extend(feature.style, newStyle ); 
     244                        feature.style = OpenLayers.Util.extend(feature.style,  
     245                                            newStyle); 
    236246                    } 
    237247                    //console.info(feature.style); 
     
    241251                // processed as well: add a styleUrl attribute to the feature. 
    242252                var styleNode = this.getElementsByTagNameNS(featureNode, 
    243                                                        '*'
     253                                                       this.internalns
    244254                                                       "Style")[0]; 
    245255                if (styleNode) { 
     
    247257                    //console.info(style.name, style); 
    248258                    if (style) { 
    249                         feature.style = OpenLayers.Util.extend(feature.style, style ); 
     259                        feature.style = OpenLayers.Util.extend(feature.style,  
     260                                            style); 
    250261                    } 
    251262                } 
     
    264275 
    265276        // Reverse drawing order of features (this seems to be standard in KML) 
    266         if (this.reverseFeatures) { 
     277        if (!recursion && this.reverseFeatures) { 
    267278            this.features.reverse(); 
    268279        } 
     
    281292 
    282293    getStyle: function(styleUrl) { 
    283         // Fetch remote Style URLs  
    284         var baseUrl = OpenLayers.Util.removeTail(styleUrl); 
    285         //console.info(baseUrl, styleUrl); 
    286  
    287         if (!this.styles[styleUrl] && !OpenLayers.String.startsWith(styleUrl, "#") && !this.loadedKML[baseUrl] ) { 
    288  
     294          
     295        // Fetch remote Style URLs (if not fetched before)  
     296        if (!this.styles[styleUrl]  
     297                && !OpenLayers.String.startsWith(styleUrl, "#")  
     298                && !this.fetched[baseUrl] ) { 
     299 
     300            var baseUrl = OpenLayers.Util.removeTail(styleUrl); 
    289301            var href = baseUrl; 
     302 
    290303            if (OpenLayers.String.startsWith(styleUrl, "http")) { 
    291304                href = OpenLayers.ProxyHost + escape(baseUrl); 
    292305            } 
    293306                     
    294             var request = new OpenLayers.Ajax.Request(href, {method: 'get', asynchronous: false }); 
     307            var request = new OpenLayers.Ajax.Request(href,  
     308                              {method: 'get', asynchronous: false }); 
    295309            if (request && request.transport) { 
    296                 this.loadedKML[baseUrl] = true; // prevent loading more than once 
     310                this.fetched[baseUrl] = true; // prevent loading more than once 
    297311                var data = request.transport.responseText; 
    298312                if(typeof data == "string") { 
    299                     data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
     313                    data = OpenLayers.Format.XML.prototype.read.apply(this,  
     314                              [data]); 
    300315                } 
    301316                if (data) { 
    302                     this.readStyles(data, baseUrl); 
    303                 } 
    304             } 
    305  
    306         } 
    307  
    308         // some KML files don't have a # for internal styles. add one 
    309         //if (styleUrl.substring(0,4) != "http" && styleUrl.substring(0,1) != "#") { 
    310             //styleUrl = "#" + styleUrl; 
    311         //}  
    312  
     317                    this.parseStyles(data, baseUrl); 
     318                } 
     319            } 
     320 
     321        } 
     322 
     323        // return requested style 
    313324        var style = this.styles[styleUrl]; 
    314         //console.info(style); 
    315  
    316325        return style; 
    317326    }, 
    318327     
    319328 
    320     readStyles: function(data, baseUrl) { 
     329 
     330    parseStyles: function(data, baseUrl) { 
    321331        if (!data) { 
    322332            return null; 
    323333        } 
    324334 
    325         //this.counter++; 
    326         //console.info("readStyles", this.counter, baseUrl); 
    327  
    328         if (this.counter > 20 ) { return null} 
     335        if (!baseUrl) { 
     336            baseUrl = ""; 
     337        } 
    329338 
    330339        // Parse Styles 
    331340        var styleNodes = this.getElementsByTagNameNS(data, 
    332                                                        '*'
     341                                                       this.internalns
    333342                                                       "Style"); 
    334343 
     
    350359        // To do the select or "highlight" bit, we'd need to change lots more 
    351360        var styleMapNodes = this.getElementsByTagNameNS(data, 
    352                                                        '*'
     361                                                       this.internalns
    353362                                                       "StyleMap"); 
    354363 
     
    357366        for(var i=0; i<numNodes; i++) { 
    358367            var node = styleMapNodes[i]; 
    359             var pairs = this.getElementsByTagNameNS(node, "*", "Pair"); 
    360             //console.info(pairs); 
     368            var pairs = this.getElementsByTagNameNS(node, this.internalns,  
     369                            "Pair"); 
    361370 
    362371            var id = node.getAttribute("id"); 
     
    364373                pair = pairs[j]; 
    365374         
    366                 // Use the shortcut in the SLD format to quickly retrieve the value of a node 
    367                 // Maybe it's good to have a method in Util to do this 
    368                 key = OpenLayers.Format.SLD.prototype.parseProperty(pair, "*", "key"); 
    369                 styleUrl = OpenLayers.Format.SLD.prototype.parseProperty(pair, "*", "styleUrl"); 
     375                // Use the shortcut in the SLD format to quickly retrieve the  
     376                // value of a node. Maybe it's good to have a method in Util  
     377                // to do this 
     378                key = OpenLayers.Format.SLD.prototype.parseProperty(pair, 
     379                          this.internalns, "key"); 
     380                styleUrl = OpenLayers.Format.SLD.prototype.parseProperty(pair,  
     381                          this.internalns, "styleUrl"); 
    370382 
    371383                if (key == "normal") { 
    372                     styles[(baseUrl || "") + "#" + id] = styles[(baseUrl || "") + styleUrl]; 
     384                    styles[(baseUrl || "") + "#" + id] = styles[(baseUrl || "") 
     385                                                            + styleUrl]; 
    373386                } 
    374387 
     
    382395        //console.info("before", this.styles); 
    383396        OpenLayers.Util.extend(this.styles, styles); 
     397 
    384398        //console.info("after", this.styles); 
    385399    }, 
    386400 
    387401    parseStyle: function(node) { 
    388          
    389         var styleObject = {}; 
    390  
    391         /** 
    392          * Constant: OpenLayers.Feature.Vector.style 
    393          * OpenLayers features can have a number of style attributes. The 'default'  
    394          *     style will typically be used if no other style is specified. 
    395          * 
    396          * Default style properties: 
    397          * 
    398          *  - fillColor: "#ee9900", 
    399          *  - fillOpacity: 0.4,  
    400          *  - hoverFillColor: "white", 
    401          *  - hoverFillOpacity: 0.8, 
    402          *  - strokeColor: "#ee9900", 
    403          *  - strokeOpacity: 1, 
    404          *  - strokeWidth: 1, 
    405          *  - strokeLinecap: "round", 
    406          *  - hoverStrokeColor: "red", 
    407          *  - hoverStrokeOpacity: 1, 
    408          *  - hoverStrokeWidth: 0.2, 
    409          *  - pointRadius: 6, 
    410          *  - hoverPointRadius: 1, 
    411          *  - hoverPointUnit: "%", 
    412          *  - pointerEvents: "visiblePainted" 
    413          * 
    414          * Other style properties that have no default values: 
    415          * 
    416          *  - externalGraphic, 
    417          *  - graphicWidth, 
    418          *  - graphicHeight, 
    419          *  - graphicOpacity 
    420          *  - graphicXOffset 
    421          *  - graphicYOffset 
    422          */  
    423  
    424402        var style = {}; 
    425403         
     
    432410            styleTypeNode = this.getElementsByTagNameNS(node,  
    433411                                                   this.internalns, type)[0]; 
    434             if(styleTypeNode) { 
    435                 // only deal with first geometry of this type 
    436                 //console.info(styleTypeNode); 
    437                 switch (type.toLowerCase()) { 
    438                     case "linestyle": 
    439                         colorNode = this.getElementsByTagNameNS(styleTypeNode,  
    440                                                    "*",  
    441                                                    "color")[0]; 
    442                         if (colorNode) { 
    443                             var alpha = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(0, 2); 
    444                             style["strokeOpacity"] = parseInt(alpha, 16) / 255; 
    445  
    446                             var b = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(2, 4); 
    447                             var g = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(4, 6); 
    448                             var r = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(6, 8); 
    449                             style["strokeColor"] = "#" + r + g + b; 
     412            if(!styleTypeNode) {  
     413                continue; 
     414            } 
     415 
     416            // only deal with first geometry of this type 
     417            //console.info(styleTypeNode); 
     418            switch (type.toLowerCase()) { 
     419                case "linestyle": 
     420                    colorNode = this.getElementsByTagNameNS(styleTypeNode,  
     421                                               this.internalns,  
     422                                               "color")[0]; 
     423                    if (colorNode) { 
     424                        var colorNodeValue = OpenLayers.Util.getXmlNodeValue( 
     425                                                  colorNode); 
     426                        var matches = colorNodeValue.match( 
     427                                          this.regExes.kmlColor); 
     428 
     429                        // transparency 
     430                        var alpha = matches[1]; 
     431                        style["strokeOpacity"] = parseInt(alpha, 16) / 255; 
     432 
     433                        // rgb colors (google uses bgr) 
     434                        var b = matches[2];  
     435                        var g = matches[3];  
     436                        var r = matches[4];  
     437                        style["strokeColor"] = "#" + r + g + b; 
     438                    } 
     439                     
     440                    widthNode = this.getElementsByTagNameNS(styleTypeNode,  
     441                                               this.internalns, 
     442                                               "width")[0]; 
     443                    if (widthNode) { 
     444                        style["strokeWidth"] = OpenLayers.Util.getXmlNodeValue( 
     445                                                    widthNode); 
     446                    } 
     447 
     448                case "polystyle": 
     449                    colorNode = this.getElementsByTagNameNS(styleTypeNode,  
     450                                               this.internalns,  
     451                                               "color")[0]; 
     452                    if (colorNode) { 
     453                        var colorNodeValue = OpenLayers.Util.getXmlNodeValue( 
     454                                                  colorNode); 
     455                        var matches = colorNodeValue.match( 
     456                                          this.regExes.kmlColor); 
     457 
     458                        console.info(matches); 
     459                        // transparency 
     460                        var alpha = matches[1]; 
     461                        style["fillOpacity"] = parseInt(alpha, 16) / 255; 
     462 
     463                        // rgb colors (google uses bgr) 
     464                        var b = matches[2];  
     465                        var g = matches[3];  
     466                        var r = matches[4];  
     467                        style["fillColor"] = "#" + r + g + b; 
     468                    } 
     469                     
     470                    break; 
     471                case "iconstyle": 
     472                    iconNode = this.getElementsByTagNameNS(styleTypeNode,  
     473                                               this.internalns,  
     474                                               "Icon")[0]; 
     475 
     476                    // set default width and height of icon 
     477                    style["graphicWidth"] = 32; 
     478                    style["graphicHeight"] = 32; 
     479 
     480                    if (iconNode) { 
     481                        hrefNode = this.getElementsByTagNameNS(iconNode,  
     482                                               this.internalns,  
     483                                               "href")[0]; 
     484                        if (hrefNode) {                                                    
     485                            href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
     486 
     487                            // support for internal icons  
     488                            //    (/root://icons/palette-x.png) 
     489                            // x and y tell the position on the palette: 
     490                            // - in pixels 
     491                            // - starting from the left bottom 
     492                            // We need to translate that to a position in the  
     493                            // list and request the appropriate icon from the  
     494                            // google maps website 
     495                            matches = href.match(this.regExes.kmlIconPalette); 
     496                            if (matches)  { 
     497                                var palette = matches[1]; 
     498                                var file_extension = matches[2]; 
     499 
     500                                var xNode = this.getElementsByTagNameNS(iconNode,  
     501                                                       this.internalns,  
     502                                                       "x")[0]; 
     503                                var x = OpenLayers.Util.getXmlNodeValue(xNode); 
     504 
     505                                var yNode = this.getElementsByTagNameNS(iconNode,  
     506                                                       this.internalns,  
     507                                                       "y")[0]; 
     508                                var y = OpenLayers.Util.getXmlNodeValue(yNode); 
     509 
     510                                var posX = x ? x/32 : 0; 
     511                                var posY = y ? (7 - y/32) : 7; 
     512 
     513                                var pos = posY * 8 + posX; 
     514                                href = "http://maps.google.com/mapfiles/kml/pal"  
     515                                     + palette + "/icon" + pos + file_extension; 
     516                                //console.info(x,y, pos, href); 
     517                            } 
     518 
     519 
     520                            var wNode = this.getElementsByTagNameNS(iconNode,  
     521                                                   this.internalns,  
     522                                                   "w")[0]; 
     523                            w = OpenLayers.Util.getXmlNodeValue(wNode); 
     524 
     525                            var hNode = this.getElementsByTagNameNS(iconNode,  
     526                                                   this.internalns,  
     527                                                   "h")[0]; 
     528                            h = OpenLayers.Util.getXmlNodeValue(hNode); 
     529 
     530                            if (w) { 
     531                                style["graphicWidth"] = parseInt(w); 
     532                            } 
     533 
     534                            if (h) { 
     535                                style["graphicHeight"] = parseInt(h); 
     536                            } 
     537 
     538                            style["graphicOpacity"] = 1; 
     539                            style["externalGraphic"] = href; 
    450540                        } 
    451                          
    452                         widthNode = this.getElementsByTagNameNS(styleTypeNode,  
    453                                                    "*", 
    454                                                    "width")[0]; 
    455                         if (widthNode) { 
    456                             style["strokeWidth"] = OpenLayers.Util.getXmlNodeValue(widthNode); 
     541 
     542                    } 
     543 
     544 
     545                    // hotSpots define the offset for an Icon 
     546                    hotSpotNode = this.getElementsByTagNameNS(styleTypeNode,  
     547                                               this.internalns,  
     548                                               "hotSpot")[0]; 
     549                    if (false && hotSpotNode) { 
     550                        var x = hotSpotNode.getAttribute("x"); 
     551                        var y = hotSpotNode.getAttribute("y"); 
     552 
     553                        var xUnits = hotSpotNode.getAttribute("xunits"); 
     554                        if (xUnits == "pixels") { 
     555                            style["graphicXOffset"] = parseInt(x); 
    457556                        } 
    458  
    459                     case "polystyle": 
    460                         colorNode = this.getElementsByTagNameNS(styleTypeNode,  
    461                                                    "*",  
    462                                                    "color")[0]; 
    463                         if (colorNode) { 
    464                             var alpha = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(0, 2); 
    465                             style["fillOpacity"] = parseInt(alpha, 16) / 255; 
    466  
    467                             var b = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(2, 4); 
    468                             var g = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(4, 6); 
    469                             var r = (OpenLayers.Util.getXmlNodeValue(colorNode)).substring(6, 8); 
    470                             style["fillColor"] = "#" + r + g + b; 
     557                        else if (xUnits == "insetPixels") { 
     558                            style["graphicXOffset"] = style["graphicWidth"]  
     559                                                          - parseInt(x); 
    471560                        } 
    472                          
    473                         break; 
    474                     case "iconstyle": 
    475                         iconNode = this.getElementsByTagNameNS(styleTypeNode,  
    476                                                    "*",  
    477                                                    "Icon")[0]; 
    478  
    479                         // set default width and height of icon 
    480                         style["graphicWidth"] = 32; 
    481                         style["graphicHeight"] = 32; 
    482  
    483                         if (iconNode) { 
    484                             hrefNode = this.getElementsByTagNameNS(iconNode,  
    485                                                    "*",  
    486                                                    "href")[0]; 
    487                             if (hrefNode) {                                                    
    488                                 href = OpenLayers.Util.getXmlNodeValue(hrefNode); 
    489  
    490                                 // support for internal icons (/root://icons/palette-x.png) 
    491                                 // x and y tell the position on the palette: 
    492                                 // - in pixels 
    493                                 // - starting from the left bottom 
    494                                 // We need to translate that to a position in the list 
    495                                 // and request the appropriate icon from the  
    496                                 // google maps website 
    497                                 matches = href.match(/root:\/\/icons\/palette-(\d+)(\.\w+)/); 
    498                                 if (matches)  { 
    499                                     var palette = matches[1]; 
    500                                     var file_extension = matches[2]; 
    501  
    502                                     var xNode = this.getElementsByTagNameNS(iconNode,  
    503                                                            "*",  
    504                                                            "x")[0]; 
    505                                     var x = OpenLayers.Util.getXmlNodeValue(xNode); 
    506  
    507                                     var yNode = this.getElementsByTagNameNS(iconNode,  
    508                                                            "*",  
    509                                                            "y")[0]; 
    510                                     var y = OpenLayers.Util.getXmlNodeValue(yNode); 
    511  
    512                                     var posX = x ? x/32 : 0; 
    513                                     var posY = y ? (7 - y/32) : 7; 
    514  
    515                                     var pos = posY * 8 + posX; 
    516                                     href = "http://maps.google.com/mapfiles/kml/pal" + palette + "/icon" + pos + file_extension; 
    517                                     //console.info(x,y, pos, href); 
    518                                 } 
    519  
    520  
    521                                 var wNode = this.getElementsByTagNameNS(iconNode,  
    522                                                        "*",  
    523                                                        "w")[0]; 
    524                                 w = OpenLayers.Util.getXmlNodeValue(wNode); 
    525  
    526                                 var hNode = this.getElementsByTagNameNS(iconNode,  
    527                                                        "*",  
    528                                                        "h")[0]; 
    529                                 h = OpenLayers.Util.getXmlNodeValue(hNode); 
    530  
    531                                 if (w) { 
    532                                     style["graphicWidth"] = parseInt(w); 
    533                                 } 
    534  
    535                                 if (h) { 
    536                                     style["graphicHeight"] = parseInt(h); 
    537                                 } 
    538  
    539                                 style["graphicOpacity"] = 1; 
    540                                 style["externalGraphic"] = href; 
    541                             } 
    542  
     561                        else if (xUnits == "fraction") { 
     562                            style["graphicXOffset"] = style["graphicWidth"]  
     563                                                          * parseFloat(x); 
    543564                        } 
    544565 
    545  
    546                         // hotSpots define the offset for an Icon 
    547                         hotSpotNode = this.getElementsByTagNameNS(styleTypeNode,  
    548                                                    "*",  
    549                                                    "hotSpot")[0]; 
    550                         if (false && hotSpotNode) { 
    551                             var x = hotSpotNode.getAttribute("x"); 
    552                             var y = hotSpotNode.getAttribute("y"); 
    553  
    554                             var xUnits = hotSpotNode.getAttribute("xunits"); 
    555                             if (xUnits == "pixels") { 
    556                                 style["graphicXOffset"] = parseInt(x); 
    557                             } 
    558                             else if (xUnits == "insetPixels") { 
    559                                 style["graphicXOffset"] = style["graphicWidth"] - parseInt(x); 
    560                             } 
    561                             else if (xUnits == "fraction") { 
    562                                 style["graphicXOffset"] = style["graphicWidth"] * parseFloat(x); 
    563                             } 
    564  
    565                             var yUnits = hotSpotNode.getAttribute("yunits"); 
    566                             if (yUnits == "pixels") { 
    567                                 style["graphicYOffset"] = parseInt(y); 
    568                             } 
    569                             else if (yUnits == "insetPixels") { 
    570                                 style["graphicYOffset"] = style["graphicHeight"] - parseInt(y); 
    571                             } 
    572                             else if (yUnits == "fraction") { 
    573                                 style["graphicYOffset"] = style["graphicHeight"] * parseFloat(y); 
    574                             } 
    575                             //console.info(x, xUnits, y, yUnits, style["graphicXOffset"], style["graphicYOffset"]); 
     566                        var yUnits = hotSpotNode.getAttribute("yunits"); 
     567                        if (yUnits == "pixels") { 
     568                            style["graphicYOffset"] = parseInt(y); 
    576569                        } 
    577  
    578  
    579                              
    580                         break; 
    581                     case "balloonstyle": 
    582                         balloonStyle = OpenLayers.Util.getXmlNodeValue(styleTypeNode); 
    583                         if (balloonStyle) { 
    584                             balloonStyle = balloonStyle.replace(/\$\[(.*?)\]/g, "${$1}"); 
    585                             style["balloonStyle"] = balloonStyle; 
    586                             //console.info(balloonStyle); 
     570                        else if (yUnits == "insetPixels") { 
     571                            style["graphicYOffset"] = style["graphicHeight"]  
     572                                                          - parseInt(y); 
    587573                        } 
    588                         break; 
    589                     default: 
    590                 } 
    591             } 
    592         } 
     574                        else if (yUnits == "fraction") { 
     575                            style["graphicYOffset"] = style["graphicHeight"]  
     576                                                          * parseFloat(y); 
     577                        } 
     578                        //console.info(x, xUnits, y, yUnits, style["graphicXOffset"],  
     579                        //  style["graphicYOffset"]); 
     580                    } 
     581                    break; 
     582                case "balloonstyle": 
     583                    balloonStyle = OpenLayers.Util.getXmlNodeValue(styleTypeNode); 
     584                    if (balloonStyle) { 
     585                        style["balloonStyle"] = balloonStyle.replace( 
     586                                                    straightBracket, "${$1}"); 
     587                        //console.info(balloonStyle); 
     588                    } 
     589                    break; 
     590                default: 
     591            } 
     592        } 
     593 
     594 
     595        // Some polygons have no line color, so we use that 
    593596        if (!style["strokeColor"]) { 
    594597            style["strokeColor"] = style["fillColor"]; 
    595598        } 
    596599 
    597         var id = node.getAttribute("id");  
    598         if (!id) { 
    599             id = "generated_" + Math.floor(Math.random()*100000); 
    600         } 
    601         style["name"] =  id; 
    602         //var styleObject = new OpenLayers.Style(style, options); 
    603         //console.debug(style); 
     600        // Give the style a name 
     601        style["name"] =  node.getAttribute("id"); 
     602 
    604603        return style; 
    605604    },