DrawFeature.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/Control.js
  7. * @requires OpenLayers/Feature/Vector.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.DrawFeature
  11. * The DrawFeature control draws point, line or polygon features on a vector
  12. * layer when active.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Control>
  16. */
  17. OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
  18. /**
  19. * Property: layer
  20. * {<OpenLayers.Layer.Vector>}
  21. */
  22. layer: null,
  23. /**
  24. * Property: callbacks
  25. * {Object} The functions that are sent to the handler for callback
  26. */
  27. callbacks: null,
  28. /**
  29. * APIProperty: events
  30. * {<OpenLayers.Events>} Events instance for listeners and triggering
  31. * control specific events.
  32. *
  33. * Register a listener for a particular event with the following syntax:
  34. * (code)
  35. * control.events.register(type, obj, listener);
  36. * (end)
  37. *
  38. * Supported event types (in addition to those from <OpenLayers.Control.events>):
  39. * featureadded - Triggered when a feature is added
  40. */
  41. /**
  42. * APIProperty: multi
  43. * {Boolean} Cast features to multi-part geometries before passing to the
  44. * layer. Default is false.
  45. */
  46. multi: false,
  47. /**
  48. * APIProperty: featureAdded
  49. * {Function} Called after each feature is added
  50. */
  51. featureAdded: function() {},
  52. /**
  53. * APIProperty: handlerOptions
  54. * {Object} Used to set non-default properties on the control's handler
  55. */
  56. /**
  57. * Constructor: OpenLayers.Control.DrawFeature
  58. *
  59. * Parameters:
  60. * layer - {<OpenLayers.Layer.Vector>}
  61. * handler - {<OpenLayers.Handler>}
  62. * options - {Object}
  63. */
  64. initialize: function(layer, handler, options) {
  65. OpenLayers.Control.prototype.initialize.apply(this, [options]);
  66. this.callbacks = OpenLayers.Util.extend(
  67. {
  68. done: this.drawFeature,
  69. modify: function(vertex, feature) {
  70. this.layer.events.triggerEvent(
  71. "sketchmodified", {vertex: vertex, feature: feature}
  72. );
  73. },
  74. create: function(vertex, feature) {
  75. this.layer.events.triggerEvent(
  76. "sketchstarted", {vertex: vertex, feature: feature}
  77. );
  78. }
  79. },
  80. this.callbacks
  81. );
  82. this.layer = layer;
  83. this.handlerOptions = this.handlerOptions || {};
  84. this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
  85. this.handlerOptions.layerOptions, {
  86. renderers: layer.renderers, rendererOptions: layer.rendererOptions
  87. }
  88. );
  89. if (!("multi" in this.handlerOptions)) {
  90. this.handlerOptions.multi = this.multi;
  91. }
  92. var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary;
  93. if(sketchStyle) {
  94. this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
  95. this.handlerOptions.layerOptions,
  96. {styleMap: new OpenLayers.StyleMap({"default": sketchStyle})}
  97. );
  98. }
  99. this.handler = new handler(this, this.callbacks, this.handlerOptions);
  100. },
  101. /**
  102. * Method: drawFeature
  103. */
  104. drawFeature: function(geometry) {
  105. var feature = new OpenLayers.Feature.Vector(geometry);
  106. var proceed = this.layer.events.triggerEvent(
  107. "sketchcomplete", {feature: feature}
  108. );
  109. if(proceed !== false) {
  110. feature.state = OpenLayers.State.INSERT;
  111. this.layer.addFeatures([feature]);
  112. this.featureAdded(feature);
  113. this.events.triggerEvent("featureadded",{feature : feature});
  114. }
  115. },
  116. /**
  117. * APIMethod: insertXY
  118. * Insert a point in the current sketch given x & y coordinates.
  119. *
  120. * Parameters:
  121. * x - {Number} The x-coordinate of the point.
  122. * y - {Number} The y-coordinate of the point.
  123. */
  124. insertXY: function(x, y) {
  125. if (this.handler && this.handler.line) {
  126. this.handler.insertXY(x, y);
  127. }
  128. },
  129. /**
  130. * APIMethod: insertDeltaXY
  131. * Insert a point given offsets from the previously inserted point.
  132. *
  133. * Parameters:
  134. * dx - {Number} The x-coordinate offset of the point.
  135. * dy - {Number} The y-coordinate offset of the point.
  136. */
  137. insertDeltaXY: function(dx, dy) {
  138. if (this.handler && this.handler.line) {
  139. this.handler.insertDeltaXY(dx, dy);
  140. }
  141. },
  142. /**
  143. * APIMethod: insertDirectionLength
  144. * Insert a point in the current sketch given a direction and a length.
  145. *
  146. * Parameters:
  147. * direction - {Number} Degrees clockwise from the positive x-axis.
  148. * length - {Number} Distance from the previously drawn point.
  149. */
  150. insertDirectionLength: function(direction, length) {
  151. if (this.handler && this.handler.line) {
  152. this.handler.insertDirectionLength(direction, length);
  153. }
  154. },
  155. /**
  156. * APIMethod: insertDeflectionLength
  157. * Insert a point in the current sketch given a deflection and a length.
  158. * The deflection should be degrees clockwise from the previously
  159. * digitized segment.
  160. *
  161. * Parameters:
  162. * deflection - {Number} Degrees clockwise from the previous segment.
  163. * length - {Number} Distance from the previously drawn point.
  164. */
  165. insertDeflectionLength: function(deflection, length) {
  166. if (this.handler && this.handler.line) {
  167. this.handler.insertDeflectionLength(deflection, length);
  168. }
  169. },
  170. /**
  171. * APIMethod: undo
  172. * Remove the most recently added point in the current sketch geometry.
  173. *
  174. * Returns:
  175. * {Boolean} An edit was undone.
  176. */
  177. undo: function() {
  178. return this.handler.undo && this.handler.undo();
  179. },
  180. /**
  181. * APIMethod: redo
  182. * Reinsert the most recently removed point resulting from an <undo> call.
  183. * The undo stack is deleted whenever a point is added by other means.
  184. *
  185. * Returns:
  186. * {Boolean} An edit was redone.
  187. */
  188. redo: function() {
  189. return this.handler.redo && this.handler.redo();
  190. },
  191. /**
  192. * APIMethod: finishSketch
  193. * Finishes the sketch without including the currently drawn point.
  194. * This method can be called to terminate drawing programmatically
  195. * instead of waiting for the user to end the sketch.
  196. */
  197. finishSketch: function() {
  198. this.handler.finishGeometry();
  199. },
  200. /**
  201. * APIMethod: cancel
  202. * Cancel the current sketch. This removes the current sketch and keeps
  203. * the drawing control active.
  204. */
  205. cancel: function() {
  206. this.handler.cancel();
  207. },
  208. CLASS_NAME: "OpenLayers.Control.DrawFeature"
  209. });