Common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import Size from './Size'
  2. import Pixel from './Pixel'
  3. import LngLat from './LngLat'
  4. import Bounds from './Bounds'
  5. import Check from './Check'
  6. import * as olProj from 'ol/proj';
  7. /**
  8. * @description KMap.Common类 通用静态方法
  9. */
  10. class Common{
  11. static ShowLevel = [1,22]
  12. /**
  13. *@description 底图Zoom限制
  14. */
  15. static BaseLayerZoom = [1,18]
  16. /**
  17. * @description 利通地图像素转OpenLayers地图像素
  18. * @param {KMap.Pixel} pixel KMap.Pixel格式的像素,必填
  19. * @returns {Array} OpenLayers格式的像素,包含两个元素的数组[x,y]
  20. */
  21. static KMapPixel2MapPixel(pixel){
  22. let mapPixel = [pixel.getX(),pixel.getY()]
  23. return mapPixel
  24. }
  25. /**
  26. * @description OpenLayers地图像素转利通地图像素
  27. * @param {Array} pixel OpenLayers格式的像素,包含两个元素的数组[x,y],必填
  28. * @returns {KMap.Pixel} KMap.Pixel格式的像素
  29. */
  30. static MapPixel2KMapPixel(pixel) {
  31. let ltPixel = new Pixel(pixel[0], pixel[1])
  32. return ltPixel
  33. }
  34. /**
  35. * @description 利通地图像素尺寸转OpenLayers地图像素尺寸
  36. * @param {KMap.Size} size KMap.Size格式的尺寸,必填
  37. * @returns {Array} OpenLayers地图像素尺寸,包含两个元素的数组[width,height]
  38. */
  39. static KMapSize2MapSize(size) {
  40. let mapSize = [size.getWidth(), size.getHeight()]
  41. return mapSize
  42. }
  43. /**
  44. * @description OpenLayers地图像素尺寸转利通地图像素尺寸
  45. * @param {Array} size OpenLayers地图像素尺寸,包含两个元素的数组[width,height],必填
  46. * @returns {KMap.Size} 格式的尺寸
  47. */
  48. static MapSize2KMapSize(size) {
  49. let ltSize = new Size(size[0], size[1])
  50. return ltSize
  51. }
  52. /**
  53. * @description 利通地图经纬度转OpenLayers地图经纬度
  54. * @param {KMap.LngLat} lnglat KMap.LngLat格式的经纬度,必填
  55. * @returns {Array} OpenLayers的经纬度格式,包含两个元素的数组[lng,lat]
  56. */
  57. static KMapLngLat2MapLngLat(lnglat) {
  58. let alnglat = [lnglat.getLng(),lnglat.getLat()]
  59. return alnglat
  60. }
  61. /**
  62. * @description OpenLayers地图经纬度转利通地图经纬度
  63. * @param {Array} lnglat OpenLayers的经纬度格式,包含两个元素的数组[lng,lat],必填
  64. * @returns {KMap.LngLat} KMap.LngLat格式的经纬度
  65. */
  66. static MapLngLat2KMapLngLat(lnglat) {
  67. let ltlnglat = new LngLat(lnglat[0], lnglat[1])
  68. return ltlnglat
  69. }
  70. /**
  71. * @description 利通地图经纬度矩形范围转OpenLayers地图经纬度矩形范围
  72. * @param {KMap.Bounds} bounds KMap.Bounds对象,必填
  73. * @returns {Array} 西南角经度、西南角纬度、东北角经度、东北角纬度构成的数组
  74. */
  75. static KMapBounds2MapBounds(bounds) {
  76. let array = new Array()
  77. let southWest = bounds.getSouthWest()
  78. let northEast = bounds.getNorthEast()
  79. array.push(southWest.getLng())
  80. array.push(southWest.getLat())
  81. array.push(northEast.getLng())
  82. array.push(northEast.getLat())
  83. return array
  84. }
  85. /**
  86. * @description OpenLayers地图经纬度矩形范围转利通地图经纬度范围
  87. * @param {Array} bounds 西南角经度、西南角纬度、东北角经度、东北角纬度构成的数组,必填
  88. * @returns {KMap.Bounds} KMap.Bounds类型对象
  89. */
  90. static MapBounds2KMapBounds(bounds) {
  91. let southWest = [bounds[0],bounds[1]]
  92. let northEast = [bounds[2],bounds[3]]
  93. southWest = new LngLat(southWest[0],southWest[1])
  94. northEast = new LngLat(northEast[0],northEast[1])
  95. bounds = new Bounds(southWest,northEast)
  96. return bounds
  97. }
  98. static toWGS84LngLat(map,coordinate){
  99. map.getCoor
  100. return olProj.transform(coordinate,map.getProjection(),"EPSG:4326")
  101. }
  102. /**
  103. * @description 扩展JSON对象属性
  104. * @param {JSON} des 目标JSON对象,必填
  105. * @param {JSON} src 源JSON对象,必填
  106. * @param {boolean} override 是否覆盖属性,选填
  107. * @returns {JSON} 目标JSON对象
  108. */
  109. static extend(des, src, override){
  110. if(src instanceof Array){
  111. for(let i = 0, len = src.length; i < len; i++)
  112. Common.extend(des, src[i], override)
  113. }
  114. for( let i in src){
  115. if(override || !(i in des)){
  116. des[i] = src[i]
  117. }
  118. }
  119. return des
  120. }
  121. static checkLngLat(lng,lat){
  122. let info = Check.lngLat(lng,lat)
  123. if(!info.isPass){
  124. throw new Error(info.msg)
  125. }
  126. }
  127. static notEmpty(name,str){
  128. let info = Check.notEmpty(name,str)
  129. if(!info.isPass){
  130. throw new Error(info.msg)
  131. }
  132. }
  133. }
  134. export default Common