Util.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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.js
  7. * @requires OpenLayers/BaseTypes/Bounds.js
  8. * @requires OpenLayers/BaseTypes/Element.js
  9. * @requires OpenLayers/BaseTypes/LonLat.js
  10. * @requires OpenLayers/BaseTypes/Pixel.js
  11. * @requires OpenLayers/BaseTypes/Size.js
  12. * @requires OpenLayers/Console.js
  13. * @requires OpenLayers/Lang.js
  14. */
  15. /**
  16. * Namespace: Util
  17. */
  18. OpenLayers.Util = OpenLayers.Util || {};
  19. /**
  20. * Function: getElement
  21. * This is the old $() from prototype
  22. *
  23. * Parameters:
  24. * e - {String or DOMElement or Window}
  25. * Return:
  26. * {Array(DOMElement)}
  27. */
  28. OpenLayers.Util.getElement = function() {
  29. var elements = [];
  30. for (var i=0, len=arguments.length; i<len; i++) {
  31. var element = arguments[i];
  32. if (typeof element == 'string') {
  33. element = document.getElementById(element);
  34. }
  35. if (arguments.length == 1) {
  36. return element;
  37. }
  38. elements.push(element);
  39. }
  40. return elements;
  41. };
  42. /**
  43. * Function: isElement
  44. * A cross-browser implementation of "e instanceof Element".
  45. *
  46. * Parameters:
  47. * o - {Object} The object to test.
  48. *
  49. * Returns:
  50. * {Boolean}
  51. */
  52. OpenLayers.Util.isElement = function(o) {
  53. return !!(o && o.nodeType === 1);
  54. };
  55. /**
  56. * Function: isArray
  57. * Tests that the provided object is an array.
  58. * This test handles the cross-IFRAME case not caught
  59. * by "a instanceof Array" and should be used instead.
  60. *
  61. * Parameters:
  62. * a - {Object} the object test.
  63. *
  64. * Returns
  65. * {Boolean} true if the object is an array.
  66. */
  67. OpenLayers.Util.isArray = function(a) {
  68. return (Object.prototype.toString.call(a) === '[object Array]');
  69. };
  70. /**
  71. * Maintain existing definition of $.
  72. */
  73. if(typeof window.$ === "undefined") {
  74. window.$ = OpenLayers.Util.getElement;
  75. }
  76. /**
  77. * Function: removeItem
  78. * Remove an object from an array. Iterates through the array
  79. * to find the item, then removes it.
  80. *
  81. * Parameters:
  82. * array - {Array}
  83. * item - {Object}
  84. *
  85. * Return
  86. * {Array} A reference to the array
  87. */
  88. OpenLayers.Util.removeItem = function(array, item) {
  89. for(var i = array.length - 1; i >= 0; i--) {
  90. if(array[i] == item) {
  91. array.splice(i,1);
  92. //break;more than once??
  93. }
  94. }
  95. return array;
  96. };
  97. /**
  98. * Function: clearArray
  99. * *Deprecated*. This function will disappear in 3.0.
  100. * Please use "array.length = 0" instead.
  101. *
  102. * Parameters:
  103. * array - {Array}
  104. */
  105. OpenLayers.Util.clearArray = function(array) {
  106. OpenLayers.Console.warn(
  107. OpenLayers.i18n(
  108. "methodDeprecated", {'newMethod': 'array = []'}
  109. )
  110. );
  111. array.length = 0;
  112. };
  113. /**
  114. * Function: indexOf
  115. * Seems to exist already in FF, but not in MOZ.
  116. *
  117. * Parameters:
  118. * array - {Array}
  119. * obj - {*}
  120. *
  121. * Returns:
  122. * {Integer} The index at, which the first object was found in the array.
  123. * If not found, returns -1.
  124. */
  125. OpenLayers.Util.indexOf = function(array, obj) {
  126. // use the build-in function if available.
  127. if (typeof array.indexOf == "function") {
  128. return array.indexOf(obj);
  129. } else {
  130. for (var i = 0, len = array.length; i < len; i++) {
  131. if (array[i] == obj) {
  132. return i;
  133. }
  134. }
  135. return -1;
  136. }
  137. };
  138. /**
  139. * Function: modifyDOMElement
  140. *
  141. * Modifies many properties of a DOM element all at once. Passing in
  142. * null to an individual parameter will avoid setting the attribute.
  143. *
  144. * Parameters:
  145. * element - {DOMElement} DOM element to modify.
  146. * id - {String} The element id attribute to set.
  147. * px - {<OpenLayers.Pixel>} The left and top style position.
  148. * sz - {<OpenLayers.Size>} The width and height style attributes.
  149. * position - {String} The position attribute. eg: absolute,
  150. * relative, etc.
  151. * border - {String} The style.border attribute. eg:
  152. * solid black 2px
  153. * overflow - {String} The style.overview attribute.
  154. * opacity - {Float} Fractional value (0.0 - 1.0)
  155. */
  156. OpenLayers.Util.modifyDOMElement = function(element, id, px, sz, position,
  157. border, overflow, opacity) {
  158. if (id) {
  159. element.id = id;
  160. }
  161. if (px) {
  162. element.style.left = px.x + "px";
  163. element.style.top = px.y + "px";
  164. }
  165. if (sz) {
  166. element.style.width = sz.w + "px";
  167. element.style.height = sz.h + "px";
  168. }
  169. if (position) {
  170. element.style.position = position;
  171. }
  172. if (border) {
  173. element.style.border = border;
  174. }
  175. if (overflow) {
  176. element.style.overflow = overflow;
  177. }
  178. if (parseFloat(opacity) >= 0.0 && parseFloat(opacity) < 1.0) {
  179. element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
  180. element.style.opacity = opacity;
  181. } else if (parseFloat(opacity) == 1.0) {
  182. element.style.filter = '';
  183. element.style.opacity = '';
  184. }
  185. };
  186. /**
  187. * Function: createDiv
  188. * Creates a new div and optionally set some standard attributes.
  189. * Null may be passed to each parameter if you do not wish to
  190. * set a particular attribute.
  191. * Note - zIndex is NOT set on the resulting div.
  192. *
  193. * Parameters:
  194. * id - {String} An identifier for this element. If no id is
  195. * passed an identifier will be created
  196. * automatically.
  197. * px - {<OpenLayers.Pixel>} The element left and top position.
  198. * sz - {<OpenLayers.Size>} The element width and height.
  199. * imgURL - {String} A url pointing to an image to use as a
  200. * background image.
  201. * position - {String} The style.position value. eg: absolute,
  202. * relative etc.
  203. * border - {String} The the style.border value.
  204. * eg: 2px solid black
  205. * overflow - {String} The style.overflow value. Eg. hidden
  206. * opacity - {Float} Fractional value (0.0 - 1.0)
  207. *
  208. * Returns:
  209. * {DOMElement} A DOM Div created with the specified attributes.
  210. */
  211. OpenLayers.Util.createDiv = function(id, px, sz, imgURL, position,
  212. border, overflow, opacity) {
  213. var dom = document.createElement('div');
  214. if (imgURL) {
  215. dom.style.backgroundImage = 'url(' + imgURL + ')';
  216. }
  217. //set generic properties
  218. if (!id) {
  219. id = OpenLayers.Util.createUniqueID("OpenLayersDiv");
  220. }
  221. if (!position) {
  222. position = "absolute";
  223. }
  224. OpenLayers.Util.modifyDOMElement(dom, id, px, sz, position,
  225. border, overflow, opacity);
  226. return dom;
  227. };
  228. /**
  229. * Function: createImage
  230. * Creates an img element with specific attribute values.
  231. *
  232. * Parameters:
  233. * id - {String} The id field for the img. If none assigned one will be
  234. * automatically generated.
  235. * px - {<OpenLayers.Pixel>} The left and top positions.
  236. * sz - {<OpenLayers.Size>} The style.width and style.height values.
  237. * imgURL - {String} The url to use as the image source.
  238. * position - {String} The style.position value.
  239. * border - {String} The border to place around the image.
  240. * opacity - {Float} Fractional value (0.0 - 1.0)
  241. * delayDisplay - {Boolean} If true waits until the image has been
  242. * loaded.
  243. *
  244. * Returns:
  245. * {DOMElement} A DOM Image created with the specified attributes.
  246. */
  247. OpenLayers.Util.createImage = function(id, px, sz, imgURL, position, border,
  248. opacity, delayDisplay) {
  249. var image = document.createElement("img");
  250. //set generic properties
  251. if (!id) {
  252. id = OpenLayers.Util.createUniqueID("OpenLayersDiv");
  253. }
  254. if (!position) {
  255. position = "relative";
  256. }
  257. OpenLayers.Util.modifyDOMElement(image, id, px, sz, position,
  258. border, null, opacity);
  259. if(delayDisplay) {
  260. image.style.display = "none";
  261. OpenLayers.Event.observe(image, "load",
  262. OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, image));
  263. OpenLayers.Event.observe(image, "error",
  264. OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, image));
  265. }
  266. //set special properties
  267. image.style.alt = id;
  268. image.galleryImg = "no";
  269. if (imgURL) {
  270. image.src = imgURL;
  271. }
  272. return image;
  273. };
  274. /**
  275. * Function: setOpacity
  276. * *Deprecated*. This function has been deprecated. Instead, please use
  277. * <OpenLayers.Util.modifyDOMElement>
  278. * or
  279. * <OpenLayers.Util.modifyAlphaImageDiv>
  280. *
  281. * Set the opacity of a DOM Element
  282. * Note that for this function to work in IE, elements must "have layout"
  283. * according to:
  284. * http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/haslayout.asp
  285. *
  286. * Parameters:
  287. * element - {DOMElement} Set the opacity on this DOM element
  288. * opacity - {Float} Opacity value (0.0 - 1.0)
  289. */
  290. OpenLayers.Util.setOpacity = function(element, opacity) {
  291. OpenLayers.Util.modifyDOMElement(element, null, null, null,
  292. null, null, null, opacity);
  293. };
  294. /**
  295. * Function: onImageLoad
  296. * Bound to image load events. For all images created with <createImage> or
  297. * <createAlphaImageDiv>, this function will be bound to the load event.
  298. */
  299. OpenLayers.Util.onImageLoad = function() {
  300. // The complex check here is to solve issues described in #480.
  301. // Every time a map view changes, it increments the 'viewRequestID'
  302. // property. As the requests for the images for the new map view are sent
  303. // out, they are tagged with this unique viewRequestID.
  304. //
  305. // If an image has no viewRequestID property set, we display it regardless,
  306. // but if it does have a viewRequestID property, we check that it matches
  307. // the viewRequestID set on the map.
  308. //
  309. // If the viewRequestID on the map has changed, that means that the user
  310. // has changed the map view since this specific request was sent out, and
  311. // therefore this tile does not need to be displayed (so we do not execute
  312. // this code that turns its display on).
  313. //
  314. if (!this.viewRequestID ||
  315. (this.map && this.viewRequestID == this.map.viewRequestID)) {
  316. this.style.display = "";
  317. }
  318. OpenLayers.Element.removeClass(this, "olImageLoadError");
  319. };
  320. /**
  321. * Property: IMAGE_RELOAD_ATTEMPTS
  322. * {Integer} How many times should we try to reload an image before giving up?
  323. * Default is 0
  324. */
  325. OpenLayers.IMAGE_RELOAD_ATTEMPTS = 0;
  326. /**
  327. * Function: onImageLoadError
  328. */
  329. OpenLayers.Util.onImageLoadError = function() {
  330. this._attempts = (this._attempts) ? (this._attempts + 1) : 1;
  331. if (this._attempts <= OpenLayers.IMAGE_RELOAD_ATTEMPTS) {
  332. var urls = this.urls;
  333. if (urls && OpenLayers.Util.isArray(urls) && urls.length > 1){
  334. var src = this.src.toString();
  335. var current_url, k;
  336. for (k = 0; current_url = urls[k]; k++){
  337. if(src.indexOf(current_url) != -1){
  338. break;
  339. }
  340. }
  341. var guess = Math.floor(urls.length * Math.random());
  342. var new_url = urls[guess];
  343. k = 0;
  344. while(new_url == current_url && k++ < 4){
  345. guess = Math.floor(urls.length * Math.random());
  346. new_url = urls[guess];
  347. }
  348. this.src = src.replace(current_url, new_url);
  349. } else {
  350. this.src = this.src;
  351. }
  352. } else {
  353. OpenLayers.Element.addClass(this, "olImageLoadError");
  354. }
  355. this.style.display = "";
  356. };
  357. /**
  358. * Property: alphaHackNeeded
  359. * {Boolean} true if the png alpha hack is necessary and possible, false otherwise.
  360. */
  361. OpenLayers.Util.alphaHackNeeded = null;
  362. /**
  363. * Function: alphaHack
  364. * Checks whether it's necessary (and possible) to use the png alpha
  365. * hack which allows alpha transparency for png images under Internet
  366. * Explorer.
  367. *
  368. * Returns:
  369. * {Boolean} true if the png alpha hack is necessary and possible, false otherwise.
  370. */
  371. OpenLayers.Util.alphaHack = function() {
  372. if (OpenLayers.Util.alphaHackNeeded == null) {
  373. var arVersion = navigator.appVersion.split("MSIE");
  374. var version = parseFloat(arVersion[1]);
  375. var filter = false;
  376. // IEs4Lin dies when trying to access document.body.filters, because
  377. // the property is there, but requires a DLL that can't be provided. This
  378. // means that we need to wrap this in a try/catch so that this can
  379. // continue.
  380. try {
  381. filter = !!(document.body.filters);
  382. } catch (e) {}
  383. OpenLayers.Util.alphaHackNeeded = (filter &&
  384. (version >= 5.5) && (version < 7));
  385. }
  386. return OpenLayers.Util.alphaHackNeeded;
  387. };
  388. /**
  389. * Function: modifyAlphaImageDiv
  390. *
  391. * Parameters:
  392. * div - {DOMElement} Div containing Alpha-adjusted Image
  393. * id - {String}
  394. * px - {<OpenLayers.Pixel>}
  395. * sz - {<OpenLayers.Size>}
  396. * imgURL - {String}
  397. * position - {String}
  398. * border - {String}
  399. * sizing - {String} 'crop', 'scale', or 'image'. Default is "scale"
  400. * opacity - {Float} Fractional value (0.0 - 1.0)
  401. */
  402. OpenLayers.Util.modifyAlphaImageDiv = function(div, id, px, sz, imgURL,
  403. position, border, sizing,
  404. opacity) {
  405. OpenLayers.Util.modifyDOMElement(div, id, px, sz, position,
  406. null, null, opacity);
  407. var img = div.childNodes[0];
  408. if (imgURL) {
  409. img.src = imgURL;
  410. }
  411. OpenLayers.Util.modifyDOMElement(img, div.id + "_innerImage", null, sz,
  412. "relative", border);
  413. if (OpenLayers.Util.alphaHack()) {
  414. if(div.style.display != "none") {
  415. div.style.display = "inline-block";
  416. }
  417. if (sizing == null) {
  418. sizing = "scale";
  419. }
  420. div.style.filter = "progid:DXImageTransform.Microsoft" +
  421. ".AlphaImageLoader(src='" + img.src + "', " +
  422. "sizingMethod='" + sizing + "')";
  423. if (parseFloat(div.style.opacity) >= 0.0 &&
  424. parseFloat(div.style.opacity) < 1.0) {
  425. div.style.filter += " alpha(opacity=" + div.style.opacity * 100 + ")";
  426. }
  427. img.style.filter = "alpha(opacity=0)";
  428. }
  429. };
  430. /**
  431. * Function: createAlphaImageDiv
  432. *
  433. * Parameters:
  434. * id - {String}
  435. * px - {<OpenLayers.Pixel>}
  436. * sz - {<OpenLayers.Size>}
  437. * imgURL - {String}
  438. * position - {String}
  439. * border - {String}
  440. * sizing - {String} 'crop', 'scale', or 'image'. Default is "scale"
  441. * opacity - {Float} Fractional value (0.0 - 1.0)
  442. * delayDisplay - {Boolean} If true waits until the image has been
  443. * loaded.
  444. *
  445. * Returns:
  446. * {DOMElement} A DOM Div created with a DOM Image inside it. If the hack is
  447. * needed for transparency in IE, it is added.
  448. */
  449. OpenLayers.Util.createAlphaImageDiv = function(id, px, sz, imgURL,
  450. position, border, sizing,
  451. opacity, delayDisplay) {
  452. var div = OpenLayers.Util.createDiv();
  453. var img = OpenLayers.Util.createImage(null, null, null, null, null, null,
  454. null, false);
  455. div.appendChild(img);
  456. if (delayDisplay) {
  457. img.style.display = "none";
  458. OpenLayers.Event.observe(img, "load",
  459. OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, div));
  460. OpenLayers.Event.observe(img, "error",
  461. OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, div));
  462. }
  463. OpenLayers.Util.modifyAlphaImageDiv(div, id, px, sz, imgURL, position,
  464. border, sizing, opacity);
  465. return div;
  466. };
  467. /**
  468. * Function: upperCaseObject
  469. * Creates a new hashtable and copies over all the keys from the
  470. * passed-in object, but storing them under an uppercased
  471. * version of the key at which they were stored.
  472. *
  473. * Parameters:
  474. * object - {Object}
  475. *
  476. * Returns:
  477. * {Object} A new Object with all the same keys but uppercased
  478. */
  479. OpenLayers.Util.upperCaseObject = function (object) {
  480. var uObject = {};
  481. for (var key in object) {
  482. uObject[key.toUpperCase()] = object[key];
  483. }
  484. return uObject;
  485. };
  486. /**
  487. * Function: applyDefaults
  488. * Takes an object and copies any properties that don't exist from
  489. * another properties, by analogy with OpenLayers.Util.extend() from
  490. * Prototype.js.
  491. *
  492. * Parameters:
  493. * to - {Object} The destination object.
  494. * from - {Object} The source object. Any properties of this object that
  495. * are undefined in the to object will be set on the to object.
  496. *
  497. * Returns:
  498. * {Object} A reference to the to object. Note that the to argument is modified
  499. * in place and returned by this function.
  500. */
  501. OpenLayers.Util.applyDefaults = function (to, from) {
  502. to = to || {};
  503. /*
  504. * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
  505. * prototype object" when calling hawOwnProperty if the source object is an
  506. * instance of window.Event.
  507. */
  508. var fromIsEvt = typeof window.Event == "function"
  509. && from instanceof window.Event;
  510. for (var key in from) {
  511. if (to[key] === undefined ||
  512. (!fromIsEvt && from.hasOwnProperty
  513. && from.hasOwnProperty(key) && !to.hasOwnProperty(key))) {
  514. to[key] = from[key];
  515. }
  516. }
  517. /**
  518. * IE doesn't include the toString property when iterating over an object's
  519. * properties with the for(property in object) syntax. Explicitly check if
  520. * the source has its own toString property.
  521. */
  522. if(!fromIsEvt && from && from.hasOwnProperty
  523. && from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) {
  524. to.toString = from.toString;
  525. }
  526. return to;
  527. };
  528. /**
  529. * Function: getParameterString
  530. *
  531. * Parameters:
  532. * params - {Object}
  533. *
  534. * Returns:
  535. * {String} A concatenation of the properties of an object in
  536. * http parameter notation.
  537. * (ex. <i>"key1=value1&key2=value2&key3=value3"</i>)
  538. * If a parameter is actually a list, that parameter will then
  539. * be set to a comma-seperated list of values (foo,bar) instead
  540. * of being URL escaped (foo%3Abar).
  541. */
  542. OpenLayers.Util.getParameterString = function(params) {
  543. var paramsArray = [];
  544. for (var key in params) {
  545. var value = params[key];
  546. if ((value != null) && (typeof value != 'function')) {
  547. var encodedValue;
  548. if (typeof value == 'object' && value.constructor == Array) {
  549. /* value is an array; encode items and separate with "," */
  550. var encodedItemArray = [];
  551. var item;
  552. for (var itemIndex=0, len=value.length; itemIndex<len; itemIndex++) {
  553. item = value[itemIndex];
  554. encodedItemArray.push(encodeURIComponent(
  555. (item === null || item === undefined) ? "" : item)
  556. );
  557. }
  558. encodedValue = encodedItemArray.join(",");
  559. }
  560. else {
  561. /* value is a string; simply encode */
  562. encodedValue = encodeURIComponent(value);
  563. }
  564. paramsArray.push(encodeURIComponent(key) + "=" + encodedValue);
  565. }
  566. }
  567. return paramsArray.join("&");
  568. };
  569. /**
  570. * Function: urlAppend
  571. * Appends a parameter string to a url. This function includes the logic for
  572. * using the appropriate character (none, & or ?) to append to the url before
  573. * appending the param string.
  574. *
  575. * Parameters:
  576. * url - {String} The url to append to
  577. * paramStr - {String} The param string to append
  578. *
  579. * Returns:
  580. * {String} The new url
  581. */
  582. OpenLayers.Util.urlAppend = function(url, paramStr) {
  583. var newUrl = url;
  584. if(paramStr) {
  585. var parts = (url + " ").split(/[?&]/);
  586. newUrl += (parts.pop() === " " ?
  587. paramStr :
  588. parts.length ? "&" + paramStr : "?" + paramStr);
  589. }
  590. return newUrl;
  591. };
  592. /**
  593. * Property: ImgPath
  594. * {String} Default is ''.
  595. */
  596. OpenLayers.ImgPath = '';
  597. /**
  598. * Function: getImagesLocation
  599. *
  600. * Returns:
  601. * {String} The fully formatted image location string
  602. */
  603. OpenLayers.Util.getImagesLocation = function() {
  604. return OpenLayers.ImgPath || (OpenLayers._getScriptLocation() + "img/");
  605. };
  606. /**
  607. * Function: Try
  608. * Execute functions until one of them doesn't throw an error.
  609. * Capitalized because "try" is a reserved word in JavaScript.
  610. * Taken directly from OpenLayers.Util.Try()
  611. *
  612. * Parameters:
  613. * [*] - {Function} Any number of parameters may be passed to Try()
  614. * It will attempt to execute each of them until one of them
  615. * successfully executes.
  616. * If none executes successfully, returns null.
  617. *
  618. * Returns:
  619. * {*} The value returned by the first successfully executed function.
  620. */
  621. OpenLayers.Util.Try = function() {
  622. var returnValue = null;
  623. for (var i=0, len=arguments.length; i<len; i++) {
  624. var lambda = arguments[i];
  625. try {
  626. returnValue = lambda();
  627. break;
  628. } catch (e) {}
  629. }
  630. return returnValue;
  631. };
  632. /**
  633. * Function: getXmlNodeValue
  634. *
  635. * Parameters:
  636. * node - {XMLNode}
  637. *
  638. * Returns:
  639. * {String} The text value of the given node, without breaking in firefox or IE
  640. */
  641. OpenLayers.Util.getXmlNodeValue = function(node) {
  642. var val = null;
  643. OpenLayers.Util.Try(
  644. function() {
  645. val = node.text;
  646. if (!val) {
  647. val = node.textContent;
  648. }
  649. if (!val) {
  650. val = node.firstChild.nodeValue;
  651. }
  652. },
  653. function() {
  654. val = node.textContent;
  655. });
  656. return val;
  657. };
  658. /**
  659. * Function: mouseLeft
  660. *
  661. * Parameters:
  662. * evt - {Event}
  663. * div - {HTMLDivElement}
  664. *
  665. * Returns:
  666. * {Boolean}
  667. */
  668. OpenLayers.Util.mouseLeft = function (evt, div) {
  669. // start with the element to which the mouse has moved
  670. var target = (evt.relatedTarget) ? evt.relatedTarget : evt.toElement;
  671. // walk up the DOM tree.
  672. while (target != div && target != null) {
  673. target = target.parentNode;
  674. }
  675. // if the target we stop at isn't the div, then we've left the div.
  676. return (target != div);
  677. };
  678. /**
  679. * Property: precision
  680. * {Number} The number of significant digits to retain to avoid
  681. * floating point precision errors.
  682. *
  683. * We use 14 as a "safe" default because, although IEEE 754 double floats
  684. * (standard on most modern operating systems) support up to about 16
  685. * significant digits, 14 significant digits are sufficient to represent
  686. * sub-millimeter accuracy in any coordinate system that anyone is likely to
  687. * use with OpenLayers.
  688. *
  689. * If DEFAULT_PRECISION is set to 0, the original non-truncating behavior
  690. * of OpenLayers <2.8 is preserved. Be aware that this will cause problems
  691. * with certain projections, e.g. spherical Mercator.
  692. *
  693. */
  694. OpenLayers.Util.DEFAULT_PRECISION = 14;
  695. /**
  696. * Function: toFloat
  697. * Convenience method to cast an object to a Number, rounded to the
  698. * desired floating point precision.
  699. *
  700. * Parameters:
  701. * number - {Number} The number to cast and round.
  702. * precision - {Number} An integer suitable for use with
  703. * Number.toPrecision(). Defaults to OpenLayers.Util.DEFAULT_PRECISION.
  704. * If set to 0, no rounding is performed.
  705. *
  706. * Returns:
  707. * {Number} The cast, rounded number.
  708. */
  709. OpenLayers.Util.toFloat = function (number, precision) {
  710. if (precision == null) {
  711. precision = OpenLayers.Util.DEFAULT_PRECISION;
  712. }
  713. if (typeof number !== "number") {
  714. number = parseFloat(number);
  715. }
  716. return precision === 0 ? number :
  717. parseFloat(number.toPrecision(precision));
  718. };
  719. /**
  720. * Function: rad
  721. *
  722. * Parameters:
  723. * x - {Float}
  724. *
  725. * Returns:
  726. * {Float}
  727. */
  728. OpenLayers.Util.rad = function(x) {return x*Math.PI/180;};
  729. /**
  730. * Function: deg
  731. *
  732. * Parameters:
  733. * x - {Float}
  734. *
  735. * Returns:
  736. * {Float}
  737. */
  738. OpenLayers.Util.deg = function(x) {return x*180/Math.PI;};
  739. /**
  740. * Property: VincentyConstants
  741. * {Object} Constants for Vincenty functions.
  742. */
  743. OpenLayers.Util.VincentyConstants = {
  744. a: 6378137,
  745. b: 6356752.3142,
  746. f: 1/298.257223563
  747. };
  748. /**
  749. * APIFunction: distVincenty
  750. * Given two objects representing points with geographic coordinates, this
  751. * calculates the distance between those points on the surface of an
  752. * ellipsoid.
  753. *
  754. * Parameters:
  755. * p1 - {<OpenLayers.LonLat>} (or any object with both .lat, .lon properties)
  756. * p2 - {<OpenLayers.LonLat>} (or any object with both .lat, .lon properties)
  757. *
  758. * Returns:
  759. * {Float} The distance (in km) between the two input points as measured on an
  760. * ellipsoid. Note that the input point objects must be in geographic
  761. * coordinates (decimal degrees) and the return distance is in kilometers.
  762. */
  763. OpenLayers.Util.distVincenty = function(p1, p2) {
  764. var ct = OpenLayers.Util.VincentyConstants;
  765. var a = ct.a, b = ct.b, f = ct.f;
  766. var L = OpenLayers.Util.rad(p2.lon - p1.lon);
  767. var U1 = Math.atan((1-f) * Math.tan(OpenLayers.Util.rad(p1.lat)));
  768. var U2 = Math.atan((1-f) * Math.tan(OpenLayers.Util.rad(p2.lat)));
  769. var sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
  770. var sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
  771. var lambda = L, lambdaP = 2*Math.PI;
  772. var iterLimit = 20;
  773. while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0) {
  774. var sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);
  775. var sinSigma = Math.sqrt((cosU2*sinLambda) * (cosU2*sinLambda) +
  776. (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
  777. if (sinSigma==0) {
  778. return 0; // co-incident points
  779. }
  780. var cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
  781. var sigma = Math.atan2(sinSigma, cosSigma);
  782. var alpha = Math.asin(cosU1 * cosU2 * sinLambda / sinSigma);
  783. var cosSqAlpha = Math.cos(alpha) * Math.cos(alpha);
  784. var cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
  785. var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
  786. lambdaP = lambda;
  787. lambda = L + (1-C) * f * Math.sin(alpha) *
  788. (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
  789. }
  790. if (iterLimit==0) {
  791. return NaN; // formula failed to converge
  792. }
  793. var uSq = cosSqAlpha * (a*a - b*b) / (b*b);
  794. var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
  795. var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
  796. var deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-
  797. B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
  798. var s = b*A*(sigma-deltaSigma);
  799. var d = s.toFixed(3)/1000; // round to 1mm precision
  800. return d;
  801. };
  802. /**
  803. * APIFunction: destinationVincenty
  804. * Calculate destination point given start point lat/long (numeric degrees),
  805. * bearing (numeric degrees) & distance (in m).
  806. * Adapted from Chris Veness work, see
  807. * http://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html
  808. *
  809. * Parameters:
  810. * lonlat - {<OpenLayers.LonLat>} (or any object with both .lat, .lon
  811. * properties) The start point.
  812. * brng - {Float} The bearing (degrees).
  813. * dist - {Float} The ground distance (meters).
  814. *
  815. * Returns:
  816. * {<OpenLayers.LonLat>} The destination point.
  817. */
  818. OpenLayers.Util.destinationVincenty = function(lonlat, brng, dist) {
  819. var u = OpenLayers.Util;
  820. var ct = u.VincentyConstants;
  821. var a = ct.a, b = ct.b, f = ct.f;
  822. var lon1 = lonlat.lon;
  823. var lat1 = lonlat.lat;
  824. var s = dist;
  825. var alpha1 = u.rad(brng);
  826. var sinAlpha1 = Math.sin(alpha1);
  827. var cosAlpha1 = Math.cos(alpha1);
  828. var tanU1 = (1-f) * Math.tan(u.rad(lat1));
  829. var cosU1 = 1 / Math.sqrt((1 + tanU1*tanU1)), sinU1 = tanU1*cosU1;
  830. var sigma1 = Math.atan2(tanU1, cosAlpha1);
  831. var sinAlpha = cosU1 * sinAlpha1;
  832. var cosSqAlpha = 1 - sinAlpha*sinAlpha;
  833. var uSq = cosSqAlpha * (a*a - b*b) / (b*b);
  834. var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
  835. var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
  836. var sigma = s / (b*A), sigmaP = 2*Math.PI;
  837. while (Math.abs(sigma-sigmaP) > 1e-12) {
  838. var cos2SigmaM = Math.cos(2*sigma1 + sigma);
  839. var sinSigma = Math.sin(sigma);
  840. var cosSigma = Math.cos(sigma);
  841. var deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-
  842. B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
  843. sigmaP = sigma;
  844. sigma = s / (b*A) + deltaSigma;
  845. }
  846. var tmp = sinU1*sinSigma - cosU1*cosSigma*cosAlpha1;
  847. var lat2 = Math.atan2(sinU1*cosSigma + cosU1*sinSigma*cosAlpha1,
  848. (1-f)*Math.sqrt(sinAlpha*sinAlpha + tmp*tmp));
  849. var lambda = Math.atan2(sinSigma*sinAlpha1, cosU1*cosSigma - sinU1*sinSigma*cosAlpha1);
  850. var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
  851. var L = lambda - (1-C) * f * sinAlpha *
  852. (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
  853. var revAz = Math.atan2(sinAlpha, -tmp); // final bearing
  854. return new OpenLayers.LonLat(lon1+u.deg(L), u.deg(lat2));
  855. };
  856. /**
  857. * Function: getParameters
  858. * Parse the parameters from a URL or from the current page itself into a
  859. * JavaScript Object. Note that parameter values with commas are separated
  860. * out into an Array.
  861. *
  862. * Parameters:
  863. * url - {String} Optional url used to extract the query string.
  864. * If url is null or is not supplied, query string is taken
  865. * from the page location.
  866. *
  867. * Returns:
  868. * {Object} An object of key/value pairs from the query string.
  869. */
  870. OpenLayers.Util.getParameters = function(url) {
  871. // if no url specified, take it from the location bar
  872. url = (url === null || url === undefined) ? window.location.href : url;
  873. //parse out parameters portion of url string
  874. var paramsString = "";
  875. if (OpenLayers.String.contains(url, '?')) {
  876. var start = url.indexOf('?') + 1;
  877. var end = OpenLayers.String.contains(url, "#") ?
  878. url.indexOf('#') : url.length;
  879. paramsString = url.substring(start, end);
  880. }
  881. var parameters = {};
  882. var pairs = paramsString.split(/[&;]/);
  883. for(var i=0, len=pairs.length; i<len; ++i) {
  884. var keyValue = pairs[i].split('=');
  885. if (keyValue[0]) {
  886. var key = keyValue[0];
  887. try {
  888. key = decodeURIComponent(key);
  889. } catch (err) {
  890. key = unescape(key);
  891. }
  892. // being liberal by replacing "+" with " "
  893. var value = (keyValue[1] || '').replace(/\+/g, " ");
  894. try {
  895. value = decodeURIComponent(value);
  896. } catch (err) {
  897. value = unescape(value);
  898. }
  899. // follow OGC convention of comma delimited values
  900. value = value.split(",");
  901. //if there's only one value, do not return as array
  902. if (value.length == 1) {
  903. value = value[0];
  904. }
  905. parameters[key] = value;
  906. }
  907. }
  908. return parameters;
  909. };
  910. /**
  911. * Function: getArgs
  912. * *Deprecated*. Will be removed in 3.0. Please use instead
  913. * <OpenLayers.Util.getParameters>
  914. *
  915. * Parameters:
  916. * url - {String} Optional url used to extract the query string.
  917. * If null, query string is taken from page location.
  918. *
  919. * Returns:
  920. * {Object} An object of key/value pairs from the query string.
  921. */
  922. OpenLayers.Util.getArgs = function(url) {
  923. OpenLayers.Console.warn(
  924. OpenLayers.i18n(
  925. "methodDeprecated", {'newMethod': 'OpenLayers.Util.getParameters'}
  926. )
  927. );
  928. return OpenLayers.Util.getParameters(url);
  929. };
  930. /**
  931. * Property: lastSeqID
  932. * {Integer} The ever-incrementing count variable.
  933. * Used for generating unique ids.
  934. */
  935. OpenLayers.Util.lastSeqID = 0;
  936. /**
  937. * Function: createUniqueID
  938. * Create a unique identifier for this session. Each time this function
  939. * is called, a counter is incremented. The return will be the optional
  940. * prefix (defaults to "id_") appended with the counter value.
  941. *
  942. * Parameters:
  943. * prefix {String} Optionsal string to prefix unique id. Default is "id_".
  944. *
  945. * Returns:
  946. * {String} A unique id string, built on the passed in prefix.
  947. */
  948. OpenLayers.Util.createUniqueID = function(prefix) {
  949. if (prefix == null) {
  950. prefix = "id_";
  951. }
  952. OpenLayers.Util.lastSeqID += 1;
  953. return prefix + OpenLayers.Util.lastSeqID;
  954. };
  955. /**
  956. * Constant: INCHES_PER_UNIT
  957. * {Object} Constant inches per unit -- borrowed from MapServer mapscale.c
  958. * derivation of nautical miles from http://en.wikipedia.org/wiki/Nautical_mile
  959. * Includes the full set of units supported by CS-MAP (http://trac.osgeo.org/csmap/)
  960. * and PROJ.4 (http://trac.osgeo.org/proj/)
  961. * The hardcoded table is maintain in a CS-MAP source code module named CSdataU.c
  962. * The hardcoded table of PROJ.4 units are in pj_units.c.
  963. */
  964. OpenLayers.INCHES_PER_UNIT = {
  965. 'inches': 1.0,
  966. 'ft': 12.0,
  967. 'mi': 63360.0,
  968. 'm': 39.3701,
  969. 'km': 39370.1,
  970. 'dd': 4374754,
  971. 'yd': 36
  972. };
  973. OpenLayers.INCHES_PER_UNIT["in"]= OpenLayers.INCHES_PER_UNIT.inches;
  974. OpenLayers.INCHES_PER_UNIT["degrees"] = OpenLayers.INCHES_PER_UNIT.dd;
  975. OpenLayers.INCHES_PER_UNIT["nmi"] = 1852 * OpenLayers.INCHES_PER_UNIT.m;
  976. // Units from CS-Map
  977. OpenLayers.METERS_PER_INCH = 0.02540005080010160020;
  978. OpenLayers.Util.extend(OpenLayers.INCHES_PER_UNIT, {
  979. "Inch": OpenLayers.INCHES_PER_UNIT.inches,
  980. "Meter": 1.0 / OpenLayers.METERS_PER_INCH, //EPSG:9001
  981. "Foot": 0.30480060960121920243 / OpenLayers.METERS_PER_INCH, //EPSG:9003
  982. "IFoot": 0.30480000000000000000 / OpenLayers.METERS_PER_INCH, //EPSG:9002
  983. "ClarkeFoot": 0.3047972651151 / OpenLayers.METERS_PER_INCH, //EPSG:9005
  984. "SearsFoot": 0.30479947153867624624 / OpenLayers.METERS_PER_INCH, //EPSG:9041
  985. "GoldCoastFoot": 0.30479971018150881758 / OpenLayers.METERS_PER_INCH, //EPSG:9094
  986. "IInch": 0.02540000000000000000 / OpenLayers.METERS_PER_INCH,
  987. "MicroInch": 0.00002540000000000000 / OpenLayers.METERS_PER_INCH,
  988. "Mil": 0.00000002540000000000 / OpenLayers.METERS_PER_INCH,
  989. "Centimeter": 0.01000000000000000000 / OpenLayers.METERS_PER_INCH,
  990. "Kilometer": 1000.00000000000000000000 / OpenLayers.METERS_PER_INCH, //EPSG:9036
  991. "Yard": 0.91440182880365760731 / OpenLayers.METERS_PER_INCH,
  992. "SearsYard": 0.914398414616029 / OpenLayers.METERS_PER_INCH, //EPSG:9040
  993. "IndianYard": 0.91439853074444079983 / OpenLayers.METERS_PER_INCH, //EPSG:9084
  994. "IndianYd37": 0.91439523 / OpenLayers.METERS_PER_INCH, //EPSG:9085
  995. "IndianYd62": 0.9143988 / OpenLayers.METERS_PER_INCH, //EPSG:9086
  996. "IndianYd75": 0.9143985 / OpenLayers.METERS_PER_INCH, //EPSG:9087
  997. "IndianFoot": 0.30479951 / OpenLayers.METERS_PER_INCH, //EPSG:9080
  998. "IndianFt37": 0.30479841 / OpenLayers.METERS_PER_INCH, //EPSG:9081
  999. "IndianFt62": 0.3047996 / OpenLayers.METERS_PER_INCH, //EPSG:9082
  1000. "IndianFt75": 0.3047995 / OpenLayers.METERS_PER_INCH, //EPSG:9083
  1001. "Mile": 1609.34721869443738887477 / OpenLayers.METERS_PER_INCH,
  1002. "IYard": 0.91440000000000000000 / OpenLayers.METERS_PER_INCH, //EPSG:9096
  1003. "IMile": 1609.34400000000000000000 / OpenLayers.METERS_PER_INCH, //EPSG:9093
  1004. "NautM": 1852.00000000000000000000 / OpenLayers.METERS_PER_INCH, //EPSG:9030
  1005. "Lat-66": 110943.316488932731 / OpenLayers.METERS_PER_INCH,
  1006. "Lat-83": 110946.25736872234125 / OpenLayers.METERS_PER_INCH,
  1007. "Decimeter": 0.10000000000000000000 / OpenLayers.METERS_PER_INCH,
  1008. "Millimeter": 0.00100000000000000000 / OpenLayers.METERS_PER_INCH,
  1009. "Dekameter": 10.00000000000000000000 / OpenLayers.METERS_PER_INCH,
  1010. "Decameter": 10.00000000000000000000 / OpenLayers.METERS_PER_INCH,
  1011. "Hectometer": 100.00000000000000000000 / OpenLayers.METERS_PER_INCH,
  1012. "GermanMeter": 1.0000135965 / OpenLayers.METERS_PER_INCH, //EPSG:9031
  1013. "CaGrid": 0.999738 / OpenLayers.METERS_PER_INCH,
  1014. "ClarkeChain": 20.1166194976 / OpenLayers.METERS_PER_INCH, //EPSG:9038
  1015. "GunterChain": 20.11684023368047 / OpenLayers.METERS_PER_INCH, //EPSG:9033
  1016. "BenoitChain": 20.116782494375872 / OpenLayers.METERS_PER_INCH, //EPSG:9062
  1017. "SearsChain": 20.11676512155 / OpenLayers.METERS_PER_INCH, //EPSG:9042
  1018. "ClarkeLink": 0.201166194976 / OpenLayers.METERS_PER_INCH, //EPSG:9039
  1019. "GunterLink": 0.2011684023368047 / OpenLayers.METERS_PER_INCH, //EPSG:9034
  1020. "BenoitLink": 0.20116782494375872 / OpenLayers.METERS_PER_INCH, //EPSG:9063
  1021. "SearsLink": 0.2011676512155 / OpenLayers.METERS_PER_INCH, //EPSG:9043
  1022. "Rod": 5.02921005842012 / OpenLayers.METERS_PER_INCH,
  1023. "IntnlChain": 20.1168 / OpenLayers.METERS_PER_INCH, //EPSG:9097
  1024. "IntnlLink": 0.201168 / OpenLayers.METERS_PER_INCH, //EPSG:9098
  1025. "Perch": 5.02921005842012 / OpenLayers.METERS_PER_INCH,
  1026. "Pole": 5.02921005842012 / OpenLayers.METERS_PER_INCH,
  1027. "Furlong": 201.1684023368046 / OpenLayers.METERS_PER_INCH,
  1028. "Rood": 3.778266898 / OpenLayers.METERS_PER_INCH,
  1029. "CapeFoot": 0.3047972615 / OpenLayers.METERS_PER_INCH,
  1030. "Brealey": 375.00000000000000000000 / OpenLayers.METERS_PER_INCH,
  1031. "ModAmFt": 0.304812252984505969011938 / OpenLayers.METERS_PER_INCH,
  1032. "Fathom": 1.8288 / OpenLayers.METERS_PER_INCH,
  1033. "NautM-UK": 1853.184 / OpenLayers.METERS_PER_INCH,
  1034. "50kilometers": 50000.0 / OpenLayers.METERS_PER_INCH,
  1035. "150kilometers": 150000.0 / OpenLayers.METERS_PER_INCH
  1036. });
  1037. //unit abbreviations supported by PROJ.4
  1038. OpenLayers.Util.extend(OpenLayers.INCHES_PER_UNIT, {
  1039. "mm": OpenLayers.INCHES_PER_UNIT["Meter"] / 1000.0,
  1040. "cm": OpenLayers.INCHES_PER_UNIT["Meter"] / 100.0,
  1041. "dm": OpenLayers.INCHES_PER_UNIT["Meter"] * 100.0,
  1042. "km": OpenLayers.INCHES_PER_UNIT["Meter"] * 1000.0,
  1043. "kmi": OpenLayers.INCHES_PER_UNIT["nmi"], //International Nautical Mile
  1044. "fath": OpenLayers.INCHES_PER_UNIT["Fathom"], //International Fathom
  1045. "ch": OpenLayers.INCHES_PER_UNIT["IntnlChain"], //International Chain
  1046. "link": OpenLayers.INCHES_PER_UNIT["IntnlLink"], //International Link
  1047. "us-in": OpenLayers.INCHES_PER_UNIT["inches"], //U.S. Surveyor's Inch
  1048. "us-ft": OpenLayers.INCHES_PER_UNIT["Foot"], //U.S. Surveyor's Foot
  1049. "us-yd": OpenLayers.INCHES_PER_UNIT["Yard"], //U.S. Surveyor's Yard
  1050. "us-ch": OpenLayers.INCHES_PER_UNIT["GunterChain"], //U.S. Surveyor's Chain
  1051. "us-mi": OpenLayers.INCHES_PER_UNIT["Mile"], //U.S. Surveyor's Statute Mile
  1052. "ind-yd": OpenLayers.INCHES_PER_UNIT["IndianYd37"], //Indian Yard
  1053. "ind-ft": OpenLayers.INCHES_PER_UNIT["IndianFt37"], //Indian Foot
  1054. "ind-ch": 20.11669506 / OpenLayers.METERS_PER_INCH //Indian Chain
  1055. });
  1056. /**
  1057. * Constant: DOTS_PER_INCH
  1058. * {Integer} 72 (A sensible default)
  1059. */
  1060. OpenLayers.DOTS_PER_INCH = 72;
  1061. /**
  1062. * Function: normalizeScale
  1063. *
  1064. * Parameters:
  1065. * scale - {float}
  1066. *
  1067. * Returns:
  1068. * {Float} A normalized scale value, in 1 / X format.
  1069. * This means that if a value less than one ( already 1/x) is passed
  1070. * in, it just returns scale directly. Otherwise, it returns
  1071. * 1 / scale
  1072. */
  1073. OpenLayers.Util.normalizeScale = function (scale) {
  1074. var normScale = (scale > 1.0) ? (1.0 / scale)
  1075. : scale;
  1076. return normScale;
  1077. };
  1078. /**
  1079. * Function: getResolutionFromScale
  1080. *
  1081. * Parameters:
  1082. * scale - {Float}
  1083. * units - {String} Index into OpenLayers.INCHES_PER_UNIT hashtable.
  1084. * Default is degrees
  1085. *
  1086. * Returns:
  1087. * {Float} The corresponding resolution given passed-in scale and unit
  1088. * parameters. If the given scale is falsey, the returned resolution will
  1089. * be undefined.
  1090. */
  1091. OpenLayers.Util.getResolutionFromScale = function (scale, units) {
  1092. var resolution;
  1093. if (scale) {
  1094. if (units == null) {
  1095. units = "degrees";
  1096. }
  1097. var normScale = OpenLayers.Util.normalizeScale(scale);
  1098. resolution = 1 / (normScale * OpenLayers.INCHES_PER_UNIT[units]
  1099. * OpenLayers.DOTS_PER_INCH);
  1100. }
  1101. return resolution;
  1102. };
  1103. /**
  1104. * Function: getScaleFromResolution
  1105. *
  1106. * Parameters:
  1107. * resolution - {Float}
  1108. * units - {String} Index into OpenLayers.INCHES_PER_UNIT hashtable.
  1109. * Default is degrees
  1110. *
  1111. * Returns:
  1112. * {Float} The corresponding scale given passed-in resolution and unit
  1113. * parameters.
  1114. */
  1115. OpenLayers.Util.getScaleFromResolution = function (resolution, units) {
  1116. if (units == null) {
  1117. units = "degrees";
  1118. }
  1119. var scale = resolution * OpenLayers.INCHES_PER_UNIT[units] *
  1120. OpenLayers.DOTS_PER_INCH;
  1121. return scale;
  1122. };
  1123. /**
  1124. * Function: safeStopPropagation
  1125. * *Deprecated*. This function has been deprecated. Please use directly
  1126. * <OpenLayers.Event.stop> passing 'true' as the 2nd
  1127. * argument (preventDefault)
  1128. *
  1129. * Safely stop the propagation of an event *without* preventing
  1130. * the default browser action from occurring.
  1131. *
  1132. * Parameter:
  1133. * evt - {Event}
  1134. */
  1135. OpenLayers.Util.safeStopPropagation = function(evt) {
  1136. OpenLayers.Event.stop(evt, true);
  1137. };
  1138. /**
  1139. * Function: pagePosition
  1140. * Calculates the position of an element on the page (see
  1141. * http://code.google.com/p/doctype/wiki/ArticlePageOffset)
  1142. *
  1143. * OpenLayers.Util.pagePosition is based on Yahoo's getXY method, which is
  1144. * Copyright (c) 2006, Yahoo! Inc.
  1145. * All rights reserved.
  1146. *
  1147. * Redistribution and use of this software in source and binary forms, with or
  1148. * without modification, are permitted provided that the following conditions
  1149. * are met:
  1150. *
  1151. * * Redistributions of source code must retain the above copyright notice,
  1152. * this list of conditions and the following disclaimer.
  1153. *
  1154. * * Redistributions in binary form must reproduce the above copyright notice,
  1155. * this list of conditions and the following disclaimer in the documentation
  1156. * and/or other materials provided with the distribution.
  1157. *
  1158. * * Neither the name of Yahoo! Inc. nor the names of its contributors may be
  1159. * used to endorse or promote products derived from this software without
  1160. * specific prior written permission of Yahoo! Inc.
  1161. *
  1162. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1163. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1164. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1165. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  1166. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  1167. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  1168. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  1169. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  1170. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  1171. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  1172. * POSSIBILITY OF SUCH DAMAGE.
  1173. *
  1174. * Parameters:
  1175. * forElement - {DOMElement}
  1176. *
  1177. * Returns:
  1178. * {Array} two item array, Left value then Top value.
  1179. */
  1180. OpenLayers.Util.pagePosition = function(forElement) {
  1181. // NOTE: If element is hidden (display none or disconnected or any the
  1182. // ancestors are hidden) we get (0,0) by default but we still do the
  1183. // accumulation of scroll position.
  1184. var pos = [0, 0];
  1185. var viewportElement = OpenLayers.Util.getViewportElement();
  1186. if (!forElement || forElement == window || forElement == viewportElement) {
  1187. // viewport is always at 0,0 as that defined the coordinate system for
  1188. // this function - this avoids special case checks in the code below
  1189. return pos;
  1190. }
  1191. // Gecko browsers normally use getBoxObjectFor to calculate the position.
  1192. // When invoked for an element with an implicit absolute position though it
  1193. // can be off by one. Therefore the recursive implementation is used in
  1194. // those (relatively rare) cases.
  1195. var BUGGY_GECKO_BOX_OBJECT =
  1196. OpenLayers.IS_GECKO && document.getBoxObjectFor &&
  1197. OpenLayers.Element.getStyle(forElement, 'position') == 'absolute' &&
  1198. (forElement.style.top == '' || forElement.style.left == '');
  1199. var parent = null;
  1200. var box;
  1201. if (forElement.getBoundingClientRect) { // IE
  1202. box = forElement.getBoundingClientRect();
  1203. var scrollTop = viewportElement.scrollTop;
  1204. var scrollLeft = viewportElement.scrollLeft;
  1205. pos[0] = box.left + scrollLeft;
  1206. pos[1] = box.top + scrollTop;
  1207. } else if (document.getBoxObjectFor && !BUGGY_GECKO_BOX_OBJECT) { // gecko
  1208. // Gecko ignores the scroll values for ancestors, up to 1.9. See:
  1209. // https://bugzilla.mozilla.org/show_bug.cgi?id=328881 and
  1210. // https://bugzilla.mozilla.org/show_bug.cgi?id=330619
  1211. box = document.getBoxObjectFor(forElement);
  1212. var vpBox = document.getBoxObjectFor(viewportElement);
  1213. pos[0] = box.screenX - vpBox.screenX;
  1214. pos[1] = box.screenY - vpBox.screenY;
  1215. } else { // safari/opera
  1216. pos[0] = forElement.offsetLeft;
  1217. pos[1] = forElement.offsetTop;
  1218. parent = forElement.offsetParent;
  1219. if (parent != forElement) {
  1220. while (parent) {
  1221. pos[0] += parent.offsetLeft;
  1222. pos[1] += parent.offsetTop;
  1223. parent = parent.offsetParent;
  1224. }
  1225. }
  1226. var browser = OpenLayers.BROWSER_NAME;
  1227. // opera & (safari absolute) incorrectly account for body offsetTop
  1228. if (browser == "opera" || (browser == "safari" &&
  1229. OpenLayers.Element.getStyle(forElement, 'position') == 'absolute')) {
  1230. pos[1] -= document.body.offsetTop;
  1231. }
  1232. // accumulate the scroll positions for everything but the body element
  1233. parent = forElement.offsetParent;
  1234. while (parent && parent != document.body) {
  1235. pos[0] -= parent.scrollLeft;
  1236. // see https://bugs.opera.com/show_bug.cgi?id=249965
  1237. if (browser != "opera" || parent.tagName != 'TR') {
  1238. pos[1] -= parent.scrollTop;
  1239. }
  1240. parent = parent.offsetParent;
  1241. }
  1242. }
  1243. return pos;
  1244. };
  1245. /**
  1246. * Function: getViewportElement
  1247. * Returns die viewport element of the document. The viewport element is
  1248. * usually document.documentElement, except in IE,where it is either
  1249. * document.body or document.documentElement, depending on the document's
  1250. * compatibility mode (see
  1251. * http://code.google.com/p/doctype/wiki/ArticleClientViewportElement)
  1252. */
  1253. OpenLayers.Util.getViewportElement = function() {
  1254. var viewportElement = arguments.callee.viewportElement;
  1255. if (viewportElement == undefined) {
  1256. viewportElement = (OpenLayers.BROWSER_NAME == "msie" &&
  1257. document.compatMode != 'CSS1Compat') ? document.body :
  1258. document.documentElement;
  1259. arguments.callee.viewportElement = viewportElement;
  1260. }
  1261. return viewportElement;
  1262. };
  1263. /**
  1264. * Function: isEquivalentUrl
  1265. * Test two URLs for equivalence.
  1266. *
  1267. * Setting 'ignoreCase' allows for case-independent comparison.
  1268. *
  1269. * Comparison is based on:
  1270. * - Protocol
  1271. * - Host (evaluated without the port)
  1272. * - Port (set 'ignorePort80' to ignore "80" values)
  1273. * - Hash ( set 'ignoreHash' to disable)
  1274. * - Pathname (for relative <-> absolute comparison)
  1275. * - Arguments (so they can be out of order)
  1276. *
  1277. * Parameters:
  1278. * url1 - {String}
  1279. * url2 - {String}
  1280. * options - {Object} Allows for customization of comparison:
  1281. * 'ignoreCase' - Default is True
  1282. * 'ignorePort80' - Default is True
  1283. * 'ignoreHash' - Default is True
  1284. *
  1285. * Returns:
  1286. * {Boolean} Whether or not the two URLs are equivalent
  1287. */
  1288. OpenLayers.Util.isEquivalentUrl = function(url1, url2, options) {
  1289. options = options || {};
  1290. OpenLayers.Util.applyDefaults(options, {
  1291. ignoreCase: true,
  1292. ignorePort80: true,
  1293. ignoreHash: true
  1294. });
  1295. var urlObj1 = OpenLayers.Util.createUrlObject(url1, options);
  1296. var urlObj2 = OpenLayers.Util.createUrlObject(url2, options);
  1297. //compare all keys except for "args" (treated below)
  1298. for(var key in urlObj1) {
  1299. if(key !== "args") {
  1300. if(urlObj1[key] != urlObj2[key]) {
  1301. return false;
  1302. }
  1303. }
  1304. }
  1305. // compare search args - irrespective of order
  1306. for(var key in urlObj1.args) {
  1307. if(urlObj1.args[key] != urlObj2.args[key]) {
  1308. return false;
  1309. }
  1310. delete urlObj2.args[key];
  1311. }
  1312. // urlObj2 shouldn't have any args left
  1313. for(var key in urlObj2.args) {
  1314. return false;
  1315. }
  1316. return true;
  1317. };
  1318. /**
  1319. * Function: createUrlObject
  1320. *
  1321. * Parameters:
  1322. * url - {String}
  1323. * options - {Object} A hash of options. Can be one of:
  1324. * ignoreCase: lowercase url,
  1325. * ignorePort80: don't include explicit port if port is 80,
  1326. * ignoreHash: Don't include part of url after the hash (#).
  1327. *
  1328. * Returns:
  1329. * {Object} An object with separate url, a, port, host, and args parsed out
  1330. * and ready for comparison
  1331. */
  1332. OpenLayers.Util.createUrlObject = function(url, options) {
  1333. options = options || {};
  1334. // deal with relative urls first
  1335. if(!(/^\w+:\/\//).test(url)) {
  1336. var loc = window.location;
  1337. var port = loc.port ? ":" + loc.port : "";
  1338. var fullUrl = loc.protocol + "//" + loc.host.split(":").shift() + port;
  1339. if(url.indexOf("/") === 0) {
  1340. // full pathname
  1341. url = fullUrl + url;
  1342. } else {
  1343. // relative to current path
  1344. var parts = loc.pathname.split("/");
  1345. parts.pop();
  1346. url = fullUrl + parts.join("/") + "/" + url;
  1347. }
  1348. }
  1349. if (options.ignoreCase) {
  1350. url = url.toLowerCase();
  1351. }
  1352. var a = document.createElement('a');
  1353. a.href = url;
  1354. var urlObject = {};
  1355. //host (without port)
  1356. urlObject.host = a.host.split(":").shift();
  1357. //protocol
  1358. urlObject.protocol = a.protocol;
  1359. //port (get uniform browser behavior with port 80 here)
  1360. if(options.ignorePort80) {
  1361. urlObject.port = (a.port == "80" || a.port == "0") ? "" : a.port;
  1362. } else {
  1363. urlObject.port = (a.port == "" || a.port == "0") ? "80" : a.port;
  1364. }
  1365. //hash
  1366. urlObject.hash = (options.ignoreHash || a.hash === "#") ? "" : a.hash;
  1367. //args
  1368. var queryString = a.search;
  1369. if (!queryString) {
  1370. var qMark = url.indexOf("?");
  1371. queryString = (qMark != -1) ? url.substr(qMark) : "";
  1372. }
  1373. urlObject.args = OpenLayers.Util.getParameters(queryString);
  1374. //pathname (uniform browser behavior with leading "/")
  1375. urlObject.pathname = (a.pathname.charAt(0) == "/") ? a.pathname : "/" + a.pathname;
  1376. return urlObject;
  1377. };
  1378. /**
  1379. * Function: removeTail
  1380. * Takes a url and removes everything after the ? and #
  1381. *
  1382. * Parameters:
  1383. * url - {String} The url to process
  1384. *
  1385. * Returns:
  1386. * {String} The string with all queryString and Hash removed
  1387. */
  1388. OpenLayers.Util.removeTail = function(url) {
  1389. var head = null;
  1390. var qMark = url.indexOf("?");
  1391. var hashMark = url.indexOf("#");
  1392. if (qMark == -1) {
  1393. head = (hashMark != -1) ? url.substr(0,hashMark) : url;
  1394. } else {
  1395. head = (hashMark != -1) ? url.substr(0,Math.min(qMark, hashMark))
  1396. : url.substr(0, qMark);
  1397. }
  1398. return head;
  1399. };
  1400. /**
  1401. * Constant: IS_GECKO
  1402. * {Boolean} True if the userAgent reports the browser to use the Gecko engine
  1403. */
  1404. OpenLayers.IS_GECKO = (function() {
  1405. var ua = navigator.userAgent.toLowerCase();
  1406. return ua.indexOf("webkit") == -1 && ua.indexOf("gecko") != -1;
  1407. })();
  1408. /**
  1409. * Constant: BROWSER_NAME
  1410. * {String}
  1411. * A substring of the navigator.userAgent property. Depending on the userAgent
  1412. * property, this will be the empty string or one of the following:
  1413. * * "opera" -- Opera
  1414. * * "msie" -- Internet Explorer
  1415. * * "safari" -- Safari
  1416. * * "firefox" -- Firefox
  1417. * * "mozilla" -- Mozilla
  1418. */
  1419. OpenLayers.BROWSER_NAME = (function() {
  1420. var name = "";
  1421. var ua = navigator.userAgent.toLowerCase();
  1422. if (ua.indexOf("opera") != -1) {
  1423. name = "opera";
  1424. } else if (ua.indexOf("msie") != -1) {
  1425. name = "msie";
  1426. } else if (ua.indexOf("safari") != -1) {
  1427. name = "safari";
  1428. } else if (ua.indexOf("mozilla") != -1) {
  1429. if (ua.indexOf("firefox") != -1) {
  1430. name = "firefox";
  1431. } else {
  1432. name = "mozilla";
  1433. }
  1434. }
  1435. return name;
  1436. })();
  1437. /**
  1438. * Function: getBrowserName
  1439. *
  1440. * Returns:
  1441. * {String} A string which specifies which is the current
  1442. * browser in which we are running.
  1443. *
  1444. * Currently-supported browser detection and codes:
  1445. * * 'opera' -- Opera
  1446. * * 'msie' -- Internet Explorer
  1447. * * 'safari' -- Safari
  1448. * * 'firefox' -- Firefox
  1449. * * 'mozilla' -- Mozilla
  1450. *
  1451. * If we are unable to property identify the browser, we
  1452. * return an empty string.
  1453. */
  1454. OpenLayers.Util.getBrowserName = function() {
  1455. return OpenLayers.BROWSER_NAME;
  1456. };
  1457. /**
  1458. * Method: getRenderedDimensions
  1459. * Renders the contentHTML offscreen to determine actual dimensions for
  1460. * popup sizing. As we need layout to determine dimensions the content
  1461. * is rendered -9999px to the left and absolute to ensure the
  1462. * scrollbars do not flicker
  1463. *
  1464. * Parameters:
  1465. * contentHTML
  1466. * size - {<OpenLayers.Size>} If either the 'w' or 'h' properties is
  1467. * specified, we fix that dimension of the div to be measured. This is
  1468. * useful in the case where we have a limit in one dimension and must
  1469. * therefore meaure the flow in the other dimension.
  1470. * options - {Object}
  1471. *
  1472. * Allowed Options:
  1473. * displayClass - {String} Optional parameter. A CSS class name(s) string
  1474. * to provide the CSS context of the rendered content.
  1475. * containerElement - {DOMElement} Optional parameter. Insert the HTML to
  1476. * this node instead of the body root when calculating dimensions.
  1477. *
  1478. * Returns:
  1479. * {OpenLayers.Size}
  1480. */
  1481. OpenLayers.Util.getRenderedDimensions = function(contentHTML, size, options) {
  1482. var w, h;
  1483. // create temp container div with restricted size
  1484. var container = document.createElement("div");
  1485. container.style.visibility = "hidden";
  1486. var containerElement = (options && options.containerElement)
  1487. ? options.containerElement : document.body;
  1488. //fix a dimension, if specified.
  1489. if (size) {
  1490. if (size.w) {
  1491. w = size.w;
  1492. container.style.width = w + "px";
  1493. } else if (size.h) {
  1494. h = size.h;
  1495. container.style.height = h + "px";
  1496. }
  1497. }
  1498. //add css classes, if specified
  1499. if (options && options.displayClass) {
  1500. container.className = options.displayClass;
  1501. }
  1502. // create temp content div and assign content
  1503. var content = document.createElement("div");
  1504. content.innerHTML = contentHTML;
  1505. // we need overflow visible when calculating the size
  1506. content.style.overflow = "visible";
  1507. if (content.childNodes) {
  1508. for (var i=0, l=content.childNodes.length; i<l; i++) {
  1509. if (!content.childNodes[i].style) continue;
  1510. content.childNodes[i].style.overflow = "visible";
  1511. }
  1512. }
  1513. // add content to restricted container
  1514. container.appendChild(content);
  1515. // append container to body for rendering
  1516. containerElement.appendChild(container);
  1517. // Opera and IE7 can't handle a node with position:aboslute if it inherits
  1518. // position:absolute from a parent.
  1519. var parentHasPositionAbsolute = false;
  1520. var parent = container.parentNode;
  1521. while (parent && parent.tagName.toLowerCase()!="body") {
  1522. var parentPosition = OpenLayers.Element.getStyle(parent, "position");
  1523. if(parentPosition == "absolute") {
  1524. parentHasPositionAbsolute = true;
  1525. break;
  1526. } else if (parentPosition && parentPosition != "static") {
  1527. break;
  1528. }
  1529. parent = parent.parentNode;
  1530. }
  1531. if(!parentHasPositionAbsolute) {
  1532. container.style.position = "absolute";
  1533. }
  1534. // calculate scroll width of content and add corners and shadow width
  1535. if (!w) {
  1536. w = parseInt(content.scrollWidth);
  1537. // update container width to allow height to adjust
  1538. container.style.width = w + "px";
  1539. }
  1540. // capture height and add shadow and corner image widths
  1541. if (!h) {
  1542. h = parseInt(content.scrollHeight);
  1543. }
  1544. // remove elements
  1545. container.removeChild(content);
  1546. containerElement.removeChild(container);
  1547. return new OpenLayers.Size(w, h);
  1548. };
  1549. /**
  1550. * APIFunction: getScrollbarWidth
  1551. * This function has been modified by the OpenLayers from the original version,
  1552. * written by Matthew Eernisse and released under the Apache 2
  1553. * license here:
  1554. *
  1555. * http://www.fleegix.org/articles/2006/05/30/getting-the-scrollbar-width-in-pixels
  1556. *
  1557. * It has been modified simply to cache its value, since it is physically
  1558. * impossible that this code could ever run in more than one browser at
  1559. * once.
  1560. *
  1561. * Returns:
  1562. * {Integer}
  1563. */
  1564. OpenLayers.Util.getScrollbarWidth = function() {
  1565. var scrollbarWidth = OpenLayers.Util._scrollbarWidth;
  1566. if (scrollbarWidth == null) {
  1567. var scr = null;
  1568. var inn = null;
  1569. var wNoScroll = 0;
  1570. var wScroll = 0;
  1571. // Outer scrolling div
  1572. scr = document.createElement('div');
  1573. scr.style.position = 'absolute';
  1574. scr.style.top = '-1000px';
  1575. scr.style.left = '-1000px';
  1576. scr.style.width = '100px';
  1577. scr.style.height = '50px';
  1578. // Start with no scrollbar
  1579. scr.style.overflow = 'hidden';
  1580. // Inner content div
  1581. inn = document.createElement('div');
  1582. inn.style.width = '100%';
  1583. inn.style.height = '200px';
  1584. // Put the inner div in the scrolling div
  1585. scr.appendChild(inn);
  1586. // Append the scrolling div to the doc
  1587. document.body.appendChild(scr);
  1588. // Width of the inner div sans scrollbar
  1589. wNoScroll = inn.offsetWidth;
  1590. // Add the scrollbar
  1591. scr.style.overflow = 'scroll';
  1592. // Width of the inner div width scrollbar
  1593. wScroll = inn.offsetWidth;
  1594. // Remove the scrolling div from the doc
  1595. document.body.removeChild(document.body.lastChild);
  1596. // Pixel width of the scroller
  1597. OpenLayers.Util._scrollbarWidth = (wNoScroll - wScroll);
  1598. scrollbarWidth = OpenLayers.Util._scrollbarWidth;
  1599. }
  1600. return scrollbarWidth;
  1601. };
  1602. /**
  1603. * APIFunction: getFormattedLonLat
  1604. * This function will return latitude or longitude value formatted as
  1605. *
  1606. * Parameters:
  1607. * coordinate - {Float} the coordinate value to be formatted
  1608. * axis - {String} value of either 'lat' or 'lon' to indicate which axis is to
  1609. * to be formatted (default = lat)
  1610. * dmsOption - {String} specify the precision of the output can be one of:
  1611. * 'dms' show degrees minutes and seconds
  1612. * 'dm' show only degrees and minutes
  1613. * 'd' show only degrees
  1614. *
  1615. * Returns:
  1616. * {String} the coordinate value formatted as a string
  1617. */
  1618. OpenLayers.Util.getFormattedLonLat = function(coordinate, axis, dmsOption) {
  1619. if (!dmsOption) {
  1620. dmsOption = 'dms'; //default to show degree, minutes, seconds
  1621. }
  1622. coordinate = (coordinate+540)%360 - 180; // normalize for sphere being round
  1623. var abscoordinate = Math.abs(coordinate);
  1624. var coordinatedegrees = Math.floor(abscoordinate);
  1625. var coordinateminutes = (abscoordinate - coordinatedegrees)/(1/60);
  1626. var tempcoordinateminutes = coordinateminutes;
  1627. coordinateminutes = Math.floor(coordinateminutes);
  1628. var coordinateseconds = (tempcoordinateminutes - coordinateminutes)/(1/60);
  1629. coordinateseconds = Math.round(coordinateseconds*10);
  1630. coordinateseconds /= 10;
  1631. if( coordinateseconds >= 60) {
  1632. coordinateseconds -= 60;
  1633. coordinateminutes += 1;
  1634. if( coordinateminutes >= 60) {
  1635. coordinateminutes -= 60;
  1636. coordinatedegrees += 1;
  1637. }
  1638. }
  1639. if( coordinatedegrees < 10 ) {
  1640. coordinatedegrees = "0" + coordinatedegrees;
  1641. }
  1642. var str = coordinatedegrees + "\u00B0";
  1643. if (dmsOption.indexOf('dm') >= 0) {
  1644. if( coordinateminutes < 10 ) {
  1645. coordinateminutes = "0" + coordinateminutes;
  1646. }
  1647. str += coordinateminutes + "'";
  1648. if (dmsOption.indexOf('dms') >= 0) {
  1649. if( coordinateseconds < 10 ) {
  1650. coordinateseconds = "0" + coordinateseconds;
  1651. }
  1652. str += coordinateseconds + '"';
  1653. }
  1654. }
  1655. if (axis == "lon") {
  1656. str += coordinate < 0 ? OpenLayers.i18n("W") : OpenLayers.i18n("E");
  1657. } else {
  1658. str += coordinate < 0 ? OpenLayers.i18n("S") : OpenLayers.i18n("N");
  1659. }
  1660. return str;
  1661. };