當前位置:
首頁 > 知識 > LeaFlet學習之結合turf.js生成簡單的等值線demo

LeaFlet學習之結合turf.js生成簡單的等值線demo

本文主要結合turf.js生成等值線俺,進行展示效

一、放張圖:

LeaFlet學習之結合turf.js生成簡單的等值線demo

二、全部源碼

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>等值線的生成</title>
  6. <link href="Script/leaflet/leaflet.css" rel="stylesheet" />
  7. <style>
  8. #map
  9. {
  10. height: 2000px;
  11. width: 1500px;
  12. }
  13. </style>
  14. <script src="Script/leaflet/leaflet.js"></script>
  15. <script src="Script/leaflet/leaflet.ChineseTmsProviders.js"></script>
  16. <script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>
  17. </head>
  18. <body>
  19. <div id="map"></div>
  20. </body>
  21. <script>
  22. var normalm = L.tileLayer.chinaProvider("TianDiTu.Normal.Map", {
  23. maxZoom: 18,
  24. minZoom: 1
  25. }),
  26. normala = L.tileLayer.chinaProvider("TianDiTu.Normal.Annotion", {
  27. maxZoom: 18,
  28. minZoom: 1
  29. }),
  30. imgm = L.tileLayer.chinaProvider("TianDiTu.Satellite.Map", {
  31. maxZoom: 18,
  32. minZoom: 1
  33. }),
  34. imga = L.tileLayer.chinaProvider("TianDiTu.Satellite.Annotion", {
  35. maxZoom: 18,
  36. minZoom: 1
  37. });
  38. var normal = L.layerGroup([normalm, normala]);
  39. image = L.layerGroup([imgm, imga]);
  40. var baseLayers = {
  41. "地圖": normal,
  42. "影像": image,
  43. }
  44. var overlayLayers = {
  45. "圖": normal,
  46. "像": image,
  47. }
  48. var map = L.map("map", {
  49. center: [31.59, 120.29],
  50. zoom: 12,
  51. layers: [normal],
  52. zoomControl: false
  53. });
  54. // 創建等值線區域
  55. var extent = [0, 30, 20, 50];
  56. var cellWidth = 100;
  57. var pointGrid = turf.pointGrid(extent, cellWidth, { units: "miles" });
  58. for (var i = 0; i < pointGrid.features.length; i++) {
  59. pointGrid.features[i].properties.temperature = Math.random() * 10;
  60. }
  61. console.log(pointGrid.features.length);
  62. //等值線的級數
  63. var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  64. var lines = turf.isolines(pointGrid, breaks, { zProperty: "temperature" });
  65. //設置顏色
  66. var myStyle = {
  67. "color": "#ff7800",
  68. "weight": 5,
  69. "opacity": 0.65
  70. };
  71. //進行平滑處理
  72. var _lFeatures = lines.features;
  73. for(var i=0;i<_lFeatures.length;i++){
  74. var _coords = _lFeatures[i].geometry.coordinates;
  75. var _lCoords = [];
  76. for(var j=0;j<_coords.length;j++){
  77. var _coord = _coords[j];
  78. var line = turf.lineString(_coord);
  79. var curved = turf.bezierSpline(line);
  80. _lCoords.push(curved.geometry.coordinates);
  81. }
  82. _lFeatures[i].geometry.coordinates = _lCoords;
  83. }
  84. //geojson數據讀取
  85. L.geoJSON(lines, {
  86. style: myStyle
  87. }).addTo(map);
  88. </script>
  89. </html>

三、總結

這裡是等值線,可不是等高線,等高線不會相交,等值線可能相交,數據平滑參考牛老師ol4的等值線效果。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

基於SSH的航空公司票務機票預訂管理系統(前台以及後台)
C 標準性能測試

TAG:程序員小新人學習 |