Layer.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. * @requires OpenLayers/Map.js
  8. * @requires OpenLayers/Projection.js
  9. */
  10. /**
  11. * Class: OpenLayers.Layer
  12. */
  13. OpenLayers.Layer = OpenLayers.Class({
  14. /**
  15. * APIProperty: id
  16. * {String}
  17. */
  18. id: null,
  19. /**
  20. * APIProperty: name
  21. * {String}
  22. */
  23. name: null,
  24. /**
  25. * APIProperty: div
  26. * {DOMElement}
  27. */
  28. div: null,
  29. /**
  30. * APIProperty: opacity
  31. * {Float} The layer's opacity. Float number between 0.0 and 1.0. Default
  32. * is 1.
  33. */
  34. opacity: 1,
  35. /**
  36. * APIProperty: alwaysInRange
  37. * {Boolean} If a layer's display should not be scale-based, this should
  38. * be set to true. This will cause the layer, as an overlay, to always
  39. * be 'active', by always returning true from the calculateInRange()
  40. * function.
  41. *
  42. * If not explicitly specified for a layer, its value will be
  43. * determined on startup in initResolutions() based on whether or not
  44. * any scale-specific properties have been set as options on the
  45. * layer. If no scale-specific options have been set on the layer, we
  46. * assume that it should always be in range.
  47. *
  48. * See #987 for more info.
  49. */
  50. alwaysInRange: null,
  51. /**
  52. * Constant: RESOLUTION_PROPERTIES
  53. * {Array} The properties that are used for calculating resolutions
  54. * information.
  55. */
  56. RESOLUTION_PROPERTIES: [
  57. 'scales', 'resolutions',
  58. 'maxScale', 'minScale',
  59. 'maxResolution', 'minResolution',
  60. 'numZoomLevels', 'maxZoomLevel'
  61. ],
  62. /**
  63. * APIProperty: events
  64. * {<OpenLayers.Events>}
  65. *
  66. * Register a listener for a particular event with the following syntax:
  67. * (code)
  68. * layer.events.register(type, obj, listener);
  69. * (end)
  70. *
  71. * Listeners will be called with a reference to an event object. The
  72. * properties of this event depends on exactly what happened.
  73. *
  74. * All event objects have at least the following properties:
  75. * object - {Object} A reference to layer.events.object.
  76. * element - {DOMElement} A reference to layer.events.element.
  77. *
  78. * Supported map event types:
  79. * loadstart - Triggered when layer loading starts. When using a Vector
  80. * layer with a Fixed or BBOX strategy, the event object includes
  81. * a *filter* property holding the OpenLayers.Filter used when
  82. * calling read on the protocol.
  83. * loadend - Triggered when layer loading ends. When using a Vector layer
  84. * with a Fixed or BBOX strategy, the event object includes a
  85. * *response* property holding an OpenLayers.Protocol.Response object.
  86. * visibilitychanged - Triggered when the layer's visibility property is
  87. * changed, e.g. by turning the layer on or off in the layer switcher.
  88. * Note that the actual visibility of the layer can also change if it
  89. * gets out of range (see <calculateInRange>). If you also want to catch
  90. * these cases, register for the map's 'changelayer' event instead.
  91. * move - Triggered when layer moves (triggered with every mousemove
  92. * during a drag).
  93. * moveend - Triggered when layer is done moving, object passed as
  94. * argument has a zoomChanged boolean property which tells that the
  95. * zoom has changed.
  96. * added - Triggered after the layer is added to a map. Listeners will
  97. * receive an object with a *map* property referencing the map and a
  98. * *layer* property referencing the layer.
  99. * removed - Triggered after the layer is removed from the map. Listeners
  100. * will receive an object with a *map* property referencing the map and
  101. * a *layer* property referencing the layer.
  102. */
  103. events: null,
  104. /**
  105. * APIProperty: map
  106. * {<OpenLayers.Map>} This variable is set when the layer is added to
  107. * the map, via the accessor function setMap().
  108. */
  109. map: null,
  110. /**
  111. * APIProperty: isBaseLayer
  112. * {Boolean} Whether or not the layer is a base layer. This should be set
  113. * individually by all subclasses. Default is false
  114. */
  115. isBaseLayer: false,
  116. /**
  117. * Property: alpha
  118. * {Boolean} The layer's images have an alpha channel. Default is false.
  119. */
  120. alpha: false,
  121. /**
  122. * APIProperty: displayInLayerSwitcher
  123. * {Boolean} Display the layer's name in the layer switcher. Default is
  124. * true.
  125. */
  126. displayInLayerSwitcher: true,
  127. /**
  128. * APIProperty: visibility
  129. * {Boolean} The layer should be displayed in the map. Default is true.
  130. */
  131. visibility: true,
  132. /**
  133. * APIProperty: attribution
  134. * {String} Attribution string, displayed when an
  135. * <OpenLayers.Control.Attribution> has been added to the map.
  136. */
  137. attribution: null,
  138. /**
  139. * Property: inRange
  140. * {Boolean} The current map resolution is within the layer's min/max
  141. * range. This is set in <OpenLayers.Map.setCenter> whenever the zoom
  142. * changes.
  143. */
  144. inRange: false,
  145. /**
  146. * Propery: imageSize
  147. * {<OpenLayers.Size>} For layers with a gutter, the image is larger than
  148. * the tile by twice the gutter in each dimension.
  149. */
  150. imageSize: null,
  151. // OPTIONS
  152. /**
  153. * Property: options
  154. * {Object} An optional object whose properties will be set on the layer.
  155. * Any of the layer properties can be set as a property of the options
  156. * object and sent to the constructor when the layer is created.
  157. */
  158. options: null,
  159. /**
  160. * APIProperty: eventListeners
  161. * {Object} If set as an option at construction, the eventListeners
  162. * object will be registered with <OpenLayers.Events.on>. Object
  163. * structure must be a listeners object as shown in the example for
  164. * the events.on method.
  165. */
  166. eventListeners: null,
  167. /**
  168. * APIProperty: gutter
  169. * {Integer} Determines the width (in pixels) of the gutter around image
  170. * tiles to ignore. By setting this property to a non-zero value,
  171. * images will be requested that are wider and taller than the tile
  172. * size by a value of 2 x gutter. This allows artifacts of rendering
  173. * at tile edges to be ignored. Set a gutter value that is equal to
  174. * half the size of the widest symbol that needs to be displayed.
  175. * Defaults to zero. Non-tiled layers always have zero gutter.
  176. */
  177. gutter: 0,
  178. /**
  179. * APIProperty: projection
  180. * {<OpenLayers.Projection>} or {<String>} Specifies the projection of the layer.
  181. * Can be set in the layer options. If not specified in the layer options,
  182. * it is set to the default projection specified in the map,
  183. * when the layer is added to the map.
  184. * Projection along with default maxExtent and resolutions
  185. * are set automatically with commercial baselayers in EPSG:3857,
  186. * such as Google, Bing and OpenStreetMap, and do not need to be specified.
  187. * Otherwise, if specifying projection, also set maxExtent,
  188. * maxResolution or resolutions as appropriate.
  189. * When using vector layers with strategies, layer projection should be set
  190. * to the projection of the source data if that is different from the map default.
  191. *
  192. * Can be either a string or an <OpenLayers.Projection> object;
  193. * if a string is passed, will be converted to an object when
  194. * the layer is added to the map.
  195. *
  196. */
  197. projection: null,
  198. /**
  199. * APIProperty: units
  200. * {String} The layer map units. Defaults to null. Possible values
  201. * are 'degrees' (or 'dd'), 'm', 'ft', 'km', 'mi', 'inches'.
  202. * Normally taken from the projection.
  203. * Only required if both map and layers do not define a projection,
  204. * or if they define a projection which does not define units.
  205. */
  206. units: null,
  207. /**
  208. * APIProperty: scales
  209. * {Array} An array of map scales in descending order. The values in the
  210. * array correspond to the map scale denominator. Note that these
  211. * values only make sense if the display (monitor) resolution of the
  212. * client is correctly guessed by whomever is configuring the
  213. * application. In addition, the units property must also be set.
  214. * Use <resolutions> instead wherever possible.
  215. */
  216. scales: null,
  217. /**
  218. * APIProperty: resolutions
  219. * {Array} A list of map resolutions (map units per pixel) in descending
  220. * order. If this is not set in the layer constructor, it will be set
  221. * based on other resolution related properties (maxExtent,
  222. * maxResolution, maxScale, etc.).
  223. */
  224. resolutions: null,
  225. /**
  226. * APIProperty: maxExtent
  227. * {<OpenLayers.Bounds>|Array} If provided as an array, the array
  228. * should consist of four values (left, bottom, right, top).
  229. * The maximum extent for the layer. Defaults to null.
  230. *
  231. * The center of these bounds will not stray outside
  232. * of the viewport extent during panning. In addition, if
  233. * <displayOutsideMaxExtent> is set to false, data will not be
  234. * requested that falls completely outside of these bounds.
  235. */
  236. maxExtent: null,
  237. /**
  238. * APIProperty: minExtent
  239. * {<OpenLayers.Bounds>|Array} If provided as an array, the array
  240. * should consist of four values (left, bottom, right, top).
  241. * The minimum extent for the layer. Defaults to null.
  242. */
  243. minExtent: null,
  244. /**
  245. * APIProperty: maxResolution
  246. * {Float} Default max is 360 deg / 256 px, which corresponds to
  247. * zoom level 0 on gmaps. Specify a different value in the layer
  248. * options if you are not using the default <OpenLayers.Map.tileSize>
  249. * and displaying the whole world.
  250. */
  251. maxResolution: null,
  252. /**
  253. * APIProperty: minResolution
  254. * {Float}
  255. */
  256. minResolution: null,
  257. /**
  258. * APIProperty: numZoomLevels
  259. * {Integer}
  260. */
  261. numZoomLevels: null,
  262. /**
  263. * APIProperty: minScale
  264. * {Float}
  265. */
  266. minScale: null,
  267. /**
  268. * APIProperty: maxScale
  269. * {Float}
  270. */
  271. maxScale: null,
  272. /**
  273. * APIProperty: displayOutsideMaxExtent
  274. * {Boolean} Request map tiles that are completely outside of the max
  275. * extent for this layer. Defaults to false.
  276. */
  277. displayOutsideMaxExtent: false,
  278. /**
  279. * APIProperty: wrapDateLine
  280. * {Boolean} Wraps the world at the international dateline, so the map can
  281. * be panned infinitely in longitudinal direction. Only use this on the
  282. * base layer, and only if the layer's maxExtent equals the world bounds.
  283. * #487 for more info.
  284. */
  285. wrapDateLine: false,
  286. /**
  287. * Property: metadata
  288. * {Object} This object can be used to store additional information on a
  289. * layer object.
  290. */
  291. metadata: null,
  292. /**
  293. * Constructor: OpenLayers.Layer
  294. *
  295. * Parameters:
  296. * name - {String} The layer name
  297. * options - {Object} Hashtable of extra options to tag onto the layer
  298. */
  299. initialize: function(name, options) {
  300. this.metadata = {};
  301. options = OpenLayers.Util.extend({}, options);
  302. // make sure we respect alwaysInRange if set on the prototype
  303. if (this.alwaysInRange != null) {
  304. options.alwaysInRange = this.alwaysInRange;
  305. }
  306. this.addOptions(options);
  307. this.name = name;
  308. if (this.id == null) {
  309. this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
  310. this.div = OpenLayers.Util.createDiv(this.id);
  311. this.div.style.width = "100%";
  312. this.div.style.height = "100%";
  313. this.div.dir = "ltr";
  314. this.events = new OpenLayers.Events(this, this.div);
  315. if(this.eventListeners instanceof Object) {
  316. this.events.on(this.eventListeners);
  317. }
  318. }
  319. },
  320. /**
  321. * Method: destroy
  322. * Destroy is a destructor: this is to alleviate cyclic references which
  323. * the Javascript garbage cleaner can not take care of on its own.
  324. *
  325. * Parameters:
  326. * setNewBaseLayer - {Boolean} Set a new base layer when this layer has
  327. * been destroyed. Default is true.
  328. */
  329. destroy: function(setNewBaseLayer) {
  330. if (setNewBaseLayer == null) {
  331. setNewBaseLayer = true;
  332. }
  333. if (this.map != null) {
  334. this.map.removeLayer(this, setNewBaseLayer);
  335. }
  336. this.projection = null;
  337. this.map = null;
  338. this.name = null;
  339. this.div = null;
  340. this.options = null;
  341. if (this.events) {
  342. if(this.eventListeners) {
  343. this.events.un(this.eventListeners);
  344. }
  345. this.events.destroy();
  346. }
  347. this.eventListeners = null;
  348. this.events = null;
  349. },
  350. /**
  351. * Method: clone
  352. *
  353. * Parameters:
  354. * obj - {<OpenLayers.Layer>} The layer to be cloned
  355. *
  356. * Returns:
  357. * {<OpenLayers.Layer>} An exact clone of this <OpenLayers.Layer>
  358. */
  359. clone: function (obj) {
  360. if (obj == null) {
  361. obj = new OpenLayers.Layer(this.name, this.getOptions());
  362. }
  363. // catch any randomly tagged-on properties
  364. OpenLayers.Util.applyDefaults(obj, this);
  365. // a cloned layer should never have its map property set
  366. // because it has not been added to a map yet.
  367. obj.map = null;
  368. return obj;
  369. },
  370. /**
  371. * Method: getOptions
  372. * Extracts an object from the layer with the properties that were set as
  373. * options, but updates them with the values currently set on the
  374. * instance.
  375. *
  376. * Returns:
  377. * {Object} the <options> of the layer, representing the current state.
  378. */
  379. getOptions: function() {
  380. var options = {};
  381. for(var o in this.options) {
  382. options[o] = this[o];
  383. }
  384. return options;
  385. },
  386. /**
  387. * APIMethod: setName
  388. * Sets the new layer name for this layer. Can trigger a changelayer event
  389. * on the map.
  390. *
  391. * Parameters:
  392. * newName - {String} The new name.
  393. */
  394. setName: function(newName) {
  395. if (newName != this.name) {
  396. this.name = newName;
  397. if (this.map != null) {
  398. this.map.events.triggerEvent("changelayer", {
  399. layer: this,
  400. property: "name"
  401. });
  402. }
  403. }
  404. },
  405. /**
  406. * APIMethod: addOptions
  407. *
  408. * Parameters:
  409. * newOptions - {Object}
  410. * reinitialize - {Boolean} If set to true, and if resolution options of the
  411. * current baseLayer were changed, the map will be recentered to make
  412. * sure that it is displayed with a valid resolution, and a
  413. * changebaselayer event will be triggered.
  414. */
  415. addOptions: function (newOptions, reinitialize) {
  416. if (this.options == null) {
  417. this.options = {};
  418. }
  419. if (newOptions) {
  420. // make sure this.projection references a projection object
  421. if(typeof newOptions.projection == "string") {
  422. newOptions.projection = new OpenLayers.Projection(newOptions.projection);
  423. }
  424. if (newOptions.projection) {
  425. // get maxResolution, units and maxExtent from projection defaults if
  426. // they are not defined already
  427. OpenLayers.Util.applyDefaults(newOptions,
  428. OpenLayers.Projection.defaults[newOptions.projection.getCode()]);
  429. }
  430. // allow array for extents
  431. if (newOptions.maxExtent && !(newOptions.maxExtent instanceof OpenLayers.Bounds)) {
  432. newOptions.maxExtent = new OpenLayers.Bounds(newOptions.maxExtent);
  433. }
  434. if (newOptions.minExtent && !(newOptions.minExtent instanceof OpenLayers.Bounds)) {
  435. newOptions.minExtent = new OpenLayers.Bounds(newOptions.minExtent);
  436. }
  437. }
  438. // update our copy for clone
  439. OpenLayers.Util.extend(this.options, newOptions);
  440. // add new options to this
  441. OpenLayers.Util.extend(this, newOptions);
  442. // get the units from the projection, if we have a projection
  443. // and it it has units
  444. if(this.projection && this.projection.getUnits()) {
  445. this.units = this.projection.getUnits();
  446. }
  447. // re-initialize resolutions if necessary, i.e. if any of the
  448. // properties of the "properties" array defined below is set
  449. // in the new options
  450. if(this.map) {
  451. // store current resolution so we can try to restore it later
  452. var resolution = this.map.getResolution();
  453. var properties = this.RESOLUTION_PROPERTIES.concat(
  454. ["projection", "units", "minExtent", "maxExtent"]
  455. );
  456. for(var o in newOptions) {
  457. if(newOptions.hasOwnProperty(o) &&
  458. OpenLayers.Util.indexOf(properties, o) >= 0) {
  459. this.initResolutions();
  460. if (reinitialize && this.map.baseLayer === this) {
  461. // update map position, and restore previous resolution
  462. this.map.setCenter(this.map.getCenter(),
  463. this.map.getZoomForResolution(resolution),
  464. false, true
  465. );
  466. // trigger a changebaselayer event to make sure that
  467. // all controls (especially
  468. // OpenLayers.Control.PanZoomBar) get notified of the
  469. // new options
  470. this.map.events.triggerEvent("changebaselayer", {
  471. layer: this
  472. });
  473. }
  474. break;
  475. }
  476. }
  477. }
  478. },
  479. /**
  480. * APIMethod: onMapResize
  481. * This function can be implemented by subclasses
  482. */
  483. onMapResize: function() {
  484. //this function can be implemented by subclasses
  485. },
  486. /**
  487. * APIMethod: redraw
  488. * Redraws the layer. Returns true if the layer was redrawn, false if not.
  489. *
  490. * Returns:
  491. * {Boolean} The layer was redrawn.
  492. */
  493. redraw: function() {
  494. var redrawn = false;
  495. if (this.map) {
  496. // min/max Range may have changed
  497. this.inRange = this.calculateInRange();
  498. // map's center might not yet be set
  499. var extent = this.getExtent();
  500. if (extent && this.inRange && this.visibility) {
  501. var zoomChanged = true;
  502. this.moveTo(extent, zoomChanged, false);
  503. this.events.triggerEvent("moveend",
  504. {"zoomChanged": zoomChanged});
  505. redrawn = true;
  506. }
  507. }
  508. return redrawn;
  509. },
  510. /**
  511. * Method: moveTo
  512. *
  513. * Parameters:
  514. * bounds - {<OpenLayers.Bounds>}
  515. * zoomChanged - {Boolean} Tells when zoom has changed, as layers have to
  516. * do some init work in that case.
  517. * dragging - {Boolean}
  518. */
  519. moveTo:function(bounds, zoomChanged, dragging) {
  520. var display = this.visibility;
  521. if (!this.isBaseLayer) {
  522. display = display && this.inRange;
  523. }
  524. this.display(display);
  525. },
  526. /**
  527. * Method: moveByPx
  528. * Move the layer based on pixel vector. To be implemented by subclasses.
  529. *
  530. * Parameters:
  531. * dx - {Number} The x coord of the displacement vector.
  532. * dy - {Number} The y coord of the displacement vector.
  533. */
  534. moveByPx: function(dx, dy) {
  535. },
  536. /**
  537. * Method: setMap
  538. * Set the map property for the layer. This is done through an accessor
  539. * so that subclasses can override this and take special action once
  540. * they have their map variable set.
  541. *
  542. * Here we take care to bring over any of the necessary default
  543. * properties from the map.
  544. *
  545. * Parameters:
  546. * map - {<OpenLayers.Map>}
  547. */
  548. setMap: function(map) {
  549. if (this.map == null) {
  550. this.map = map;
  551. // grab some essential layer data from the map if it hasn't already
  552. // been set
  553. this.maxExtent = this.maxExtent || this.map.maxExtent;
  554. this.minExtent = this.minExtent || this.map.minExtent;
  555. this.projection = this.projection || this.map.projection;
  556. if (typeof this.projection == "string") {
  557. this.projection = new OpenLayers.Projection(this.projection);
  558. }
  559. // Check the projection to see if we can get units -- if not, refer
  560. // to properties.
  561. this.units = this.projection.getUnits() ||
  562. this.units || this.map.units;
  563. this.initResolutions();
  564. if (!this.isBaseLayer) {
  565. this.inRange = this.calculateInRange();
  566. var show = ((this.visibility) && (this.inRange));
  567. this.div.style.display = show ? "" : "none";
  568. }
  569. // deal with gutters
  570. this.setTileSize();
  571. }
  572. },
  573. /**
  574. * Method: afterAdd
  575. * Called at the end of the map.addLayer sequence. At this point, the map
  576. * will have a base layer. To be overridden by subclasses.
  577. */
  578. afterAdd: function() {
  579. },
  580. /**
  581. * APIMethod: removeMap
  582. * Just as setMap() allows each layer the possibility to take a
  583. * personalized action on being added to the map, removeMap() allows
  584. * each layer to take a personalized action on being removed from it.
  585. * For now, this will be mostly unused, except for the EventPane layer,
  586. * which needs this hook so that it can remove the special invisible
  587. * pane.
  588. *
  589. * Parameters:
  590. * map - {<OpenLayers.Map>}
  591. */
  592. removeMap: function(map) {
  593. //to be overridden by subclasses
  594. },
  595. /**
  596. * APIMethod: getImageSize
  597. *
  598. * Parameters:
  599. * bounds - {<OpenLayers.Bounds>} optional tile bounds, can be used
  600. * by subclasses that have to deal with different tile sizes at the
  601. * layer extent edges (e.g. Zoomify)
  602. *
  603. * Returns:
  604. * {<OpenLayers.Size>} The size that the image should be, taking into
  605. * account gutters.
  606. */
  607. getImageSize: function(bounds) {
  608. return (this.imageSize || this.tileSize);
  609. },
  610. /**
  611. * APIMethod: setTileSize
  612. * Set the tile size based on the map size. This also sets layer.imageSize
  613. * or use by Tile.Image.
  614. *
  615. * Parameters:
  616. * size - {<OpenLayers.Size>}
  617. */
  618. setTileSize: function(size) {
  619. var tileSize = (size) ? size :
  620. ((this.tileSize) ? this.tileSize :
  621. this.map.getTileSize());
  622. this.tileSize = tileSize;
  623. if(this.gutter) {
  624. // layers with gutters need non-null tile sizes
  625. //if(tileSize == null) {
  626. // OpenLayers.console.error("Error in layer.setMap() for " +
  627. // this.name + ": layers with " +
  628. // "gutters need non-null tile sizes");
  629. //}
  630. this.imageSize = new OpenLayers.Size(tileSize.w + (2*this.gutter),
  631. tileSize.h + (2*this.gutter));
  632. }
  633. },
  634. /**
  635. * APIMethod: getVisibility
  636. *
  637. * Returns:
  638. * {Boolean} The layer should be displayed (if in range).
  639. */
  640. getVisibility: function() {
  641. return this.visibility;
  642. },
  643. /**
  644. * APIMethod: setVisibility
  645. * Set the visibility flag for the layer and hide/show & redraw
  646. * accordingly. Fire event unless otherwise specified
  647. *
  648. * Note that visibility is no longer simply whether or not the layer's
  649. * style.display is set to "block". Now we store a 'visibility' state
  650. * property on the layer class, this allows us to remember whether or
  651. * not we *desire* for a layer to be visible. In the case where the
  652. * map's resolution is out of the layer's range, this desire may be
  653. * subverted.
  654. *
  655. * Parameters:
  656. * visibility - {Boolean} Whether or not to display the layer (if in range)
  657. */
  658. setVisibility: function(visibility) {
  659. if (visibility != this.visibility) {
  660. this.visibility = visibility;
  661. this.display(visibility);
  662. this.redraw();
  663. if (this.map != null) {
  664. this.map.events.triggerEvent("changelayer", {
  665. layer: this,
  666. property: "visibility"
  667. });
  668. }
  669. this.events.triggerEvent("visibilitychanged");
  670. }
  671. },
  672. /**
  673. * APIMethod: display
  674. * Hide or show the Layer. This is designed to be used internally, and
  675. * is not generally the way to enable or disable the layer. For that,
  676. * use the setVisibility function instead..
  677. *
  678. * Parameters:
  679. * display - {Boolean}
  680. */
  681. display: function(display) {
  682. if (display != (this.div.style.display != "none")) {
  683. this.div.style.display = (display && this.calculateInRange()) ? "block" : "none";
  684. }
  685. },
  686. /**
  687. * APIMethod: calculateInRange
  688. *
  689. * Returns:
  690. * {Boolean} The layer is displayable at the current map's current
  691. * resolution. Note that if 'alwaysInRange' is true for the layer,
  692. * this function will always return true.
  693. */
  694. calculateInRange: function() {
  695. var inRange = false;
  696. if (this.alwaysInRange) {
  697. inRange = true;
  698. } else {
  699. if (this.map) {
  700. var resolution = this.map.getResolution();
  701. inRange = ( (resolution >= this.minResolution) &&
  702. (resolution <= this.maxResolution) );
  703. }
  704. }
  705. return inRange;
  706. },
  707. /**
  708. * APIMethod: setIsBaseLayer
  709. *
  710. * Parameters:
  711. * isBaseLayer - {Boolean}
  712. */
  713. setIsBaseLayer: function(isBaseLayer) {
  714. if (isBaseLayer != this.isBaseLayer) {
  715. this.isBaseLayer = isBaseLayer;
  716. if (this.map != null) {
  717. this.map.events.triggerEvent("changebaselayer", {
  718. layer: this
  719. });
  720. }
  721. }
  722. },
  723. /********************************************************/
  724. /* */
  725. /* Baselayer Functions */
  726. /* */
  727. /********************************************************/
  728. /**
  729. * Method: initResolutions
  730. * This method's responsibility is to set up the 'resolutions' array
  731. * for the layer -- this array is what the layer will use to interface
  732. * between the zoom levels of the map and the resolution display
  733. * of the layer.
  734. *
  735. * The user has several options that determine how the array is set up.
  736. *
  737. * For a detailed explanation, see the following wiki from the
  738. * openlayers.org homepage:
  739. * http://trac.openlayers.org/wiki/SettingZoomLevels
  740. */
  741. initResolutions: function() {
  742. // ok we want resolutions, here's our strategy:
  743. //
  744. // 1. if resolutions are defined in the layer config, use them
  745. // 2. else, if scales are defined in the layer config then derive
  746. // resolutions from these scales
  747. // 3. else, attempt to calculate resolutions from maxResolution,
  748. // minResolution, numZoomLevels, maxZoomLevel set in the
  749. // layer config
  750. // 4. if we still don't have resolutions, and if resolutions
  751. // are defined in the same, use them
  752. // 5. else, if scales are defined in the map then derive
  753. // resolutions from these scales
  754. // 6. else, attempt to calculate resolutions from maxResolution,
  755. // minResolution, numZoomLevels, maxZoomLevel set in the
  756. // map
  757. // 7. hope for the best!
  758. var i, len, p;
  759. var props = {}, alwaysInRange = true;
  760. // get resolution data from layer config
  761. // (we also set alwaysInRange in the layer as appropriate)
  762. for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
  763. p = this.RESOLUTION_PROPERTIES[i];
  764. props[p] = this.options[p];
  765. if(alwaysInRange && this.options[p]) {
  766. alwaysInRange = false;
  767. }
  768. }
  769. if(this.options.alwaysInRange == null) {
  770. this.alwaysInRange = alwaysInRange;
  771. }
  772. // if we don't have resolutions then attempt to derive them from scales
  773. if(props.resolutions == null) {
  774. props.resolutions = this.resolutionsFromScales(props.scales);
  775. }
  776. // if we still don't have resolutions then attempt to calculate them
  777. if(props.resolutions == null) {
  778. props.resolutions = this.calculateResolutions(props);
  779. }
  780. // if we couldn't calculate resolutions then we look at we have
  781. // in the map
  782. if(props.resolutions == null) {
  783. for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
  784. p = this.RESOLUTION_PROPERTIES[i];
  785. props[p] = this.options[p] != null ?
  786. this.options[p] : this.map[p];
  787. }
  788. if(props.resolutions == null) {
  789. props.resolutions = this.resolutionsFromScales(props.scales);
  790. }
  791. if(props.resolutions == null) {
  792. props.resolutions = this.calculateResolutions(props);
  793. }
  794. }
  795. // ok, we new need to set properties in the instance
  796. // get maxResolution from the config if it's defined there
  797. var maxResolution;
  798. if(this.options.maxResolution &&
  799. this.options.maxResolution !== "auto") {
  800. maxResolution = this.options.maxResolution;
  801. }
  802. if(this.options.minScale) {
  803. maxResolution = OpenLayers.Util.getResolutionFromScale(
  804. this.options.minScale, this.units);
  805. }
  806. // get minResolution from the config if it's defined there
  807. var minResolution;
  808. if(this.options.minResolution &&
  809. this.options.minResolution !== "auto") {
  810. minResolution = this.options.minResolution;
  811. }
  812. if(this.options.maxScale) {
  813. minResolution = OpenLayers.Util.getResolutionFromScale(
  814. this.options.maxScale, this.units);
  815. }
  816. if(props.resolutions) {
  817. //sort resolutions array descendingly
  818. props.resolutions.sort(function(a, b) {
  819. return (b - a);
  820. });
  821. // if we still don't have a maxResolution get it from the
  822. // resolutions array
  823. if(!maxResolution) {
  824. maxResolution = props.resolutions[0];
  825. }
  826. // if we still don't have a minResolution get it from the
  827. // resolutions array
  828. if(!minResolution) {
  829. var lastIdx = props.resolutions.length - 1;
  830. minResolution = props.resolutions[lastIdx];
  831. }
  832. }
  833. this.resolutions = props.resolutions;
  834. if(this.resolutions) {
  835. len = this.resolutions.length;
  836. this.scales = new Array(len);
  837. for(i=0; i<len; i++) {
  838. this.scales[i] = OpenLayers.Util.getScaleFromResolution(
  839. this.resolutions[i], this.units);
  840. }
  841. this.numZoomLevels = len;
  842. }
  843. this.minResolution = minResolution;
  844. if(minResolution) {
  845. this.maxScale = OpenLayers.Util.getScaleFromResolution(
  846. minResolution, this.units);
  847. }
  848. this.maxResolution = maxResolution;
  849. if(maxResolution) {
  850. this.minScale = OpenLayers.Util.getScaleFromResolution(
  851. maxResolution, this.units);
  852. }
  853. },
  854. /**
  855. * Method: resolutionsFromScales
  856. * Derive resolutions from scales.
  857. *
  858. * Parameters:
  859. * scales - {Array(Number)} Scales
  860. *
  861. * Returns
  862. * {Array(Number)} Resolutions
  863. */
  864. resolutionsFromScales: function(scales) {
  865. if(scales == null) {
  866. return;
  867. }
  868. var resolutions, i, len;
  869. len = scales.length;
  870. resolutions = new Array(len);
  871. for(i=0; i<len; i++) {
  872. resolutions[i] = OpenLayers.Util.getResolutionFromScale(
  873. scales[i], this.units);
  874. }
  875. return resolutions;
  876. },
  877. /**
  878. * Method: calculateResolutions
  879. * Calculate resolutions based on the provided properties.
  880. *
  881. * Parameters:
  882. * props - {Object} Properties
  883. *
  884. * Returns:
  885. * {Array({Number})} Array of resolutions.
  886. */
  887. calculateResolutions: function(props) {
  888. var viewSize, wRes, hRes;
  889. // determine maxResolution
  890. var maxResolution = props.maxResolution;
  891. if(props.minScale != null) {
  892. maxResolution =
  893. OpenLayers.Util.getResolutionFromScale(props.minScale,
  894. this.units);
  895. } else if(maxResolution == "auto" && this.maxExtent != null) {
  896. viewSize = this.map.getSize();
  897. wRes = this.maxExtent.getWidth() / viewSize.w;
  898. hRes = this.maxExtent.getHeight() / viewSize.h;
  899. maxResolution = Math.max(wRes, hRes);
  900. }
  901. // determine minResolution
  902. var minResolution = props.minResolution;
  903. if(props.maxScale != null) {
  904. minResolution =
  905. OpenLayers.Util.getResolutionFromScale(props.maxScale,
  906. this.units);
  907. } else if(props.minResolution == "auto" && this.minExtent != null) {
  908. viewSize = this.map.getSize();
  909. wRes = this.minExtent.getWidth() / viewSize.w;
  910. hRes = this.minExtent.getHeight()/ viewSize.h;
  911. minResolution = Math.max(wRes, hRes);
  912. }
  913. if(typeof maxResolution !== "number" &&
  914. typeof minResolution !== "number" &&
  915. this.maxExtent != null) {
  916. // maxResolution for default grid sets assumes that at zoom
  917. // level zero, the whole world fits on one tile.
  918. var tileSize = this.map.getTileSize();
  919. maxResolution = Math.max(
  920. this.maxExtent.getWidth() / tileSize.w,
  921. this.maxExtent.getHeight() / tileSize.h
  922. );
  923. }
  924. // determine numZoomLevels
  925. var maxZoomLevel = props.maxZoomLevel;
  926. var numZoomLevels = props.numZoomLevels;
  927. if(typeof minResolution === "number" &&
  928. typeof maxResolution === "number" && numZoomLevels === undefined) {
  929. var ratio = maxResolution / minResolution;
  930. numZoomLevels = Math.floor(Math.log(ratio) / Math.log(2)) + 1;
  931. } else if(numZoomLevels === undefined && maxZoomLevel != null) {
  932. numZoomLevels = maxZoomLevel + 1;
  933. }
  934. // are we able to calculate resolutions?
  935. if(typeof numZoomLevels !== "number" || numZoomLevels <= 0 ||
  936. (typeof maxResolution !== "number" &&
  937. typeof minResolution !== "number")) {
  938. return;
  939. }
  940. // now we have numZoomLevels and at least one of maxResolution
  941. // or minResolution, we can populate the resolutions array
  942. var resolutions = new Array(numZoomLevels);
  943. var base = 2;
  944. if(typeof minResolution == "number" &&
  945. typeof maxResolution == "number") {
  946. // if maxResolution and minResolution are set, we calculate
  947. // the base for exponential scaling that starts at
  948. // maxResolution and ends at minResolution in numZoomLevels
  949. // steps.
  950. base = Math.pow(
  951. (maxResolution / minResolution),
  952. (1 / (numZoomLevels - 1))
  953. );
  954. }
  955. var i;
  956. if(typeof maxResolution === "number") {
  957. for(i=0; i<numZoomLevels; i++) {
  958. resolutions[i] = maxResolution / Math.pow(base, i);
  959. }
  960. } else {
  961. for(i=0; i<numZoomLevels; i++) {
  962. resolutions[numZoomLevels - 1 - i] =
  963. minResolution * Math.pow(base, i);
  964. }
  965. }
  966. return resolutions;
  967. },
  968. /**
  969. * APIMethod: getResolution
  970. *
  971. * Returns:
  972. * {Float} The currently selected resolution of the map, taken from the
  973. * resolutions array, indexed by current zoom level.
  974. */
  975. getResolution: function() {
  976. var zoom = this.map.getZoom();
  977. return this.getResolutionForZoom(zoom);
  978. },
  979. /**
  980. * APIMethod: getExtent
  981. *
  982. * Returns:
  983. * {<OpenLayers.Bounds>} A Bounds object which represents the lon/lat
  984. * bounds of the current viewPort.
  985. */
  986. getExtent: function() {
  987. // just use stock map calculateBounds function -- passing no arguments
  988. // means it will user map's current center & resolution
  989. //
  990. return this.map.calculateBounds();
  991. },
  992. /**
  993. * APIMethod: getZoomForExtent
  994. *
  995. * Parameters:
  996. * extent - {<OpenLayers.Bounds>}
  997. * closest - {Boolean} Find the zoom level that most closely fits the
  998. * specified bounds. Note that this may result in a zoom that does
  999. * not exactly contain the entire extent.
  1000. * Default is false.
  1001. *
  1002. * Returns:
  1003. * {Integer} The index of the zoomLevel (entry in the resolutions array)
  1004. * for the passed-in extent. We do this by calculating the ideal
  1005. * resolution for the given extent (based on the map size) and then
  1006. * calling getZoomForResolution(), passing along the 'closest'
  1007. * parameter.
  1008. */
  1009. getZoomForExtent: function(extent, closest) {
  1010. var viewSize = this.map.getSize();
  1011. var idealResolution = Math.max( extent.getWidth() / viewSize.w,
  1012. extent.getHeight() / viewSize.h );
  1013. return this.getZoomForResolution(idealResolution, closest);
  1014. },
  1015. /**
  1016. * Method: getDataExtent
  1017. * Calculates the max extent which includes all of the data for the layer.
  1018. * This function is to be implemented by subclasses.
  1019. *
  1020. * Returns:
  1021. * {<OpenLayers.Bounds>}
  1022. */
  1023. getDataExtent: function () {
  1024. //to be implemented by subclasses
  1025. },
  1026. /**
  1027. * APIMethod: getResolutionForZoom
  1028. *
  1029. * Parameters:
  1030. * zoom - {Float}
  1031. *
  1032. * Returns:
  1033. * {Float} A suitable resolution for the specified zoom.
  1034. */
  1035. getResolutionForZoom: function(zoom) {
  1036. zoom = Math.max(0, Math.min(zoom, this.resolutions.length - 1));
  1037. var resolution;
  1038. if(this.map.fractionalZoom) {
  1039. var low = Math.floor(zoom);
  1040. var high = Math.ceil(zoom);
  1041. resolution = this.resolutions[low] -
  1042. ((zoom-low) * (this.resolutions[low]-this.resolutions[high]));
  1043. } else {
  1044. resolution = this.resolutions[Math.round(zoom)];
  1045. }
  1046. return resolution;
  1047. },
  1048. /**
  1049. * APIMethod: getZoomForResolution
  1050. *
  1051. * Parameters:
  1052. * resolution - {Float}
  1053. * closest - {Boolean} Find the zoom level that corresponds to the absolute
  1054. * closest resolution, which may result in a zoom whose corresponding
  1055. * resolution is actually smaller than we would have desired (if this
  1056. * is being called from a getZoomForExtent() call, then this means that
  1057. * the returned zoom index might not actually contain the entire
  1058. * extent specified... but it'll be close).
  1059. * Default is false.
  1060. *
  1061. * Returns:
  1062. * {Integer} The index of the zoomLevel (entry in the resolutions array)
  1063. * that corresponds to the best fit resolution given the passed in
  1064. * value and the 'closest' specification.
  1065. */
  1066. getZoomForResolution: function(resolution, closest) {
  1067. var zoom, i, len;
  1068. if(this.map.fractionalZoom) {
  1069. var lowZoom = 0;
  1070. var highZoom = this.resolutions.length - 1;
  1071. var highRes = this.resolutions[lowZoom];
  1072. var lowRes = this.resolutions[highZoom];
  1073. var res;
  1074. for(i=0, len=this.resolutions.length; i<len; ++i) {
  1075. res = this.resolutions[i];
  1076. if(res >= resolution) {
  1077. highRes = res;
  1078. lowZoom = i;
  1079. }
  1080. if(res <= resolution) {
  1081. lowRes = res;
  1082. highZoom = i;
  1083. break;
  1084. }
  1085. }
  1086. var dRes = highRes - lowRes;
  1087. if(dRes > 0) {
  1088. zoom = lowZoom + ((highRes - resolution) / dRes);
  1089. } else {
  1090. zoom = lowZoom;
  1091. }
  1092. } else {
  1093. var diff;
  1094. var minDiff = Number.POSITIVE_INFINITY;
  1095. for(i=0, len=this.resolutions.length; i<len; i++) {
  1096. if (closest) {
  1097. diff = Math.abs(this.resolutions[i] - resolution);
  1098. if (diff > minDiff) {
  1099. break;
  1100. }
  1101. minDiff = diff;
  1102. } else {
  1103. if (this.resolutions[i] < resolution) {
  1104. break;
  1105. }
  1106. }
  1107. }
  1108. zoom = Math.max(0, i-1);
  1109. }
  1110. return zoom;
  1111. },
  1112. /**
  1113. * APIMethod: getLonLatFromViewPortPx
  1114. *
  1115. * Parameters:
  1116. * viewPortPx - {<OpenLayers.Pixel>|Object} An OpenLayers.Pixel or
  1117. * an object with a 'x'
  1118. * and 'y' properties.
  1119. *
  1120. * Returns:
  1121. * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in
  1122. * view port <OpenLayers.Pixel>, translated into lon/lat by the layer.
  1123. */
  1124. getLonLatFromViewPortPx: function (viewPortPx) {
  1125. var lonlat = null;
  1126. var map = this.map;
  1127. if (viewPortPx != null && map.minPx) {
  1128. var res = map.getResolution();
  1129. var maxExtent = map.getMaxExtent({restricted: true});
  1130. var lon = (viewPortPx.x - map.minPx.x) * res + maxExtent.left;
  1131. var lat = (map.minPx.y - viewPortPx.y) * res + maxExtent.top;
  1132. lonlat = new OpenLayers.LonLat(lon, lat);
  1133. if (this.wrapDateLine) {
  1134. lonlat = lonlat.wrapDateLine(this.maxExtent);
  1135. }
  1136. }
  1137. return lonlat;
  1138. },
  1139. /**
  1140. * APIMethod: getViewPortPxFromLonLat
  1141. * Returns a pixel location given a map location. This method will return
  1142. * fractional pixel values.
  1143. *
  1144. * Parameters:
  1145. * lonlat - {<OpenLayers.LonLat>|Object} An OpenLayers.LonLat or
  1146. * an object with a 'lon'
  1147. * and 'lat' properties.
  1148. *
  1149. * Returns:
  1150. * {<OpenLayers.Pixel>} An <OpenLayers.Pixel> which is the passed-in
  1151. * lonlat translated into view port pixels.
  1152. */
  1153. getViewPortPxFromLonLat: function (lonlat, resolution) {
  1154. var px = null;
  1155. if (lonlat != null) {
  1156. resolution = resolution || this.map.getResolution();
  1157. var extent = this.map.calculateBounds(null, resolution);
  1158. px = new OpenLayers.Pixel(
  1159. (1/resolution * (lonlat.lon - extent.left)),
  1160. (1/resolution * (extent.top - lonlat.lat))
  1161. );
  1162. }
  1163. return px;
  1164. },
  1165. /**
  1166. * APIMethod: setOpacity
  1167. * Sets the opacity for the entire layer (all images)
  1168. *
  1169. * Parameters:
  1170. * opacity - {Float}
  1171. */
  1172. setOpacity: function(opacity) {
  1173. if (opacity != this.opacity) {
  1174. this.opacity = opacity;
  1175. var childNodes = this.div.childNodes;
  1176. for(var i = 0, len = childNodes.length; i < len; ++i) {
  1177. var element = childNodes[i].firstChild || childNodes[i];
  1178. var lastChild = childNodes[i].lastChild;
  1179. //TODO de-uglify this
  1180. if (lastChild && lastChild.nodeName.toLowerCase() === "iframe") {
  1181. element = lastChild.parentNode;
  1182. }
  1183. OpenLayers.Util.modifyDOMElement(element, null, null, null,
  1184. null, null, null, opacity);
  1185. }
  1186. if (this.map != null) {
  1187. this.map.events.triggerEvent("changelayer", {
  1188. layer: this,
  1189. property: "opacity"
  1190. });
  1191. }
  1192. }
  1193. },
  1194. /**
  1195. * Method: getZIndex
  1196. *
  1197. * Returns:
  1198. * {Integer} the z-index of this layer
  1199. */
  1200. getZIndex: function () {
  1201. return this.div.style.zIndex;
  1202. },
  1203. /**
  1204. * Method: setZIndex
  1205. *
  1206. * Parameters:
  1207. * zIndex - {Integer}
  1208. */
  1209. setZIndex: function (zIndex) {
  1210. this.div.style.zIndex = zIndex;
  1211. },
  1212. /**
  1213. * Method: adjustBounds
  1214. * This function will take a bounds, and if wrapDateLine option is set
  1215. * on the layer, it will return a bounds which is wrapped around the
  1216. * world. We do not wrap for bounds which *cross* the
  1217. * maxExtent.left/right, only bounds which are entirely to the left
  1218. * or entirely to the right.
  1219. *
  1220. * Parameters:
  1221. * bounds - {<OpenLayers.Bounds>}
  1222. */
  1223. adjustBounds: function (bounds) {
  1224. if (this.gutter) {
  1225. // Adjust the extent of a bounds in map units by the
  1226. // layer's gutter in pixels.
  1227. var mapGutter = this.gutter * this.map.getResolution();
  1228. bounds = new OpenLayers.Bounds(bounds.left - mapGutter,
  1229. bounds.bottom - mapGutter,
  1230. bounds.right + mapGutter,
  1231. bounds.top + mapGutter);
  1232. }
  1233. if (this.wrapDateLine) {
  1234. // wrap around the date line, within the limits of rounding error
  1235. var wrappingOptions = {
  1236. 'rightTolerance':this.getResolution(),
  1237. 'leftTolerance':this.getResolution()
  1238. };
  1239. bounds = bounds.wrapDateLine(this.maxExtent, wrappingOptions);
  1240. }
  1241. return bounds;
  1242. },
  1243. CLASS_NAME: "OpenLayers.Layer"
  1244. });