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