import * as turf from "@turf/turf"; import parse from "wellknown"; export let colors = [ "#62ADED", "#DFE15A", "#6EDC8D", "#00A42E", "#F9B447", "#7F4FE5", "#FF6969", "#27CED9", "#DF56F5", "#DCFFAF", ]; export function getCentroid(geojson, eindex) { if (turf.getType(geojson) === "Feature") { geojson = geojson.geometry; } if (turf.getType(geojson) === "Polygon") { return turf.centroid(geojson).geometry.coordinates; } else if (turf.getType(geojson) === "MultiPolygon") { let polygon = turf.polygon(geojson.coordinates[eindex]); return turf.centroid(polygon).geometry.coordinates; } else { throw new Error("Unsupported geometry type"); } } // 数组变二维数组方法 export function listToMatrix(list, elementsPerSubArray) { var matrix = [], i, k; for (i = 0, k = -1; i < list.length; i++) { if (i % elementsPerSubArray === 0) { k++; matrix[k] = []; } matrix[k].push(list[i]); } return matrix; } // 加载GeoJSON数据 export function loadGeoJSON(geom, yanse, adata, fun) { let geojson = typeof geom === 'string' ? parse(geom) : geom; let polygon = Cesium.GeoJsonDataSource.load(geojson, { clampToGround: true, stroke: yanse ? Cesium.Color.fromCssColorString(yanse) : Cesium.Color.RED.withAlpha(0), fill: yanse || adata.fill ? Cesium.Color.fromCssColorString(adata.fill || yanse).withAlpha(adata.fill_a || 0.4) : Cesium.Color.WHITE.withAlpha(0), //Cesium.Color.fromCssColorString("rgba(10, 95, 152, 0.4)"), //注意:颜色必须大写,即不能为blue strokeWidth: adata.sw || 2, }) polygon.then((data) => { viewer.dataSources.add(data); if (adata.isfly) viewer.flyTo(data, { offset: new Cesium.HeadingPitchRange(0, -45), }); fun(data) }); // let polygon = Cesium.GeoJsonDataSource.load(geojson, { // clampToGround: true, // stroke: yanse // ? Cesium.Color.fromCssColorString(yanse) // : Cesium.Color.RED.withAlpha(0), // fill: yanse // ? Cesium.Color.fromCssColorString(yanse).withAlpha(0.3) // : Cesium.Color.WHITE.withAlpha(0), // strokeWidth: isfly ? 5 : 2, // }); // polygon.then(function (dataSource) { // viewer.dataSources.add(dataSource); // layerSources[id] = dataSource; // if (isfly) // viewer.flyTo(dataSource, { // offset: new Cesium.HeadingPitchRange(0, -45), // }); // if (resdata) { // dataSource.entities.values.forEach((entity) => { // entity.properties = resdata; // }); // } // }); } export function removeGeoJSON(name) { if (!window.viewer) return; // viewer.entities.removeAll(); // viewer.dataSources.removeAll(); for (var i = viewer.dataSources._dataSources.length - 1; i >= 0; i--) { var das = viewer.dataSources._dataSources[i]; if (das.name == name) viewer.dataSources.remove(das); } }, export function addPonit() { // manager_multi_level_query.entities.add({ // name: "manager_multi_level_query", // position: Cesium.Cartesian3.fromDegrees(longitude, latitude, 40), // billboard: { // // 图像地址,URI或Canvas的属性 // image: "./static/images/overview/go.png", // height: 34, // width: 36, // scale: 1.0, // zIndex: 2, // show: true, // }, // }); } export function clearPoint() { // 图标 // var entities = manager_multi_level_query.entities.values; // for (var i = entities.length - 1; i >= 0; i--) { // manager_multi_level_query.entities.remove(entities[i]); // } } export function moveHandler(fun) { let handler = new Cesium.ScreenSpaceEventHandler(scene.canvas); handler.setInputAction((event) => { var position = viewer.scene.pickPosition(event.endPosition); if (!position) return; fun(position) }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); } export function pickPoint(isjs, fun) { let handler = new Cesium.ScreenSpaceEventHandler(scene.canvas); handler.setInputAction((event) => { var position = viewer.scene.pickPosition(event.position); if (!position) //点击到地球之外 return false; if (isjs) { var cartographic = Cesium.Cartographic.fromCartesian(position); let lon = Cesium.Math.toDegrees(cartographic.longitude); let lat = Cesium.Math.toDegrees(cartographic.latitude); let height = cartographic.height; // let heading = viewer.scene.camera.heading; // let pitch = viewer.scene.camera.pitch; fun(lon, lat, height) } else fun(position, handler) handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } // 导出结果下载图片 export function download(base64data, fu) { var image = new Image(); image.src = base64data; image.onload = function () { var canvas = convertImageToCanvas(image, fu); var url = canvas.toDataURL("image/jpeg"); var a = document.createElement("a"); var event = new MouseEvent("click"); a.download = new Date().getTime() + ".jpg"; // 指定下载图片的名称 a.href = url; a.dispatchEvent(event); // 触发超链接的点击事件 }; } // 根据图片生成画布 function convertImageToCanvas(image, fu) { var canvas = document.createElement("canvas"); canvas.width = image.width; canvas.height = image.height; var ctx = canvas.getContext("2d"); ctx.drawImage(image, 0, 0); if (typeof fu == 'function') fu(canvas, ctx) else drawLegends(canvas, ctx, fu); return canvas; } // 绘制图例 function drawLegends(canvas, ctx, legends) { var padding = 10; // 图例与边缘的间距 var lineHeight = 30; // 每行图例的高度 var labW = 200; var x = canvas.width - padding - labW; // 图例的起始X坐标 var y = canvas.height - legends.length * lineHeight - padding; // 图例的起始Y坐标 // 绘制颜色块 ctx.fillStyle = "#ffffff"; ctx.fillRect( x - padding, y - padding, canvas.width - x + padding, canvas.height - y + padding ); legends.forEach(function (legend, index) { // 绘制文本 if (legend.scS) { ctx.fillStyle = "black"; ctx.fillText(legend.scS, x, y + index * lineHeight + lineHeight / 2); // 绘制颜色块 ctx.fillStyle = legend.fill; ctx.fillRect( x + (labW / 3) * 2, y + index * lineHeight, 30, lineHeight ); } else { ctx.fillStyle = "black"; let text = `${legend.name}:${legend.value}${legend.unit}`; ctx.fillText(text, x, y + index * lineHeight + lineHeight / 2); } }); } /** *wms数据查询计算用户坐标应该减去的差值 */ function getZoomBbox(zoom) { let level0 = 142.03125 let box = level0 / Math.pow(2, zoom) box = box / 2 return box } /** *根据用户点击的坐标计算 bbox参数 */ export function bbox(latlng, zoom) { let box = getZoomBbox(zoom) let boxMin = { lat: latlng.lat - box, lng: latlng.lng - box } let boxMax = { lat: latlng.lat + box, lng: latlng.lng + box } // console.table({zoom,box,latlng,boxMin, boxMax }) return `${boxMin.lng},${boxMin.lat},${boxMax.lng},${boxMax.lat}` } export function str2Unicode(str) { var es = []; for (var i = 0; i < str.length; i++) es[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4); return "\\u" + es.join("\\u"); }