d3HexbinLayer.html 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_d3HexbinLayer"></title>
  9. <script type="text/javascript" include="jquery,randomcolor" src="../js/include-web.js"></script>
  10. <script type="text/javascript" include="d3,d3-hexbin,d3Layer" src="../../dist/leaflet/include-leaflet.js"></script>
  11. </head>
  12. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  13. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  14. <script type="text/javascript">
  15. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  16. var map, resultLayer, randomColors = [], notfirst = false,
  17. baseUrl = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
  18. url = host + "/iserver/services/data-jingjin/rest/data";
  19. map = L.map('map', {
  20. preferCanvas: true,
  21. crs: L.CRS.EPSG4326,
  22. center: [39.79, 116.85],
  23. maxZoom: 18,
  24. zoom: 7
  25. });
  26. L.supermap.tiledMapLayer(baseUrl).addTo(map);
  27. query();
  28. function query() {
  29. var sqlParam = new SuperMap.GetFeaturesBySQLParameters({
  30. queryParameter: {
  31. name: "Jingjin",
  32. attributeFilter: "SMID >= -1"
  33. },
  34. datasetNames: ["Jingjin:Town_P"],
  35. fromIndex: 0,
  36. toIndex: 600
  37. });
  38. L.supermap.featureService(url).getFeaturesBySQL(sqlParam, function (serviceResult) {
  39. var points = [];
  40. serviceResult.result.features.features.map(function (feature) {
  41. var point = map.latLngToLayerPoint(L.latLng(feature.geometry.coordinates[1], feature.geometry.coordinates[0]));
  42. points.push([point.x, point.y, feature.properties.NAME]);
  43. });
  44. var d3Layer = L.supermap.d3Layer(function (sel, proj, level) {
  45. if (!notfirst) {
  46. notfirst = true;
  47. } else {
  48. return;
  49. }
  50. var svg = sel;
  51. var margin = {top: 0, right: 0, bottom: 0, left: 0},
  52. width = +map.getSize().x - margin.left - margin.right,
  53. height = +map.getSize().y - margin.top - margin.bottom,
  54. g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  55. var hexbin = d3.hexbin()
  56. .radius(30)
  57. .extent([[0, 0], [width, height]]);
  58. g.append("clipPath")
  59. .attr("id", "clip")
  60. .append("rect")
  61. .attr("width", width)
  62. .attr("height", height);
  63. if (randomColors.length === 0) {
  64. randomColors = randomColor({
  65. luminosity: 'bright',
  66. hue: 'random',
  67. alpha: 0.6,
  68. format: 'rgba',
  69. count: hexbin.hexagon().length
  70. });
  71. }
  72. g.append("g")
  73. .attr("class", "hexagon")
  74. .attr("clip-path", "url(#clip)")
  75. .selectAll("path")
  76. .data(hexbin(points))
  77. .enter().append("path")
  78. .attr("d", hexbin.hexagon())
  79. .attr("transform", function (d) {
  80. return "translate(" + d.x + "," + d.y + ")";
  81. })
  82. .attr("fill", function (d, index) {
  83. return randomColors[index];
  84. });
  85. });
  86. d3Layer.addTo(map);
  87. });
  88. }
  89. </script>
  90. </body>
  91. </html>