Renderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/BaseTypes/Class.js
  7. */
  8. /**
  9. * Class: OpenLayers.Renderer
  10. * This is the base class for all renderers.
  11. *
  12. * This is based on a merger code written by Paul Spencer and Bertil Chapuis.
  13. * It is largely composed of virtual functions that are to be implemented
  14. * in technology-specific subclasses, but there is some generic code too.
  15. *
  16. * The functions that *are* implemented here merely deal with the maintenance
  17. * of the size and extent variables, as well as the cached 'resolution'
  18. * value.
  19. *
  20. * A note to the user that all subclasses should use getResolution() instead
  21. * of directly accessing this.resolution in order to correctly use the
  22. * cacheing system.
  23. *
  24. */
  25. OpenLayers.Renderer = OpenLayers.Class({
  26. /**
  27. * Property: container
  28. * {DOMElement}
  29. */
  30. container: null,
  31. /**
  32. * Property: root
  33. * {DOMElement}
  34. */
  35. root: null,
  36. /**
  37. * Property: extent
  38. * {<OpenLayers.Bounds>}
  39. */
  40. extent: null,
  41. /**
  42. * Property: locked
  43. * {Boolean} If the renderer is currently in a state where many things
  44. * are changing, the 'locked' property is set to true. This means
  45. * that renderers can expect at least one more drawFeature event to be
  46. * called with the 'locked' property set to 'true': In some renderers,
  47. * this might make sense to use as a 'only update local information'
  48. * flag.
  49. */
  50. locked: false,
  51. /**
  52. * Property: size
  53. * {<OpenLayers.Size>}
  54. */
  55. size: null,
  56. /**
  57. * Property: resolution
  58. * {Float} cache of current map resolution
  59. */
  60. resolution: null,
  61. /**
  62. * Property: map
  63. * {<OpenLayers.Map>} Reference to the map -- this is set in Vector's setMap()
  64. */
  65. map: null,
  66. /**
  67. * Property: featureDx
  68. * {Number} Feature offset in x direction. Will be calculated for and
  69. * applied to the current feature while rendering (see
  70. * <calculateFeatureDx>).
  71. */
  72. featureDx: 0,
  73. /**
  74. * Constructor: OpenLayers.Renderer
  75. *
  76. * Parameters:
  77. * containerID - {<String>}
  78. * options - {Object} options for this renderer. See sublcasses for
  79. * supported options.
  80. */
  81. initialize: function(containerID, options) {
  82. this.container = OpenLayers.Util.getElement(containerID);
  83. OpenLayers.Util.extend(this, options);
  84. },
  85. /**
  86. * APIMethod: destroy
  87. */
  88. destroy: function() {
  89. this.container = null;
  90. this.extent = null;
  91. this.size = null;
  92. this.resolution = null;
  93. this.map = null;
  94. },
  95. /**
  96. * APIMethod: supported
  97. * This should be overridden by specific subclasses
  98. *
  99. * Returns:
  100. * {Boolean} Whether or not the browser supports the renderer class
  101. */
  102. supported: function() {
  103. return false;
  104. },
  105. /**
  106. * Method: setExtent
  107. * Set the visible part of the layer.
  108. *
  109. * Resolution has probably changed, so we nullify the resolution
  110. * cache (this.resolution) -- this way it will be re-computed when
  111. * next it is needed.
  112. * We nullify the resolution cache (this.resolution) if resolutionChanged
  113. * is set to true - this way it will be re-computed on the next
  114. * getResolution() request.
  115. *
  116. * Parameters:
  117. * extent - {<OpenLayers.Bounds>}
  118. * resolutionChanged - {Boolean}
  119. *
  120. * Returns:
  121. * {Boolean} true to notify the layer that the new extent does not exceed
  122. * the coordinate range, and the features will not need to be redrawn.
  123. * False otherwise.
  124. */
  125. setExtent: function(extent, resolutionChanged) {
  126. this.extent = extent.clone();
  127. if (this.map.baseLayer && this.map.baseLayer.wrapDateLine) {
  128. var ratio = extent.getWidth() / this.map.getExtent().getWidth(),
  129. extent = extent.scale(1 / ratio);
  130. this.extent = extent.wrapDateLine(this.map.getMaxExtent()).scale(ratio);
  131. }
  132. if (resolutionChanged) {
  133. this.resolution = null;
  134. }
  135. return true;
  136. },
  137. /**
  138. * Method: setSize
  139. * Sets the size of the drawing surface.
  140. *
  141. * Resolution has probably changed, so we nullify the resolution
  142. * cache (this.resolution) -- this way it will be re-computed when
  143. * next it is needed.
  144. *
  145. * Parameters:
  146. * size - {<OpenLayers.Size>}
  147. */
  148. setSize: function(size) {
  149. this.size = size.clone();
  150. this.resolution = null;
  151. },
  152. /**
  153. * Method: getResolution
  154. * Uses cached copy of resolution if available to minimize computing
  155. *
  156. * Returns:
  157. * {Float} The current map's resolution
  158. */
  159. getResolution: function() {
  160. this.resolution = this.resolution || this.map.getResolution();
  161. return this.resolution;
  162. },
  163. /**
  164. * Method: drawFeature
  165. * Draw the feature. The optional style argument can be used
  166. * to override the feature's own style. This method should only
  167. * be called from layer.drawFeature().
  168. *
  169. * Parameters:
  170. * feature - {<OpenLayers.Feature.Vector>}
  171. * style - {<Object>}
  172. *
  173. * Returns:
  174. * {Boolean} true if the feature has been drawn completely, false if not,
  175. * undefined if the feature had no geometry
  176. */
  177. drawFeature: function(feature, style) {
  178. if(style == null) {
  179. style = feature.style;
  180. }
  181. if (feature.geometry) {
  182. var bounds = feature.geometry.getBounds();
  183. if(bounds) {
  184. var worldBounds;
  185. if (this.map.baseLayer && this.map.baseLayer.wrapDateLine) {
  186. worldBounds = this.map.getMaxExtent();
  187. }
  188. if (!bounds.intersectsBounds(this.extent, {worldBounds: worldBounds})) {
  189. style = {display: "none"};
  190. } else {
  191. this.calculateFeatureDx(bounds, worldBounds);
  192. }
  193. var rendered = this.drawGeometry(feature.geometry, style, feature.id);
  194. if(style.display != "none" && style.label && rendered !== false) {
  195. var location = feature.geometry.getCentroid();
  196. if(style.labelXOffset || style.labelYOffset) {
  197. var xOffset = isNaN(style.labelXOffset) ? 0 : style.labelXOffset;
  198. var yOffset = isNaN(style.labelYOffset) ? 0 : style.labelYOffset;
  199. var res = this.getResolution();
  200. location.move(xOffset*res, yOffset*res);
  201. }
  202. this.drawText(feature.id, style, location);
  203. } else {
  204. this.removeText(feature.id);
  205. }
  206. return rendered;
  207. }
  208. }
  209. },
  210. /**
  211. * Method: calculateFeatureDx
  212. * {Number} Calculates the feature offset in x direction. Looking at the
  213. * center of the feature bounds and the renderer extent, we calculate how
  214. * many world widths the two are away from each other. This distance is
  215. * used to shift the feature as close as possible to the center of the
  216. * current enderer extent, which ensures that the feature is visible in the
  217. * current viewport.
  218. *
  219. * Parameters:
  220. * bounds - {<OpenLayers.Bounds>} Bounds of the feature
  221. * worldBounds - {<OpenLayers.Bounds>} Bounds of the world
  222. */
  223. calculateFeatureDx: function(bounds, worldBounds) {
  224. this.featureDx = 0;
  225. if (worldBounds) {
  226. var worldWidth = worldBounds.getWidth(),
  227. rendererCenterX = (this.extent.left + this.extent.right) / 2,
  228. featureCenterX = (bounds.left + bounds.right) / 2,
  229. worldsAway = Math.round((featureCenterX - rendererCenterX) / worldWidth);
  230. this.featureDx = worldsAway * worldWidth;
  231. }
  232. },
  233. /**
  234. * Method: drawGeometry
  235. *
  236. * Draw a geometry. This should only be called from the renderer itself.
  237. * Use layer.drawFeature() from outside the renderer.
  238. * virtual function
  239. *
  240. * Parameters:
  241. * geometry - {<OpenLayers.Geometry>}
  242. * style - {Object}
  243. * featureId - {<String>}
  244. */
  245. drawGeometry: function(geometry, style, featureId) {},
  246. /**
  247. * Method: drawText
  248. * Function for drawing text labels.
  249. * This method is only called by the renderer itself.
  250. *
  251. * Parameters:
  252. * featureId - {String}
  253. * style -
  254. * location - {<OpenLayers.Geometry.Point>}
  255. */
  256. drawText: function(featureId, style, location) {},
  257. /**
  258. * Method: removeText
  259. * Function for removing text labels.
  260. * This method is only called by the renderer itself.
  261. *
  262. * Parameters:
  263. * featureId - {String}
  264. */
  265. removeText: function(featureId) {},
  266. /**
  267. * Method: clear
  268. * Clear all vectors from the renderer.
  269. * virtual function.
  270. */
  271. clear: function() {},
  272. /**
  273. * Method: getFeatureIdFromEvent
  274. * Returns a feature id from an event on the renderer.
  275. * How this happens is specific to the renderer. This should be
  276. * called from layer.getFeatureFromEvent().
  277. * Virtual function.
  278. *
  279. * Parameters:
  280. * evt - {<OpenLayers.Event>}
  281. *
  282. * Returns:
  283. * {String} A feature id or undefined.
  284. */
  285. getFeatureIdFromEvent: function(evt) {},
  286. /**
  287. * Method: eraseFeatures
  288. * This is called by the layer to erase features
  289. *
  290. * Parameters:
  291. * features - {Array(<OpenLayers.Feature.Vector>)}
  292. */
  293. eraseFeatures: function(features) {
  294. if(!(OpenLayers.Util.isArray(features))) {
  295. features = [features];
  296. }
  297. for(var i=0, len=features.length; i<len; ++i) {
  298. var feature = features[i];
  299. this.eraseGeometry(feature.geometry, feature.id);
  300. this.removeText(feature.id);
  301. }
  302. },
  303. /**
  304. * Method: eraseGeometry
  305. * Remove a geometry from the renderer (by id).
  306. * virtual function.
  307. *
  308. * Parameters:
  309. * geometry - {<OpenLayers.Geometry>}
  310. * featureId - {String}
  311. */
  312. eraseGeometry: function(geometry, featureId) {},
  313. /**
  314. * Method: moveRoot
  315. * moves this renderer's root to a (different) renderer.
  316. * To be implemented by subclasses that require a common renderer root for
  317. * feature selection.
  318. *
  319. * Parameters:
  320. * renderer - {<OpenLayers.Renderer>} target renderer for the moved root
  321. */
  322. moveRoot: function(renderer) {},
  323. /**
  324. * Method: getRenderLayerId
  325. * Gets the layer that this renderer's output appears on. If moveRoot was
  326. * used, this will be different from the id of the layer containing the
  327. * features rendered by this renderer.
  328. *
  329. * Returns:
  330. * {String} the id of the output layer.
  331. */
  332. getRenderLayerId: function() {
  333. return this.container.id;
  334. },
  335. /**
  336. * Method: applyDefaultSymbolizer
  337. *
  338. * Parameters:
  339. * symbolizer - {Object}
  340. *
  341. * Returns:
  342. * {Object}
  343. */
  344. applyDefaultSymbolizer: function(symbolizer) {
  345. var result = OpenLayers.Util.extend({},
  346. OpenLayers.Renderer.defaultSymbolizer);
  347. if(symbolizer.stroke === false) {
  348. delete result.strokeWidth;
  349. delete result.strokeColor;
  350. }
  351. if(symbolizer.fill === false) {
  352. delete result.fillColor;
  353. }
  354. OpenLayers.Util.extend(result, symbolizer);
  355. return result;
  356. },
  357. CLASS_NAME: "OpenLayers.Renderer"
  358. });
  359. /**
  360. * Constant: OpenLayers.Renderer.defaultSymbolizer
  361. * {Object} Properties from this symbolizer will be applied to symbolizers
  362. * with missing properties. This can also be used to set a global
  363. * symbolizer default in OpenLayers. To be SLD 1.x compliant, add the
  364. * following code before rendering any vector features:
  365. * (code)
  366. * OpenLayers.Renderer.defaultSymbolizer = {
  367. * fillColor: "#808080",
  368. * fillOpacity: 1,
  369. * strokeColor: "#000000",
  370. * strokeOpacity: 1,
  371. * strokeWidth: 1,
  372. * pointRadius: 3,
  373. * graphicName: "square"
  374. * };
  375. * (end)
  376. */
  377. OpenLayers.Renderer.defaultSymbolizer = {
  378. fillColor: "#000000",
  379. strokeColor: "#000000",
  380. strokeWidth: 2,
  381. fillOpacity: 1,
  382. strokeOpacity: 1,
  383. pointRadius: 0,
  384. labelAlign: 'cm'
  385. };
  386. /**
  387. * Constant: OpenLayers.Renderer.symbol
  388. * Coordinate arrays for well known (named) symbols.
  389. */
  390. OpenLayers.Renderer.symbol = {
  391. "star": [350,75, 379,161, 469,161, 397,215, 423,301, 350,250, 277,301,
  392. 303,215, 231,161, 321,161, 350,75],
  393. "cross": [4,0, 6,0, 6,4, 10,4, 10,6, 6,6, 6,10, 4,10, 4,6, 0,6, 0,4, 4,4,
  394. 4,0],
  395. "x": [0,0, 25,0, 50,35, 75,0, 100,0, 65,50, 100,100, 75,100, 50,65, 25,100, 0,100, 35,50, 0,0],
  396. "square": [0,0, 0,1, 1,1, 1,0, 0,0],
  397. "triangle": [0,10, 10,10, 5,0, 0,10]
  398. };