Vector.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. // TRASH THIS
  6. OpenLayers.State = {
  7. /** states */
  8. UNKNOWN: 'Unknown',
  9. INSERT: 'Insert',
  10. UPDATE: 'Update',
  11. DELETE: 'Delete'
  12. };
  13. /**
  14. * @requires OpenLayers/Feature.js
  15. * @requires OpenLayers/Util.js
  16. */
  17. /**
  18. * Class: OpenLayers.Feature.Vector
  19. * Vector features use the OpenLayers.Geometry classes as geometry description.
  20. * They have an 'attributes' property, which is the data object, and a 'style'
  21. * property, the default values of which are defined in the
  22. * <OpenLayers.Feature.Vector.style> objects.
  23. *
  24. * Inherits from:
  25. * - <OpenLayers.Feature>
  26. */
  27. OpenLayers.Feature.Vector = OpenLayers.Class(OpenLayers.Feature, {
  28. /**
  29. * Property: fid
  30. * {String}
  31. */
  32. fid: null,
  33. /**
  34. * APIProperty: geometry
  35. * {<OpenLayers.Geometry>}
  36. */
  37. geometry: null,
  38. /**
  39. * APIProperty: attributes
  40. * {Object} This object holds arbitrary, serializable properties that
  41. * describe the feature.
  42. */
  43. attributes: null,
  44. /**
  45. * Property: bounds
  46. * {<OpenLayers.Bounds>} The box bounding that feature's geometry, that
  47. * property can be set by an <OpenLayers.Format> object when
  48. * deserializing the feature, so in most cases it represents an
  49. * information set by the server.
  50. */
  51. bounds: null,
  52. /**
  53. * Property: state
  54. * {String}
  55. */
  56. state: null,
  57. /**
  58. * APIProperty: style
  59. * {Object}
  60. */
  61. style: null,
  62. /**
  63. * APIProperty: url
  64. * {String} If this property is set it will be taken into account by
  65. * {<OpenLayers.HTTP>} when upadting or deleting the feature.
  66. */
  67. url: null,
  68. /**
  69. * Property: renderIntent
  70. * {String} rendering intent currently being used
  71. */
  72. renderIntent: "default",
  73. /**
  74. * APIProperty: modified
  75. * {Object} An object with the originals of the geometry and attributes of
  76. * the feature, if they were changed. Currently this property is only read
  77. * by <OpenLayers.Format.WFST.v1>, and written by
  78. * <OpenLayers.Control.ModifyFeature>, which sets the geometry property.
  79. * Applications can set the originals of modified attributes in the
  80. * attributes property. Note that applications have to check if this
  81. * object and the attributes property is already created before using it.
  82. * After a change made with ModifyFeature, this object could look like
  83. *
  84. * (code)
  85. * {
  86. * geometry: >Object
  87. * }
  88. * (end)
  89. *
  90. * When an application has made changes to feature attributes, it could
  91. * have set the attributes to something like this:
  92. *
  93. * (code)
  94. * {
  95. * attributes: {
  96. * myAttribute: "original"
  97. * }
  98. * }
  99. * (end)
  100. *
  101. * Note that <OpenLayers.Format.WFST.v1> only checks for truthy values in
  102. * *modified.geometry* and the attribute names in *modified.attributes*,
  103. * but it is recommended to set the original values (and not just true) as
  104. * attribute value, so applications could use this information to undo
  105. * changes.
  106. */
  107. modified: null,
  108. /**
  109. * Constructor: OpenLayers.Feature.Vector
  110. * Create a vector feature.
  111. *
  112. * Parameters:
  113. * geometry - {<OpenLayers.Geometry>} The geometry that this feature
  114. * represents.
  115. * attributes - {Object} An optional object that will be mapped to the
  116. * <attributes> property.
  117. * style - {Object} An optional style object.
  118. */
  119. initialize: function(geometry, attributes, style) {
  120. OpenLayers.Feature.prototype.initialize.apply(this,
  121. [null, null, attributes]);
  122. this.lonlat = null;
  123. this.geometry = geometry ? geometry : null;
  124. this.state = null;
  125. this.attributes = {};
  126. if (attributes) {
  127. this.attributes = OpenLayers.Util.extend(this.attributes,
  128. attributes);
  129. }
  130. this.style = style ? style : null;
  131. },
  132. /**
  133. * Method: destroy
  134. * nullify references to prevent circular references and memory leaks
  135. */
  136. destroy: function() {
  137. if (this.layer) {
  138. this.layer.removeFeatures(this);
  139. this.layer = null;
  140. }
  141. this.geometry = null;
  142. this.modified = null;
  143. OpenLayers.Feature.prototype.destroy.apply(this, arguments);
  144. },
  145. /**
  146. * Method: clone
  147. * Create a clone of this vector feature. Does not set any non-standard
  148. * properties.
  149. *
  150. * Returns:
  151. * {<OpenLayers.Feature.Vector>} An exact clone of this vector feature.
  152. */
  153. clone: function () {
  154. return new OpenLayers.Feature.Vector(
  155. this.geometry ? this.geometry.clone() : null,
  156. this.attributes,
  157. this.style);
  158. },
  159. /**
  160. * Method: onScreen
  161. * Determine whether the feature is within the map viewport. This method
  162. * tests for an intersection between the geometry and the viewport
  163. * bounds. If a more effecient but less precise geometry bounds
  164. * intersection is desired, call the method with the boundsOnly
  165. * parameter true.
  166. *
  167. * Parameters:
  168. * boundsOnly - {Boolean} Only test whether a feature's bounds intersects
  169. * the viewport bounds. Default is false. If false, the feature's
  170. * geometry must intersect the viewport for onScreen to return true.
  171. *
  172. * Returns:
  173. * {Boolean} The feature is currently visible on screen (optionally
  174. * based on its bounds if boundsOnly is true).
  175. */
  176. onScreen:function(boundsOnly) {
  177. var onScreen = false;
  178. if(this.layer && this.layer.map) {
  179. var screenBounds = this.layer.map.getExtent();
  180. if(boundsOnly) {
  181. var featureBounds = this.geometry.getBounds();
  182. onScreen = screenBounds.intersectsBounds(featureBounds);
  183. } else {
  184. var screenPoly = screenBounds.toGeometry();
  185. onScreen = screenPoly.intersects(this.geometry);
  186. }
  187. }
  188. return onScreen;
  189. },
  190. /**
  191. * Method: getVisibility
  192. * Determine whether the feature is displayed or not. It may not displayed
  193. * because:
  194. * - its style display property is set to 'none',
  195. * - it doesn't belong to any layer,
  196. * - the styleMap creates a symbolizer with display property set to 'none'
  197. * for it,
  198. * - the layer which it belongs to is not visible.
  199. *
  200. * Returns:
  201. * {Boolean} The feature is currently displayed.
  202. */
  203. getVisibility: function() {
  204. return !(this.style && this.style.display == 'none' ||
  205. !this.layer ||
  206. this.layer && this.layer.styleMap &&
  207. this.layer.styleMap.createSymbolizer(this, this.renderIntent).display == 'none' ||
  208. this.layer && !this.layer.getVisibility());
  209. },
  210. /**
  211. * Method: createMarker
  212. * HACK - we need to decide if all vector features should be able to
  213. * create markers
  214. *
  215. * Returns:
  216. * {<OpenLayers.Marker>} For now just returns null
  217. */
  218. createMarker: function() {
  219. return null;
  220. },
  221. /**
  222. * Method: destroyMarker
  223. * HACK - we need to decide if all vector features should be able to
  224. * delete markers
  225. *
  226. * If user overrides the createMarker() function, s/he should be able
  227. * to also specify an alternative function for destroying it
  228. */
  229. destroyMarker: function() {
  230. // pass
  231. },
  232. /**
  233. * Method: createPopup
  234. * HACK - we need to decide if all vector features should be able to
  235. * create popups
  236. *
  237. * Returns:
  238. * {<OpenLayers.Popup>} For now just returns null
  239. */
  240. createPopup: function() {
  241. return null;
  242. },
  243. /**
  244. * Method: atPoint
  245. * Determins whether the feature intersects with the specified location.
  246. *
  247. * Parameters:
  248. * lonlat - {<OpenLayers.LonLat>|Object} OpenLayers.LonLat or an
  249. * object with a 'lon' and 'lat' properties.
  250. * toleranceLon - {float} Optional tolerance in Geometric Coords
  251. * toleranceLat - {float} Optional tolerance in Geographic Coords
  252. *
  253. * Returns:
  254. * {Boolean} Whether or not the feature is at the specified location
  255. */
  256. atPoint: function(lonlat, toleranceLon, toleranceLat) {
  257. var atPoint = false;
  258. if(this.geometry) {
  259. atPoint = this.geometry.atPoint(lonlat, toleranceLon,
  260. toleranceLat);
  261. }
  262. return atPoint;
  263. },
  264. /**
  265. * Method: destroyPopup
  266. * HACK - we need to decide if all vector features should be able to
  267. * delete popups
  268. */
  269. destroyPopup: function() {
  270. // pass
  271. },
  272. /**
  273. * Method: move
  274. * Moves the feature and redraws it at its new location
  275. *
  276. * Parameters:
  277. * location - {<OpenLayers.LonLat> or <OpenLayers.Pixel>} the
  278. * location to which to move the feature.
  279. */
  280. move: function(location) {
  281. if(!this.layer || !this.geometry.move){
  282. //do nothing if no layer or immoveable geometry
  283. return undefined;
  284. }
  285. var pixel;
  286. if (location.CLASS_NAME == "OpenLayers.LonLat") {
  287. pixel = this.layer.getViewPortPxFromLonLat(location);
  288. } else {
  289. pixel = location;
  290. }
  291. var lastPixel = this.layer.getViewPortPxFromLonLat(this.geometry.getBounds().getCenterLonLat());
  292. var res = this.layer.map.getResolution();
  293. this.geometry.move(res * (pixel.x - lastPixel.x),
  294. res * (lastPixel.y - pixel.y));
  295. this.layer.drawFeature(this);
  296. return lastPixel;
  297. },
  298. /**
  299. * Method: toState
  300. * Sets the new state
  301. *
  302. * Parameters:
  303. * state - {String}
  304. */
  305. toState: function(state) {
  306. if (state == OpenLayers.State.UPDATE) {
  307. switch (this.state) {
  308. case OpenLayers.State.UNKNOWN:
  309. case OpenLayers.State.DELETE:
  310. this.state = state;
  311. break;
  312. case OpenLayers.State.UPDATE:
  313. case OpenLayers.State.INSERT:
  314. break;
  315. }
  316. } else if (state == OpenLayers.State.INSERT) {
  317. switch (this.state) {
  318. case OpenLayers.State.UNKNOWN:
  319. break;
  320. default:
  321. this.state = state;
  322. break;
  323. }
  324. } else if (state == OpenLayers.State.DELETE) {
  325. switch (this.state) {
  326. case OpenLayers.State.INSERT:
  327. // the feature should be destroyed
  328. break;
  329. case OpenLayers.State.DELETE:
  330. break;
  331. case OpenLayers.State.UNKNOWN:
  332. case OpenLayers.State.UPDATE:
  333. this.state = state;
  334. break;
  335. }
  336. } else if (state == OpenLayers.State.UNKNOWN) {
  337. this.state = state;
  338. }
  339. },
  340. CLASS_NAME: "OpenLayers.Feature.Vector"
  341. });
  342. /**
  343. * Constant: OpenLayers.Feature.Vector.style
  344. * OpenLayers features can have a number of style attributes. The 'default'
  345. * style will typically be used if no other style is specified. These
  346. * styles correspond for the most part, to the styling properties defined
  347. * by the SVG standard.
  348. * Information on fill properties: http://www.w3.org/TR/SVG/painting.html#FillProperties
  349. * Information on stroke properties: http://www.w3.org/TR/SVG/painting.html#StrokeProperties
  350. *
  351. * Symbolizer properties:
  352. * fill - {Boolean} Set to false if no fill is desired.
  353. * fillColor - {String} Hex fill color. Default is "#ee9900".
  354. * fillOpacity - {Number} Fill opacity (0-1). Default is 0.4
  355. * stroke - {Boolean} Set to false if no stroke is desired.
  356. * strokeColor - {String} Hex stroke color. Default is "#ee9900".
  357. * strokeOpacity - {Number} Stroke opacity (0-1). Default is 1.
  358. * strokeWidth - {Number} Pixel stroke width. Default is 1.
  359. * strokeLinecap - {String} Stroke cap type. Default is "round". [butt | round | square]
  360. * strokeDashstyle - {String} Stroke dash style. Default is "solid". [dot | dash | dashdot | longdash | longdashdot | solid]
  361. * graphic - {Boolean} Set to false if no graphic is desired.
  362. * pointRadius - {Number} Pixel point radius. Default is 6.
  363. * pointerEvents - {String} Default is "visiblePainted".
  364. * cursor - {String} Default is "".
  365. * externalGraphic - {String} Url to an external graphic that will be used for rendering points.
  366. * graphicWidth - {Number} Pixel width for sizing an external graphic.
  367. * graphicHeight - {Number} Pixel height for sizing an external graphic.
  368. * graphicOpacity - {Number} Opacity (0-1) for an external graphic.
  369. * graphicXOffset - {Number} Pixel offset along the positive x axis for displacing an external graphic.
  370. * graphicYOffset - {Number} Pixel offset along the positive y axis for displacing an external graphic.
  371. * rotation - {Number} For point symbolizers, this is the rotation of a graphic in the clockwise direction about its center point (or any point off center as specified by graphicXOffset and graphicYOffset).
  372. * graphicZIndex - {Number} The integer z-index value to use in rendering.
  373. * graphicName - {String} Named graphic to use when rendering points. Supported values include "circle" (default),
  374. * "square", "star", "x", "cross", "triangle".
  375. * graphicTitle - {String} Tooltip when hovering over a feature. *deprecated*, use title instead
  376. * title - {String} Tooltip when hovering over a feature. Not supported by the canvas renderer.
  377. * backgroundGraphic - {String} Url to a graphic to be used as the background under an externalGraphic.
  378. * backgroundGraphicZIndex - {Number} The integer z-index value to use in rendering the background graphic.
  379. * backgroundXOffset - {Number} The x offset (in pixels) for the background graphic.
  380. * backgroundYOffset - {Number} The y offset (in pixels) for the background graphic.
  381. * backgroundHeight - {Number} The height of the background graphic. If not provided, the graphicHeight will be used.
  382. * backgroundWidth - {Number} The width of the background width. If not provided, the graphicWidth will be used.
  383. * label - {String} The text for an optional label. For browsers that use the canvas renderer, this requires either
  384. * fillText or mozDrawText to be available.
  385. * labelAlign - {String} Label alignment. This specifies the insertion point relative to the text. It is a string
  386. * composed of two characters. The first character is for the horizontal alignment, the second for the vertical
  387. * alignment. Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. Valid values for vertical
  388. * alignment: "t"=top, "m"=middle, "b"=bottom. Example values: "lt", "cm", "rb". Default is "cm".
  389. * labelXOffset - {Number} Pixel offset along the positive x axis for displacing the label. Not supported by the canvas renderer.
  390. * labelYOffset - {Number} Pixel offset along the positive y axis for displacing the label. Not supported by the canvas renderer.
  391. * labelSelect - {Boolean} If set to true, labels will be selectable using SelectFeature or similar controls.
  392. * Default is false.
  393. * labelOutlineColor - {String} The color of the label outline. Default is 'white'. Only supported by the canvas & SVG renderers.
  394. * labelOutlineWidth - {Number} The width of the label outline. Default is 3, set to 0 or null to disable. Only supported by the SVG renderers.
  395. * labelOutlineOpacity - {Number} The opacity (0-1) of the label outline. Default is fontOpacity. Only supported by the canvas & SVG renderers.
  396. * fontColor - {String} The font color for the label, to be provided like CSS.
  397. * fontOpacity - {Number} Opacity (0-1) for the label
  398. * fontFamily - {String} The font family for the label, to be provided like in CSS.
  399. * fontSize - {String} The font size for the label, to be provided like in CSS.
  400. * fontStyle - {String} The font style for the label, to be provided like in CSS.
  401. * fontWeight - {String} The font weight for the label, to be provided like in CSS.
  402. * display - {String} Symbolizers will have no effect if display is set to "none". All other values have no effect.
  403. */
  404. OpenLayers.Feature.Vector.style = {
  405. 'default': {
  406. fillColor: "#ee9900",
  407. fillOpacity: 0.4,
  408. hoverFillColor: "white",
  409. hoverFillOpacity: 0.8,
  410. strokeColor: "#ee9900",
  411. strokeOpacity: 1,
  412. strokeWidth: 1,
  413. strokeLinecap: "round",
  414. strokeDashstyle: "solid",
  415. hoverStrokeColor: "red",
  416. hoverStrokeOpacity: 1,
  417. hoverStrokeWidth: 0.2,
  418. pointRadius: 6,
  419. hoverPointRadius: 1,
  420. hoverPointUnit: "%",
  421. pointerEvents: "visiblePainted",
  422. cursor: "inherit",
  423. fontColor: "#000000",
  424. labelAlign: "cm",
  425. labelOutlineColor: "white",
  426. labelOutlineWidth: 3
  427. },
  428. 'select': {
  429. fillColor: "blue",
  430. fillOpacity: 0.4,
  431. hoverFillColor: "white",
  432. hoverFillOpacity: 0.8,
  433. strokeColor: "blue",
  434. strokeOpacity: 1,
  435. strokeWidth: 2,
  436. strokeLinecap: "round",
  437. strokeDashstyle: "solid",
  438. hoverStrokeColor: "red",
  439. hoverStrokeOpacity: 1,
  440. hoverStrokeWidth: 0.2,
  441. pointRadius: 6,
  442. hoverPointRadius: 1,
  443. hoverPointUnit: "%",
  444. pointerEvents: "visiblePainted",
  445. cursor: "pointer",
  446. fontColor: "#000000",
  447. labelAlign: "cm",
  448. labelOutlineColor: "white",
  449. labelOutlineWidth: 3
  450. },
  451. 'temporary': {
  452. fillColor: "#66cccc",
  453. fillOpacity: 0.2,
  454. hoverFillColor: "white",
  455. hoverFillOpacity: 0.8,
  456. strokeColor: "#66cccc",
  457. strokeOpacity: 1,
  458. strokeLinecap: "round",
  459. strokeWidth: 2,
  460. strokeDashstyle: "solid",
  461. hoverStrokeColor: "red",
  462. hoverStrokeOpacity: 1,
  463. hoverStrokeWidth: 0.2,
  464. pointRadius: 6,
  465. hoverPointRadius: 1,
  466. hoverPointUnit: "%",
  467. pointerEvents: "visiblePainted",
  468. cursor: "inherit",
  469. fontColor: "#000000",
  470. labelAlign: "cm",
  471. labelOutlineColor: "white",
  472. labelOutlineWidth: 3
  473. },
  474. 'delete': {
  475. display: "none"
  476. }
  477. };