Newer
Older
KaiFengPC / src / utils / gis / rainkriging.js
@zhangdeliang zhangdeliang on 23 May 3 KB 初始化项目
  1. const ATTRIBUTES = {
  2. kriging: {
  3. defaultConfig: {
  4. fields: { value: 'value', color: 'color' },
  5. model: 'spherical', //model还可选'gaussian','spherical',exponential
  6. sigma: 0,
  7. alpha: 100,
  8. levels: [
  9. { value: 0, color: 'rgba(169,240,145,0.4)' },
  10. { value: 5, color: 'rgba(108, 208, 89,0.4)' },
  11. { value: 10, color: 'rgba(62,185,63,0.4)' },
  12. { value: 15, color: 'rgba(80, 191, 167,0.4)' },
  13. { value: 25, color: 'rgba(97,184,253,0.4)' },
  14. { value: 35, color: 'rgba(0, 117, 255,0.4)' },
  15. { value: 50, color: 'rgba(1,0,253,0.4)' },
  16. { value: 75, color: 'rgba(172, 42, 254,0.4)' },
  17. { value: 100, color: 'rgba(250,0,252,0.4)' },
  18. ],
  19. },
  20. },
  21. };
  22.  
  23. /***
  24. * 根据点位信息获取空间插值GeoJson
  25. * @param geojson
  26. * @param boundary 边界范围
  27. * @param options kriging 相关参数
  28. * @returns {VectorLayer<VectorSourceType>}
  29. */
  30. export function getKrigingByPoints(geojson, boundary, options = {}) {
  31. let [levels, colors] = [[], []];
  32. geojson = geojson || { features: [] };
  33. options.model = options.model || ATTRIBUTES.kriging.defaultConfig.model;
  34. options.sigma = options.sigma || ATTRIBUTES.kriging.defaultConfig.sigma;
  35. options.alpha = options.alpha || ATTRIBUTES.kriging.defaultConfig.alpha;
  36. options.levels = options.levels || ATTRIBUTES.kriging.defaultConfig.levels;
  37. options.fields = options.fields || ATTRIBUTES.kriging.defaultConfig.fields;
  38. options.levels = _.sortBy(options.levels, level => Number(level[options.fields.value]));
  39.  
  40. options.levels.forEach(level => {
  41. levels.push(level[options.fields.value]);
  42. colors.push(level[options.fields.color]);
  43. });
  44.  
  45. const gridFeatureCollection = grid => {
  46. let range = grid.zlim[1] - grid.zlim[0];
  47. let i, j, x, y, z;
  48. let n = grid.length; // 列数
  49. let m = grid[0].length; //行数
  50. let pointArray = [];
  51. let sum = levels.reduce((a, b) => a + b);
  52. for (i = 0; i < n; i++) {
  53. for (j = 0; j < m; j++) {
  54. x = i * grid.width + grid.xlim[0];
  55. y = j * grid.width + grid.ylim[0];
  56. // z = (grid[i][j] - grid.zlim[0]) / range;
  57. // if (z < 0.0) z = 0.0
  58. // if (z > 1.0) z = 1.0
  59. pointArray.push(turf.point([x, y], { value: grid[i][j] || 0 }));
  60. }
  61. }
  62. return pointArray;
  63. };
  64. const randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
  65. let [lngs, lats, values] = [[], [], []];
  66. if (!!!geojson.features.length)
  67. geojson = turf.randomPoint(100, {
  68. bbox: [113.75048547379058, 30.181701862639972, 114.23892734382642, 30.545337595017667],
  69. });
  70. geojson.features.forEach(point => {
  71. let arr = turf.getCoord(point);
  72. lngs.push(arr[0]);
  73. lats.push(arr[1]);
  74. //values.push(point.properties[options.fields.value] ?? randomInteger(0, 100));
  75. values.push(point.properties[options.fields.value]);
  76. });
  77.  
  78. let extent = turf.bbox(boundary);
  79. let krigingTrain = kriging.train(values, lngs, lats, options.model, options.sigma, options.alpha);
  80. let krigingGrid = kriging.grid(turf.getCoords(boundary.features[0]), krigingTrain, (extent[2] - extent[0]) / 200);
  81. let collection = turf.featureCollection(gridFeatureCollection(krigingGrid));
  82. let isobands = turf.isobands(collection, levels, { zProperty: 'value' });
  83. isobands.features.forEach(
  84. feature =>
  85. (feature.properties.color =
  86. colors[levels.indexOf(Number(feature.properties.value.split('-')[feature.properties.value.split('-').length - 2]))])
  87. );
  88. isobands.features.push(turf.polygon(turf.getCoords(boundary.features[0]), { color: colors[0] }));
  89. isobands.features.sort((a, b) => turf.area(b) - turf.area(a));
  90. return isobands;
  91. }