Renderer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt 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. * Constructor: OpenLayers.Renderer
  68. *
  69. * Parameters:
  70. * containerID - {<String>}
  71. * options - {Object} options for this renderer. See sublcasses for
  72. * supported options.
  73. */
  74. initialize: function(containerID, options) {
  75. this.container = OpenLayers.Util.getElement(containerID);
  76. OpenLayers.Util.extend(this, options);
  77. },
  78. /**
  79. * APIMethod: destroy
  80. */
  81. destroy: function() {
  82. this.container = null;
  83. this.extent = null;
  84. this.size = null;
  85. this.resolution = null;
  86. this.map = null;
  87. },
  88. /**
  89. * APIMethod: supported
  90. * This should be overridden by specific subclasses
  91. *
  92. * Returns:
  93. * {Boolean} Whether or not the browser supports the renderer class
  94. */
  95. supported: function() {
  96. return false;
  97. },
  98. /**
  99. * Method: setExtent
  100. * Set the visible part of the layer.
  101. *
  102. * Resolution has probably changed, so we nullify the resolution
  103. * cache (this.resolution) -- this way it will be re-computed when
  104. * next it is needed.
  105. * We nullify the resolution cache (this.resolution) if resolutionChanged
  106. * is set to true - this way it will be re-computed on the next
  107. * getResolution() request.
  108. *
  109. * Parameters:
  110. * extent - {<OpenLayers.Bounds>}
  111. * resolutionChanged - {Boolean}
  112. */
  113. setExtent: function(extent, resolutionChanged) {
  114. this.extent = extent.clone();
  115. if (resolutionChanged) {
  116. this.resolution = null;
  117. }
  118. },
  119. /**
  120. * Method: setSize
  121. * Sets the size of the drawing surface.
  122. *
  123. * Resolution has probably changed, so we nullify the resolution
  124. * cache (this.resolution) -- this way it will be re-computed when
  125. * next it is needed.
  126. *
  127. * Parameters:
  128. * size - {<OpenLayers.Size>}
  129. */
  130. setSize: function(size) {
  131. this.size = size.clone();
  132. this.resolution = null;
  133. },
  134. /**
  135. * Method: getResolution
  136. * Uses cached copy of resolution if available to minimize computing
  137. *
  138. * Returns:
  139. * The current map's resolution
  140. */
  141. getResolution: function() {
  142. this.resolution = this.resolution || this.map.getResolution();
  143. return this.resolution;
  144. },
  145. /**
  146. * Method: drawFeature
  147. * Draw the feature. The optional style argument can be used
  148. * to override the feature's own style. This method should only
  149. * be called from layer.drawFeature().
  150. *
  151. * Parameters:
  152. * feature - {<OpenLayers.Feature.Vector>}
  153. * style - {<Object>}
  154. *
  155. * Returns:
  156. * {Boolean} true if the feature has been drawn completely, false if not,
  157. * undefined if the feature had no geometry
  158. */
  159. drawFeature: function(feature, style) {
  160. if(style == null) {
  161. style = feature.style;
  162. }
  163. if (feature.geometry) {
  164. var bounds = feature.geometry.getBounds();
  165. if(bounds) {
  166. if (!bounds.intersectsBounds(this.extent)) {
  167. style = {display: "none"};
  168. }
  169. var rendered = this.drawGeometry(feature.geometry, style, feature.id);
  170. if(style.display != "none" && style.label && rendered !== false) {
  171. var location = feature.geometry.getCentroid();
  172. if(style.labelXOffset || style.labelYOffset) {
  173. var xOffset = isNaN(style.labelXOffset) ? 0 : style.labelXOffset;
  174. var yOffset = isNaN(style.labelYOffset) ? 0 : style.labelYOffset;
  175. var res = this.getResolution();
  176. location.move(xOffset*res, yOffset*res);
  177. }
  178. this.drawText(feature.id, style, location);
  179. } else {
  180. this.removeText(feature.id);
  181. }
  182. return rendered;
  183. }
  184. }
  185. },
  186. /**
  187. * Method: drawGeometry
  188. *
  189. * Draw a geometry. This should only be called from the renderer itself.
  190. * Use layer.drawFeature() from outside the renderer.
  191. * virtual function
  192. *
  193. * Parameters:
  194. * geometry - {<OpenLayers.Geometry>}
  195. * style - {Object}
  196. * featureId - {<String>}
  197. */
  198. drawGeometry: function(geometry, style, featureId) {},
  199. /**
  200. * Method: drawText
  201. * Function for drawing text labels.
  202. * This method is only called by the renderer itself.
  203. *
  204. * Parameters:
  205. * featureId - {String}
  206. * style -
  207. * location - {<OpenLayers.Geometry.Point>}
  208. */
  209. drawText: function(featureId, style, location) {},
  210. /**
  211. * Method: removeText
  212. * Function for removing text labels.
  213. * This method is only called by the renderer itself.
  214. *
  215. * Parameters:
  216. * featureId - {String}
  217. */
  218. removeText: function(featureId) {},
  219. /**
  220. * Method: clear
  221. * Clear all vectors from the renderer.
  222. * virtual function.
  223. */
  224. clear: function() {},
  225. /**
  226. * Method: getFeatureIdFromEvent
  227. * Returns a feature id from an event on the renderer.
  228. * How this happens is specific to the renderer. This should be
  229. * called from layer.getFeatureFromEvent().
  230. * Virtual function.
  231. *
  232. * Parameters:
  233. * evt - {<OpenLayers.Event>}
  234. *
  235. * Returns:
  236. * {String} A feature id or null.
  237. */
  238. getFeatureIdFromEvent: function(evt) {},
  239. /**
  240. * Method: eraseFeatures
  241. * This is called by the layer to erase features
  242. *
  243. * Parameters:
  244. * features - {Array(<OpenLayers.Feature.Vector>)}
  245. */
  246. eraseFeatures: function(features) {
  247. if(!(OpenLayers.Util.isArray(features))) {
  248. features = [features];
  249. }
  250. for(var i=0, len=features.length; i<len; ++i) {
  251. var feature = features[i];
  252. this.eraseGeometry(feature.geometry, feature.id);
  253. this.removeText(feature.id);
  254. }
  255. },
  256. /**
  257. * Method: eraseGeometry
  258. * Remove a geometry from the renderer (by id).
  259. * virtual function.
  260. *
  261. * Parameters:
  262. * geometry - {<OpenLayers.Geometry>}
  263. * featureId - {String}
  264. */
  265. eraseGeometry: function(geometry, featureId) {},
  266. /**
  267. * Method: moveRoot
  268. * moves this renderer's root to a (different) renderer.
  269. * To be implemented by subclasses that require a common renderer root for
  270. * feature selection.
  271. *
  272. * Parameters:
  273. * renderer - {<OpenLayers.Renderer>} target renderer for the moved root
  274. */
  275. moveRoot: function(renderer) {},
  276. /**
  277. * Method: getRenderLayerId
  278. * Gets the layer that this renderer's output appears on. If moveRoot was
  279. * used, this will be different from the id of the layer containing the
  280. * features rendered by this renderer.
  281. *
  282. * Returns:
  283. * {String} the id of the output layer.
  284. */
  285. getRenderLayerId: function() {
  286. return this.container.id;
  287. },
  288. /**
  289. * Method: applyDefaultSymbolizer
  290. *
  291. * Parameters:
  292. * symbolizer - {Object}
  293. *
  294. * Returns:
  295. * {Object}
  296. */
  297. applyDefaultSymbolizer: function(symbolizer) {
  298. var result = OpenLayers.Util.extend({},
  299. OpenLayers.Renderer.defaultSymbolizer);
  300. if(symbolizer.stroke === false) {
  301. delete result.strokeWidth;
  302. delete result.strokeColor;
  303. }
  304. if(symbolizer.fill === false) {
  305. delete result.fillColor;
  306. }
  307. OpenLayers.Util.extend(result, symbolizer);
  308. return result;
  309. },
  310. CLASS_NAME: "OpenLayers.Renderer"
  311. });
  312. /**
  313. * Constant: OpenLayers.Renderer.defaultSymbolizer
  314. * {Object} Properties from this symbolizer will be applied to symbolizers
  315. * with missing properties. This can also be used to set a global
  316. * symbolizer default in OpenLayers. To be SLD 1.x compliant, add the
  317. * following code before rendering any vector features:
  318. * (code)
  319. * OpenLayers.Renderer.defaultSymbolizer = {
  320. * fillColor: "#808080",
  321. * fillOpacity: 1,
  322. * strokeColor: "#000000",
  323. * strokeOpacity: 1,
  324. * strokeWidth: 1,
  325. * pointRadius: 3,
  326. * graphicName: "square"
  327. * };
  328. * (end)
  329. */
  330. OpenLayers.Renderer.defaultSymbolizer = {
  331. fillColor: "#000000",
  332. strokeColor: "#000000",
  333. strokeWidth: 2,
  334. fillOpacity: 1,
  335. strokeOpacity: 1,
  336. pointRadius: 0
  337. };