help.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import * as turf from "@turf/turf";
  2. import parse from "wellknown";
  3. export let colors = [
  4. "#62ADED",
  5. "#DFE15A",
  6. "#6EDC8D",
  7. "#00A42E",
  8. "#F9B447",
  9. "#7F4FE5",
  10. "#FF6969",
  11. "#27CED9",
  12. "#DF56F5",
  13. "#DCFFAF",
  14. ];
  15. export function getCentroid(geojson, eindex) {
  16. if (turf.getType(geojson) === "Feature") {
  17. geojson = geojson.geometry;
  18. }
  19. if (turf.getType(geojson) === "Polygon") {
  20. return turf.centroid(geojson).geometry.coordinates;
  21. } else if (turf.getType(geojson) === "MultiPolygon") {
  22. let polygon = turf.polygon(geojson.coordinates[eindex]);
  23. return turf.centroid(polygon).geometry.coordinates;
  24. } else {
  25. throw new Error("Unsupported geometry type");
  26. }
  27. }
  28. // 数组变二维数组方法
  29. export function listToMatrix(list, elementsPerSubArray) {
  30. var matrix = [],
  31. i,
  32. k;
  33. for (i = 0, k = -1; i < list.length; i++) {
  34. if (i % elementsPerSubArray === 0) {
  35. k++;
  36. matrix[k] = [];
  37. }
  38. matrix[k].push(list[i]);
  39. }
  40. return matrix;
  41. }
  42. // 加载GeoJSON数据
  43. export function loadGeoJSON(geom, yanse, adata, fun) {
  44. let geojson = typeof geom === 'string' ? parse(geom) : geom;
  45. let polygon = Cesium.GeoJsonDataSource.load(geojson, {
  46. clampToGround: true,
  47. stroke: yanse
  48. ? Cesium.Color.fromCssColorString(yanse)
  49. : Cesium.Color.RED.withAlpha(0),
  50. fill: yanse || adata.fill
  51. ? Cesium.Color.fromCssColorString(adata.fill || yanse).withAlpha(adata.fill_a || 0.4)
  52. : Cesium.Color.WHITE.withAlpha(0), //Cesium.Color.fromCssColorString("rgba(10, 95, 152, 0.4)"), //注意:颜色必须大写,即不能为blue
  53. strokeWidth: adata.sw || 2,
  54. })
  55. polygon.then((data) => {
  56. viewer.dataSources.add(data);
  57. if (adata.isfly)
  58. viewer.flyTo(data, {
  59. offset: new Cesium.HeadingPitchRange(0, -45),
  60. });
  61. fun(data)
  62. });
  63. // let polygon = Cesium.GeoJsonDataSource.load(geojson, {
  64. // clampToGround: true,
  65. // stroke: yanse
  66. // ? Cesium.Color.fromCssColorString(yanse)
  67. // : Cesium.Color.RED.withAlpha(0),
  68. // fill: yanse
  69. // ? Cesium.Color.fromCssColorString(yanse).withAlpha(0.3)
  70. // : Cesium.Color.WHITE.withAlpha(0),
  71. // strokeWidth: isfly ? 5 : 2,
  72. // });
  73. // polygon.then(function (dataSource) {
  74. // viewer.dataSources.add(dataSource);
  75. // layerSources[id] = dataSource;
  76. // if (isfly)
  77. // viewer.flyTo(dataSource, {
  78. // offset: new Cesium.HeadingPitchRange(0, -45),
  79. // });
  80. // if (resdata) {
  81. // dataSource.entities.values.forEach((entity) => {
  82. // entity.properties = resdata;
  83. // });
  84. // }
  85. // });
  86. }
  87. export function addPonit() {
  88. // manager_multi_level_query.entities.add({
  89. // name: "manager_multi_level_query",
  90. // position: Cesium.Cartesian3.fromDegrees(longitude, latitude, 40),
  91. // billboard: {
  92. // // 图像地址,URI或Canvas的属性
  93. // image: "./static/images/overview/go.png",
  94. // height: 34,
  95. // width: 36,
  96. // scale: 1.0,
  97. // zIndex: 2,
  98. // show: true,
  99. // },
  100. // });
  101. }
  102. export function clearPoint() {
  103. // 图标
  104. // var entities = manager_multi_level_query.entities.values;
  105. // for (var i = entities.length - 1; i >= 0; i--) {
  106. // manager_multi_level_query.entities.remove(entities[i]);
  107. // }
  108. }
  109. export function moveHandler(fun) {
  110. let handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  111. handler.setInputAction((event) => {
  112. var position = viewer.scene.pickPosition(event.endPosition);
  113. if (!position) return;
  114. fun(position)
  115. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  116. }
  117. export function pickPoint(isjs, fun) {
  118. let handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  119. handler.setInputAction((event) => {
  120. var position = viewer.scene.pickPosition(event.position);
  121. if (!position)
  122. //点击到地球之外
  123. return false;
  124. if (isjs) {
  125. var cartographic = Cesium.Cartographic.fromCartesian(position);
  126. let lon = Cesium.Math.toDegrees(cartographic.longitude);
  127. let lat = Cesium.Math.toDegrees(cartographic.latitude);
  128. let height = cartographic.height;
  129. // let heading = viewer.scene.camera.heading;
  130. // let pitch = viewer.scene.camera.pitch;
  131. fun(lon, lat, height)
  132. }
  133. else fun(position, handler)
  134. handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  135. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  136. }
  137. // 导出结果下载图片
  138. export function download(base64data, fu) {
  139. var image = new Image();
  140. image.src = base64data;
  141. image.onload = function () {
  142. var canvas = convertImageToCanvas(image, fu);
  143. var url = canvas.toDataURL("image/jpeg");
  144. var a = document.createElement("a");
  145. var event = new MouseEvent("click");
  146. a.download = new Date().getTime() + ".jpg"; // 指定下载图片的名称
  147. a.href = url;
  148. a.dispatchEvent(event); // 触发超链接的点击事件
  149. };
  150. }
  151. // 根据图片生成画布
  152. function convertImageToCanvas(image, fu) {
  153. var canvas = document.createElement("canvas");
  154. canvas.width = image.width;
  155. canvas.height = image.height;
  156. var ctx = canvas.getContext("2d");
  157. ctx.drawImage(image, 0, 0);
  158. if (typeof fu == 'function')
  159. fu(canvas, ctx)
  160. else drawLegends(canvas, ctx, fu);
  161. return canvas;
  162. }
  163. // 绘制图例
  164. function drawLegends(canvas, ctx, legends) {
  165. var padding = 10; // 图例与边缘的间距
  166. var lineHeight = 30; // 每行图例的高度
  167. var labW = 200;
  168. var x = canvas.width - padding - labW; // 图例的起始X坐标
  169. var y = canvas.height - legends.length * lineHeight - padding; // 图例的起始Y坐标
  170. // 绘制颜色块
  171. ctx.fillStyle = "#ffffff";
  172. ctx.fillRect(
  173. x - padding,
  174. y - padding,
  175. canvas.width - x + padding,
  176. canvas.height - y + padding
  177. );
  178. legends.forEach(function (legend, index) {
  179. // 绘制文本
  180. if (legend.scS) {
  181. ctx.fillStyle = "black";
  182. ctx.fillText(legend.scS, x, y + index * lineHeight + lineHeight / 2);
  183. // 绘制颜色块
  184. ctx.fillStyle = legend.fill;
  185. ctx.fillRect(
  186. x + (labW / 3) * 2,
  187. y + index * lineHeight,
  188. 30,
  189. lineHeight
  190. );
  191. } else {
  192. ctx.fillStyle = "black";
  193. let text = `${legend.name}:${legend.value}${legend.unit}`;
  194. ctx.fillText(text, x, y + index * lineHeight + lineHeight / 2);
  195. }
  196. });
  197. }
  198. /**
  199. *wms数据查询计算用户坐标应该减去的差值
  200. */
  201. function getZoomBbox(zoom) {
  202. let level0 = 142.03125
  203. let box = level0 / Math.pow(2, zoom)
  204. box = box / 2
  205. return box
  206. }
  207. /**
  208. *根据用户点击的坐标计算 bbox参数
  209. */
  210. export function bbox(latlng, zoom) {
  211. let box = getZoomBbox(zoom)
  212. let boxMin = {
  213. lat: latlng.lat - box,
  214. lng: latlng.lng - box
  215. }
  216. let boxMax = {
  217. lat: latlng.lat + box,
  218. lng: latlng.lng + box
  219. }
  220. // console.table({zoom,box,latlng,boxMin, boxMax })
  221. return `${boxMin.lng},${boxMin.lat},${boxMax.lng},${boxMax.lat}`
  222. }
  223. export function str2Unicode(str) {
  224. var es = [];
  225. for (var i = 0; i < str.length; i++)
  226. es[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
  227. return "\\u" + es.join("\\u");
  228. }