OpenLayers OpenLayers

Ticket #736: externalGraphic.3.diff

File externalGraphic.3.diff, 14.4 kB (added by ahocevar, 1 year ago)

Added tests in Layer/test_Vector.html; the patch is against the trunk and replaces previous patches for this ticket.

  • /mnt/d/eclipse/workspace/openlayers/examples/vector-features.html

    old new  
    77            border: 1px solid black; 
    88        } 
    99    </style> 
    10     <script src="../lib/OpenLayers.js"></script> 
     10    <script src="../lib/OpenLayers.js" type="text/javascript"></script> 
    1111    <script type="text/javascript"> 
    1212        <!-- 
    1313        var map; 
     
    2121            var style_blue = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); 
    2222            style_blue.strokeColor = "blue";  
    2323            style_blue.fillColor = "blue";  
     24            style_blue.externalGraphic = "../img/marker.png"; 
     25             
     26            // each of the three lines below means the same, if only one of 
     27            // them is active: the image will have a size of 24px, and the 
     28            // aspect ratio will be kept 
     29            style_blue.pointRadius = 12; 
     30            //style_blue.graphicWidth = 24; 
     31            //style_blue.graphicHeight = 24; 
     32             
     33            style_blue.fillOpacity = 1; 
    2434            var style_green = { 
    2535                strokeColor: "#00FF00", 
    2636                strokeOpacity: 1, 
     
    2838                pointRadius: 6, 
    2939                pointerEvents: "visiblePainted" 
    3040            }; 
     41            var style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); 
     42             
     43            // if graphicWidth and graphicHeight are both set, the aspect ratio 
     44            // of the image will be ignored 
     45            style_mark.graphicWidth = 24; 
     46            style_mark.graphicHeight = 20; 
     47             
     48            style_mark.externalGraphic = "../img/marker.png"; 
    3149             
    3250            var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry"); 
    3351             
     
    3452            // create a point feature 
    3553            var point = new OpenLayers.Geometry.Point(-111.04, 45.68); 
    3654            var pointFeature = new OpenLayers.Feature.Vector(point,null,style_blue); 
     55            var point2 = new OpenLayers.Geometry.Point(-105.04, 49.68); 
     56            var pointFeature2 = new OpenLayers.Feature.Vector(point2,null,style_green); 
     57            var point3 = new OpenLayers.Geometry.Point(-105.04, 49.68); 
     58            var pointFeature3 = new OpenLayers.Feature.Vector(point3,null,style_mark); 
    3759             
    3860            // create a line feature from a list of points 
    3961            var pointList = []; 
     
    6486             
    6587            map.addLayer(vectorLayer); 
    6688            map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5); 
    67             vectorLayer.addFeatures([pointFeature, lineFeature, polygonFeature]); 
     89            vectorLayer.addFeatures([pointFeature, pointFeature3, pointFeature2, lineFeature, polygonFeature]); 
    6890        } 
    6991        // --> 
    7092    </script> 
     
    7597       in different styles, created 'manually', by constructing the entire style 
    7698       object, via 'copy', extending the default style object, and by  
    7799       inheriting the default style from the layer.</p> 
     100    <p>It also shows how to use external graphic files for point features 
     101       and how to set their size: If either graphicWidth or graphicHeight is set, 
     102       the aspect ratio of the image will be respected. If both graphicWidth and 
     103       graphicHeight are set, it will be ignored. Alternatively, if graphicWidth 
     104       and graphicHeight are omitted, pointRadius will be used to set the size 
     105       of the image, which will then be twice the value of pointRadius with the 
     106       original aspect ratio.</p> 
    78107  </body> 
    79108</html> 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Renderer/SVG.js

    old new  
    165165        options = options || node._options; 
    166166 
    167167        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    168             node.setAttributeNS(null, "r", style.pointRadius); 
     168            if (style.externalGraphic) { 
     169                // remove old node 
     170                var id = node.getAttributeNS(null, "id"); 
     171                var x = node.getAttributeNS(null, "cx"); 
     172                var y = node.getAttributeNS(null, "cy"); 
     173                var _featureId = node._featureId; 
     174                var _geometryClass = node._geometryClass; 
     175                var _style = node._style; 
     176                this.root.removeChild(node); 
     177                 
     178                // create new image node 
     179                var node = this.createNode("image", id); 
     180                node._featureId = _featureId; 
     181                node._geometryClass = _geometryClass; 
     182                node._style = _style; 
     183                this.root.appendChild(node); 
     184 
     185                // now style the new node 
     186                if (style.graphicWidth && style.graphicHeight) { 
     187                  node.setAttributeNS(null, "preserveAspectRatio", "none"); 
     188                } 
     189                var width = style.graphicWidth || style.graphicHeight; 
     190                var height = style.graphicHeight || style.graphicWidth; 
     191                width = width ? width : style.pointRadius*2; 
     192                height = height ? height : style.pointRadius*2; 
     193                 
     194                node.setAttributeNS(null, "x", x-(.5*width).toFixed()); 
     195                node.setAttributeNS(null, "y", -y-(.5*height).toFixed()); 
     196                node.setAttributeNS(null, "width", width); 
     197                node.setAttributeNS(null, "height", height); 
     198                node.setAttributeNS("http://www.w3.org/1999/xlink", "href", style.externalGraphic); 
     199                node.setAttributeNS(null, "transform", "scale(1,-1)"); 
     200                node.setAttributeNS(null, "style", "opacity: "+style.fillOpacity); 
     201            } else { 
     202                node.setAttributeNS(null, "r", style.pointRadius); 
     203            } 
    169204        } 
    170205         
    171206        if (options.isFilled) { 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Renderer/VML.js

    old new  
    129129        options = options || node._options; 
    130130         
    131131        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    132             this.drawCircle(node, geometry, style.pointRadius); 
     132            if (style.externalGraphic) { 
     133                // remove old node 
     134                var id = node.id; 
     135                var _featureId = node._featureId; 
     136                var _geometryClass = node._geometryClass; 
     137                var _style = node._style; 
     138                this.root.removeChild(node); 
     139                 
     140                // create new image node 
     141                var node = this.createNode("v:rect", id); 
     142                var fill = this.createNode("v:fill", id+"_image"); 
     143                node.appendChild(fill); 
     144                node._featureId = _featureId; 
     145                node._geometryClass = _geometryClass; 
     146                node._style = _style; 
     147                this.root.appendChild(node); 
     148                 
     149                fill.src = style.externalGraphic; 
     150                fill.type = "frame"; 
     151                node.style.flip = "y"; 
     152                 
     153                if (!(style.graphicWidth && style.graphicHeight)) { 
     154                  fill.aspect = "atmost"; 
     155                } 
     156                 
     157                // now style the new node 
     158                var width = style.graphicWidth || style.graphicHeight; 
     159                var height = style.graphicHeight || style.graphicWidth; 
     160                width = width ? width : style.pointRadius*2; 
     161                height = height ? height : style.pointRadius*2; 
     162                var resolution = this.getResolution(); 
     163                node.style.left = (geometry.x/resolution-.5*width).toFixed(); 
     164                node.style.top = (geometry.y/resolution-.5*height).toFixed(); 
     165                node.style.width = width; 
     166                node.style.height = height;     
     167                 
     168                // modify fill style for rect styling below 
     169                style.fillColor = "none"; 
     170                style.strokeColor = "none"; 
     171                          
     172            } else { 
     173                this.drawCircle(node, geometry, style.pointRadius); 
     174            } 
    133175        } 
    134176 
    135177      //fill 
     
    146188                fill = this.createNode('v:fill', node.id + "_fill"); 
    147189                node.appendChild(fill); 
    148190            } 
    149             fill.setAttribute("opacity", style.fillOpacity); 
     191            if (style.fillOpacity) { 
     192                fill.setAttribute("opacity", style.fillOpacity); 
     193            } 
    150194        } 
    151195 
    152196 
  • /mnt/d/eclipse/workspace/openlayers/tests/Layer/test_Vector.html

    old new  
    106106             "given a custom style, renders with that"); 
    107107         
    108108    } 
    109  
     109     
    110110    function test_Layer_Vector_eraseFeatures(t) { 
    111111        t.plan(2); 
    112112        var layer = new OpenLayers.Layer.Vector("Test Layer"); 
     
    156156        t.eq(layer.map, null, "layer.map is null after destroy"); 
    157157    } 
    158158 
     159    function test_Layer_Vector_externalGraphic(t) { 
     160            t.plan(8); 
     161            // base layer is needed for getResolution() to return a value, 
     162            // otherwise VML test will fail because style.left and style.top 
     163            // cannot be set 
     164            var baseLayer = new OpenLayers.Layer.WMS("Base Layer", 
     165                    "http://octo.metacarta.com/cgi-bin/mapserv", 
     166                    { map: '/mapdata/vmap_wms.map',  
     167                   layers: 'basic',  
     168                   format: 'image/png'}); 
     169             
     170        var layer = new OpenLayers.Layer.Vector("Test Layer"); 
     171        var map = new OpenLayers.Map('map'); 
     172        map.addLayers([baseLayer, layer]); 
     173 
     174        var geometry = new OpenLayers.Geometry.Point(10, 10); 
     175        var feature = new OpenLayers.Feature.Vector(geometry); 
     176 
     177        var customStyle1 = new Object({ 
     178                externalGraphic: 'test.png', 
     179                pointRadius: 10 
     180        }); 
     181        var customStyle2 = new Object({ 
     182                externalGraphic: 'test.png', 
     183                graphicWidth: 12 
     184        }); 
     185        var customStyle3 = new Object({ 
     186                externalGraphic: 'test.png', 
     187                graphicHeight: 14 
     188        }); 
     189        var customStyle4 = new Object({ 
     190                externalGraphic: 'test.png', 
     191                graphicWidth: 24, 
     192                graphicHeight: 16 
     193        }); 
     194                
     195        var root = layer.renderer.root; 
     196        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.SVG') { 
     197                feature.style = customStyle1; 
     198                layer.drawFeature(feature); 
     199                t.eq(root.firstChild.getAttributeNS(null, 'width'), 
     200                            (2*customStyle1.pointRadius).toString(), 
     201                            "given a pointRadius, width equals 2*pointRadius"); 
     202                t.eq(root.firstChild.getAttributeNS(null, 'height'), 
     203                            (2*customStyle1.pointRadius).toString(), 
     204                            "given a pointRadius, height equals 2*pointRadius"); 
     205                feature.style = customStyle2; 
     206                layer.drawFeature(feature); 
     207                t.eq(root.firstChild.getAttributeNS(null, 'width'), 
     208                            root.firstChild.getAttributeNS(null, 'height'), 
     209                            "given a graphicWidth, width equals height"); 
     210                t.eq(root.firstChild.getAttributeNS(null, 'width'), 
     211                            customStyle2.graphicWidth.toString(), 
     212                            "width is set correctly"); 
     213                feature.style = customStyle3; 
     214                layer.drawFeature(feature); 
     215                t.eq(root.firstChild.getAttributeNS(null, 'height'), 
     216                            root.firstChild.getAttributeNS(null, 'width'), 
     217                            "given a graphicHeight, height equals width"); 
     218                t.eq(root.firstChild.getAttributeNS(null, 'height'), 
     219                            customStyle3.graphicHeight.toString(), 
     220                            "height is set correctly"); 
     221                feature.style = customStyle4; 
     222                layer.drawFeature(feature); 
     223                t.eq(root.firstChild.getAttributeNS(null, 'height'), 
     224                            customStyle4.graphicHeight.toString(), 
     225                            "given graphicHeight and graphicWidth, both are set: height") 
     226                t.eq(root.firstChild.getAttributeNS(null, 'width'), 
     227                            customStyle4.graphicWidth.toString(), 
     228                            "given graphicHeight and graphicWidth, both are set: width") 
     229        } 
     230        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.VML') { 
     231                feature.style = customStyle1; 
     232                layer.drawFeature(feature); 
     233                t.eq(root.firstChild.style.width, 
     234                            (2*customStyle1.pointRadius).toString()+'px', 
     235                            "given a pointRadius, width equals 2*pointRadius"); 
     236                t.eq(root.firstChild.style.height, 
     237                            (2*customStyle1.pointRadius).toString()+'px', 
     238                            "given a pointRadius, height equals 2*pointRadius"); 
     239                feature.style = customStyle2; 
     240                layer.drawFeature(feature); 
     241                t.eq(root.firstChild.style.width, 
     242                            root.firstChild.style.height, 
     243                            "given a graphicWidth, width equals height"); 
     244                t.eq(root.firstChild.style.width, 
     245                            customStyle2.graphicWidth.toString()+'px', 
     246                            "width is set correctly"); 
     247                feature.style = customStyle3; 
     248                layer.drawFeature(feature); 
     249                t.eq(root.firstChild.style.height, 
     250                            root.firstChild.style.width, 
     251                            "given a graphicHeight, height equals width"); 
     252                t.eq(root.firstChild.style.height, 
     253                            customStyle3.graphicHeight.toString()+'px', 
     254                            "height is set correctly"); 
     255                feature.style = customStyle4; 
     256                layer.drawFeature(feature); 
     257                t.eq(root.firstChild.style.height, 
     258                            customStyle4.graphicHeight.toString()+'px', 
     259                            "given graphicHeight and graphicWidth, both are set: height") 
     260                t.eq(root.firstChild.style.width, 
     261                            customStyle4.graphicWidth.toString()+'px', 
     262                            "given graphicHeight and graphicWidth, both are set: width") 
     263        } 
     264    } 
     265 
    159266  // --> 
    160267  </script> 
    161268</head>