util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. export function getValues(arr,key) {
  2. let ids = [];
  3. arr.forEach(item => {
  4. ids.push(item[key]);
  5. })
  6. return ids;
  7. }
  8. export function getMethodValues(arr,method,key) {
  9. let ids = [];
  10. arr.forEach(item => {
  11. ids.push(item[method](key));
  12. })
  13. return ids;
  14. }
  15. export function cleanArrayUndefined(arr) {
  16. let nr = [];
  17. arr.forEach(v => {
  18. if(v){
  19. nr.push(v)
  20. }
  21. })
  22. return nr;
  23. }
  24. export function splitDate(time) {
  25. let date = new Date(time);
  26. let YY = date.getFullYear();
  27. let MM = date.getMonth() + 1;
  28. let DD = date.getDate();
  29. let hh = date.getHours();
  30. let mm = date.getMinutes();
  31. let ss = date.getSeconds();
  32. return {year:YY,month:MM,day:DD,hour:hh,minute:mm,second:ss}
  33. }
  34. /**
  35. * 获取文件名后缀
  36. * @param filePath
  37. * @returns {string}
  38. */
  39. export function getFileExt(filePath) {
  40. let index= filePath.lastIndexOf(".");
  41. //获取后缀
  42. let ext = filePath.substring(index+1);
  43. return ext;
  44. }
  45. //point 字符串格式转成数组
  46. export function parsePointToArray(pointString) {
  47. // 移除字符串两端的空格(虽然这里可能不是必需的,但为了保险起见还是加上)
  48. pointString = pointString.trim();
  49. // 正则表达式匹配POINT后面的括号和坐标
  50. let regex = /POINT\s*\(([\d.]+)\s+([\d.]+)\)/;
  51. let match = pointString.match(regex);
  52. if (!match) {
  53. throw new Error('Invalid POINT format');
  54. }
  55. // 提取匹配到的坐标字符串,并转换为浮点数
  56. let x = parseFloat(match[1]);
  57. let y = parseFloat(match[2]);
  58. // 返回包含x和y的数组
  59. return [x, y];
  60. }
  61. //POLYGON 字符串格式转成二维数组
  62. export function parsePolygon(polygonString) {
  63. const text = polygonString.slice(0,12)
  64. if(text==='MULTIPOLYGON'){
  65. // 使用正则表达式提取所有坐标点
  66. const coordinates = polygonString.match(/[\d\.]+ [\d\.]+/g);
  67. // 将坐标点转换为二维数组
  68. const result = coordinates.map(coord => coord.split(' ').map(Number));
  69. return result
  70. }else{
  71. // 提取坐标部分
  72. const coordinatesString = polygonString.replace("POLYGON Z ((", "").replace("))", "");
  73. // 将坐标字符串拆分为数组
  74. const coordinatesArray = coordinatesString.split(",");
  75. //高度集合
  76. const numbers = []
  77. // 将每个坐标字符串转换为数组,并对第三项向上取整
  78. const result = coordinatesArray.map(coord => {
  79. const [x, y, z] = coord.trim().split(" ").map(Number);
  80. numbers.push(Math.ceil(z))
  81. return [x, y, Math.ceil(z)]; // 对 z 值向上取整
  82. });
  83. const max = Math.max(...numbers); // 获取高度最大值
  84. const updateList = result.map(item =>{
  85. item[2] = max + 12
  86. return item
  87. })
  88. return updateList;
  89. }
  90. }
  91. //通过经纬度查处对应的地址
  92. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  93. export async function getLocation (point){
  94. const type = typeof point
  95. let arr = point
  96. if(type==='string'){
  97. arr = parsePointToArray(point)
  98. }
  99. const locationParams = {
  100. key: MAP_KEY,
  101. location: `${arr[1]},${arr[0]}`,
  102. };
  103. let obj = {}
  104. await VE_API.old_mini_map.location(locationParams).then(({ result }) => {
  105. obj.street = result.address_component?.street
  106. obj.address = result.formatted_addresses?.recommend
  107. ? result.formatted_addresses.recommend
  108. : result.address + "";
  109. });
  110. return obj
  111. }