OpenLayers OpenLayers

Changeset 3729

Show
Ignore:
Timestamp:
07/13/07 09:31:40 (1 year ago)
Author:
crschmidt
Message:

Add support to the vector layer to visualize point geometries with images. This
support was added, tested, and documented by Andreas Hocevar, and I want to
thank him for the work he put into this patch. It looks pretty great. (This
is from ticket #736.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/vector-features.html

    r3369 r3729  
    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        <!-- 
     
    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", 
     
    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"); 
     
    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 
     
    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        // --> 
     
    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> 
  • trunk/openlayers/lib/OpenLayers/Renderer/SVG.js

    r3609 r3729  
    184184 
    185185        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    186             node.setAttributeNS(null, "r", style.pointRadius); 
     186            if (style.externalGraphic) { 
     187                // remove old node 
     188                var id = node.getAttributeNS(null, "id"); 
     189                var x = node.getAttributeNS(null, "cx"); 
     190                var y = node.getAttributeNS(null, "cy"); 
     191                var _featureId = node._featureId; 
     192                var _geometryClass = node._geometryClass; 
     193                var _style = node._style; 
     194                this.root.removeChild(node); 
     195                 
     196                // create new image node 
     197                var node = this.createNode("image", id); 
     198                node._featureId = _featureId; 
     199                node._geometryClass = _geometryClass; 
     200                node._style = _style; 
     201                this.root.appendChild(node); 
     202 
     203                // now style the new node 
     204                if (style.graphicWidth && style.graphicHeight) { 
     205                  node.setAttributeNS(null, "preserveAspectRatio", "none"); 
     206                } 
     207                var width = style.graphicWidth || style.graphicHeight; 
     208                var height = style.graphicHeight || style.graphicWidth; 
     209                width = width ? width : style.pointRadius*2; 
     210                height = height ? height : style.pointRadius*2; 
     211                 
     212                node.setAttributeNS(null, "x", x-(.5*width).toFixed()); 
     213                node.setAttributeNS(null, "y", -y-(.5*height).toFixed()); 
     214                node.setAttributeNS(null, "width", width); 
     215                node.setAttributeNS(null, "height", height); 
     216                node.setAttributeNS("http://www.w3.org/1999/xlink", "href", style.externalGraphic); 
     217                node.setAttributeNS(null, "transform", "scale(1,-1)"); 
     218                node.setAttributeNS(null, "style", "opacity: "+style.fillOpacity); 
     219            } else { 
     220                node.setAttributeNS(null, "r", style.pointRadius); 
     221            } 
    187222        } 
    188223         
  • trunk/openlayers/lib/OpenLayers/Renderer/VML.js

    r3545 r3729  
    154154         
    155155        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    156             this.drawCircle(node, geometry, style.pointRadius); 
     156            if (style.externalGraphic) { 
     157                // remove old node 
     158                var id = node.id; 
     159                var _featureId = node._featureId; 
     160                var _geometryClass = node._geometryClass; 
     161                var _style = node._style; 
     162                this.root.removeChild(node); 
     163                 
     164                // create new image node 
     165                var node = this.createNode("v:rect", id); 
     166                var fill = this.createNode("v:fill", id+"_image"); 
     167                node.appendChild(fill); 
     168                node._featureId = _featureId; 
     169                node._geometryClass = _geometryClass; 
     170                node._style = _style; 
     171                this.root.appendChild(node); 
     172                 
     173                fill.src = style.externalGraphic; 
     174                fill.type = "frame"; 
     175                node.style.flip = "y"; 
     176                 
     177                if (!(style.graphicWidth && style.graphicHeight)) { 
     178                  fill.aspect = "atmost"; 
     179                } 
     180                 
     181                // now style the new node 
     182                var width = style.graphicWidth || style.graphicHeight; 
     183                var height = style.graphicHeight || style.graphicWidth; 
     184                width = width ? width : style.pointRadius*2; 
     185                height = height ? height : style.pointRadius*2; 
     186                var resolution = this.getResolution(); 
     187                node.style.left = (geometry.x/resolution-.5*width).toFixed(); 
     188                node.style.top = (geometry.y/resolution-.5*height).toFixed(); 
     189                node.style.width = width; 
     190                node.style.height = height;     
     191                 
     192                // modify fill style for rect styling below 
     193                style.fillColor = "none"; 
     194                style.strokeColor = "none"; 
     195                          
     196            } else { 
     197                this.drawCircle(node, geometry, style.pointRadius); 
     198            } 
    157199        } 
    158200 
     
    171213                node.appendChild(fill); 
    172214            } 
    173             fill.setAttribute("opacity", style.fillOpacity); 
     215            if (style.fillOpacity) { 
     216                fill.setAttribute("opacity", style.fillOpacity); 
     217            } 
    174218        } 
    175219 
  • trunk/openlayers/tests/Layer/test_Vector.html

    r3111 r3729  
    107107         
    108108    } 
    109  
     109     
    110110    function test_Layer_Vector_eraseFeatures(t) { 
    111111        t.plan(2); 
     
    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>