| 1 |
define([ |
| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
constructor: function(geometry){ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
this._geometry = geometry; |
| 31 |
this._shapeProperties = {}; |
| 32 |
this._fill = null; |
| 33 |
this._stroke = null; |
| 34 |
}, |
| 35 |
|
| 36 |
_createCollection: function(g){ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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(g){ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
return this._shapeProperties; |
| 116 |
}, |
| 117 |
|
| 118 |
setShapeProperties: function(s){ |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
this._shapeProperties = s; |
| 124 |
return this; |
| 125 |
}, |
| 126 |
|
| 127 |
createShape: function(s, g){ |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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; |
| 175 |
}, |
| 176 |
|
| 177 |
_createPoint: function(g){ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 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(g){ |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 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(g){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 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(g){ |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 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 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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); |
| 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 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 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 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 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 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
this._stroke = s; |
| 361 |
return this; |
| 362 |
}, |
| 363 |
|
| 364 |
getStroke: function(){ |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
return this._stroke; |
| 370 |
}, |
| 371 |
|
| 372 |
setFill: function(f){ |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
this._fill = f; |
| 378 |
return this; |
| 379 |
}, |
| 380 |
|
| 381 |
getFill: function(){ |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
return this._fill; |
| 387 |
}, |
| 388 |
|
| 389 |
remove: function(){ |
| 390 |
|
| 391 |
|
| 392 |
|
| 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 |
}); |