source: Dev/trunk/src/client/dojox/geo/openlayers/GeometryFeature.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 9.8 KB
RevLine 
[483]1define([
2        "dojo/_base/declare",
3        "dojo/_base/array",
4        "dojo/_base/lang",
5        "dojox/gfx/matrix",
6        "./Point",
7        "./LineString",
8        "./Collection",
9        "./Feature"
10], function(declare, array, lang, matrix, Point, LineString, Collection, Feature){
11
12        return declare("dojox.geo.openlayers.GeometryFeature", Feature, {
13                // summary:
14                //              A Feature encapsulating a geometry.
15                // description:
16                //              This Feature renders a geometry such as a Point or LineString geometry. This Feature
17                //              is responsible for reprojecting the geometry before creating a gfx shape to display it.
18                //              By default the shape created is a circle for a Point geometry and a polyline for a
19                //              LineString geometry. User can change these behavior by overriding the createShape
20                //              method to create the desired shape.
21                // example:
22                //      |  var geom = new dojox.geo.openlayers.Point({x:0, y:0});
23                //      |  var gf = new dojox.geo.openlayers.GeometryFeature(geom);
24
25                constructor: function(geometry){
26                        // summary:
27                        //              Constructs a GeometryFeature for the specified geometry.
28                        // geometry: dojox/geo/openlayers/Geometry
29                        //              The geometry to render.
30                        this._geometry = geometry;
31                        this._shapeProperties = {};
32                        this._fill = null;
33                        this._stroke = null;
34                },
35
36                _createCollection: function(/* dojox/geo/openlayers/Geometry */g){
37                        // summary:
38                        //              Create collection shape and add it to the viewport.
39                        // tags:
40                        //              private
41                        var layer = this.getLayer();
42                        var s = layer.getSurface();
43                        var c = this.createShape(s, g);
44                        var vp = layer.getViewport();
45                        vp.add(c);
46                        return c;
47                },
48
49                _getCollectionShape: function(/* dojox/geo/openlayers/Geometry */g){
50                        // summary:
51                        //              Get the collection shape, create it if necessary
52                        // tags:
53                        //              private
54                        var s = g.shape;
55                        if(s == null){
56                                s = this._createCollection(g);
57                                g.shape = s;
58                        }
59                        return s;
60                },
61
62                renderCollection: function(g){
63                        // summary:
64                        //              Renders a geometry collection.
65                        // g: dojox.geo.openlayers.Geometry?
66                        //              The geometry to render.
67                        if(g == undefined){
68                                g = this._geometry;
69                        }
70
71                        s = this._getCollectionShape(g);
72                        var prop = this.getShapeProperties();
73                        s.setShape(prop);
74
75                        array.forEach(g.coordinates, function(item){
76                                if(item instanceof Point){
77                                        this.renderPoint(item);
78                                }else if(item instanceof LineString){
79                                        this.renderLineString(item);
80                                }else if(item instanceof Collection){
81                                        this.renderCollection(item);
82                                }else{
83                                        throw new Error();
84                                }
85                        }, this);
86                        this._applyStyle(g);
87                },
88
89                render: function(g){
90                        // summary:
91                        //              Render a geometry.
92                        //              Called by the Layer on which the feature is added.
93                        // g: dojox/geo/openlayer/Geometry?
94                        //              The geometry to draw
95                        if(g == undefined){
96                                g = this._geometry;
97                        }
98
99                        if(g instanceof Point){
100                                this.renderPoint(g);
101                        }else if(g instanceof LineString){
102                                this.renderLineString(g);
103                        }else if(g instanceof Collection){
104                                this.renderCollection(g);
105                        }else{
106                                throw new Error();
107                        }
108                },
109
110                getShapeProperties: function(){
111                        // summary:
112                        //              Returns the shape properties.
113                        // returns: Object
114                        //              The shape properties.
115                        return this._shapeProperties;
116                },
117
118                setShapeProperties: function(s){
119                        // summary:
120                        //              Sets the shape properties.
121                        // s: Object
122                        //              The shape properties to set.
123                        this._shapeProperties = s;
124                        return this;
125                },
126
127                createShape: function(s, g){
128                        // summary:
129                        //              Called when the shape rendering the geometry has to be created.
130                        //              This default implementation creates a circle for a point geometry, a polyline for
131                        //              a LineString geometry and is recursively called when creating a collection.
132                        //              User may replace this method to produce a custom shape.
133                        // s: dojox/gfx/Surface
134                        //              The surface on which the method create the shapes.
135                        // g: dojox/geo/openlayers/Geometry?
136                        //              The reference geometry
137                        // returns:
138                        //              The resulting shape.
139                        if(!g){
140                                g = this._geometry;
141                        }
142
143                        var shape = null;
144                        if(g instanceof Point){
145                                shape = s.createCircle();
146                        }else if(g instanceof LineString){
147                                shape = s.createPolyline();
148                        }else if(g instanceof Collection){
149                                var grp = s.createGroup();
150                                array.forEach(g.coordinates, function(item){
151                                        var shp = this.createShape(s, item);
152                                        grp.add(shp);
153                                }, this);
154                                shape = grp;
155                        }else{
156                                throw new Error();
157                        }
158                        return shape;
159                },
160
161                getShape: function(){
162                        // summary:
163                        //              Returns the shape rendering the geometry
164                        // returns:
165                        //              The shape used to render the geometry.
166                        var g = this._geometry;
167                        if(!g){
168                                return null;
169                        }
170                        if(g.shape){
171                                return g.shape;
172                        }
173                        this.render();
174                        return g.shape; // dojox.gfx.shape.Shape
175                },
176
177                _createPoint: function(/* dojox/geo/openlayer/Geometry */g){
178                        // summary:
179                        //              Create a point shape
180                        // tags:
181                        //              private
182                        var layer = this.getLayer();
183                        var s = layer.getSurface();
184                        var c = this.createShape(s, g);
185                        var vp = layer.getViewport();
186                        vp.add(c);
187                        return c;
188                },
189
190                _getPointShape: function(/* dojox/geo/openlayers/Geometry */g){
191                        // summary:
192                        //              get the point geometry shape, create it if necessary
193                        // tags:
194                        //              private
195                        var s = g.shape;
196                        if(s == null){
197                                s = this._createPoint(g);
198                                g.shape = s;
199                        }
200                        return s;
201                },
202
203                renderPoint: function(g){
204                        // summary:
205                        //              Renders a point geometry.
206                        // g: dojox/geo/openlayers/Point?
207                        //              The geometry to render, or the current instance geometry if not specified.
208                        if(g == undefined){
209                                g = this._geometry;
210                        }
211                        var layer = this.getLayer();
212                        var map = layer.getDojoMap();
213
214                        s = this._getPointShape(g);
215                        var prop = lang.mixin({}, this._defaults.pointShape);
216                        prop = lang.mixin(prop, this.getShapeProperties());
217                        s.setShape(prop);
218
219                        var from = this.getCoordinateSystem();
220                        var p = map.transform(g.coordinates, from);
221
222                        var a = this._getLocalXY(p);
223                        var cx = a[0];
224                        var cy = a[1];
225                        var tr = layer.getViewport().getTransform();
226                        if(tr){
227                                s.setTransform(matrix.translate(cx - tr.dx, cy - tr.dy));
228                        }
229                        this._applyStyle(g);
230                },
231
232                _createLineString: function(/* dojox/geo/openlayers/Geometry */g){
233                        // summary:
234                        //              Create polyline shape and add it to the viewport.
235                        // tags:
236                        //              private
237                        var layer = this.getLayer();
238                        var s = layer._surface;
239                        var shape = this.createShape(s, g);
240                        var vp = layer.getViewport();
241                        vp.add(shape);
242                        g.shape = shape;
243                        return shape;
244                },
245
246                _getLineStringShape: function(/* dojox/geo/openlayers/Geometry */g){
247                        // summary:
248                        //              Get the line string geometry shape, create it if necessary
249                        // tags:
250                        //              private
251                        var s = g.shape;
252                        if(s == null){
253                                s = this._createLineString(g);
254                                g.shape = s;
255                        }
256                        return s;
257                },
258
259                renderLineString: function(g){
260                        // summary:
261                        //              Renders a line string geometry.
262                        // g: dojox/geo/openlayers/Geometry?
263                        //              The geometry to render.
264                        if(g == undefined){
265                                g = this._geometry;
266                        }
267                        var layer = this.getLayer();
268                        var map = layer.getDojoMap();
269                        var lss = this._getLineStringShape(g);
270                        var from = this.getCoordinateSystem();
271                        var points = new Array(g.coordinates.length); // ss.getShape().points;         
272                        var tr = layer.getViewport().getTransform();
273                        array.forEach(g.coordinates, function(c, i, array){
274                                var p = map.transform(c, from);
275                                var a = this._getLocalXY(p);
276                                if(tr){
277                                        a[0] -= tr.dx;
278                                        a[1] -= tr.dy;
279                                }
280                                points[i] = {
281                                        x: a[0],
282                                        y: a[1]
283                                };
284                        }, this);
285                        var prop = lang.mixin({}, this._defaults.lineStringShape);
286                        prop = lang.mixin(prop, this.getShapeProperties());
287                        prop = lang.mixin(prop, {
288                                points: points
289                        });
290                        lss.setShape(prop);
291                        this._applyStyle(g);
292                },
293
294                _applyStyle: function(g){
295                        // summary:
296                        //              Apply the style on the geometry's shape.
297                        // g: Geometry
298                        //              The geometry.
299                        // tags:
300                        //              private
301                        if(!g || !g.shape){
302                                return;
303                        }
304
305                        var f = this.getFill();
306
307                        var fill;
308                        if(!f || lang.isString(f) || lang.isArray(f)){
309                                fill = f;
310                        }else{
311                                fill = lang.mixin({}, this._defaults.fill);
312                                fill = lang.mixin(fill, f);
313                        }
314
315                        var s = this.getStroke();
316                        var stroke;
317                        if(!s || lang.isString(s) || lang.isArray(s)){
318                                stroke = s;
319                        }else{
320                                stroke = lang.mixin({}, this._defaults.stroke);
321                                stroke = lang.mixin(stroke, s);
322                        }
323
324                        this._applyRecusiveStyle(g, stroke, fill);
325                },
326
327                _applyRecusiveStyle: function(g, stroke, fill){
328                        // summary:
329                        //              Apply the style on the geometry's shape recursively.
330                        // g: dojox.geo.openlayers.Geometry
331                        //              The geometry.
332                        // stroke: Object
333                        //              The stroke
334                        // fill:Object
335                        //              The fill
336                        // tags:
337                        //              private
338                        var shp = g.shape;
339
340                        if(shp.setFill){
341                                shp.setFill(fill);
342                        }
343
344                        if(shp.setStroke){
345                                shp.setStroke(stroke);
346                        }
347
348                        if(g instanceof Collection){
349                                array.forEach(g.coordinates, function(i){
350                                        this._applyRecusiveStyle(i, stroke, fill);
351                                }, this);
352                        }
353                },
354
355                setStroke: function(s){
356                        // summary:
357                        //              Set the stroke style to be applied on the rendered shape.
358                        // s: Object
359                        //              The stroke style
360                        this._stroke = s;
361                        return this;
362                },
363
364                getStroke: function(){
365                        // summary:
366                        //              Returns the stroke style
367                        // returns:
368                        //              The stroke style
369                        return this._stroke;
370                },
371
372                setFill: function(f){
373                        // summary:
374                        //              Set the fill style to be applied on the rendered shape.
375                        // f: Object
376                        //              The fill style
377                        this._fill = f;
378                        return this;
379                },
380
381                getFill: function(){
382                        // summary:
383                        //              Returns the fill style
384                        // returns:
385                        //              The fill style
386                        return this._fill;
387                },
388
389                remove: function(){
390                        // summary:
391                        //              Removes the shape from the Surface.
392                        //              Called when the feature is removed from the layer.
393                        var g = this._geometry;
394                        var shp = g.shape;
395                        g.shape = null;
396                        if(shp){
397                                shp.removeShape();
398                        }
399                        if(g instanceof Collection){
400                                array.forEach(g.coordinates, function(i){
401                                        this.remove(i);
402                                }, this);
403                        }
404                },
405
406                _defaults: {
407                        fill: null,
408                        stroke: null,
409                        pointShape: {
410                                r: 30
411                        },
412                        lineStringShape: null
413                }
414
415        });
416});
Note: See TracBrowser for help on using the repository browser.