cesium生成热力图(geojson点数据)

来自基于cesium展示热力图 - 知乎

在此下载GitHub - pa7/heatmap.js: 🔥 JavaScript Library for HTML5 canvas based heatmaps

我是在es6  class类中使用(其他方法请查看原作者)    导入   

const h337 = require("../../public/static/heatmap.js");

 生成画布

class heatmap {
    //你的经纬度
  latMin = 30.670043;
  latMax = 30.685509;
  lonMin = 104.135339;
  lonMax = 104.160886;
  max = 100;
  width = 600;
  height = 400;
  canvas;
  heat;
  carHeat;
  water;
  constructor(options) {
    this.viewer = options.viewer;
  }
  addHeatMap() {
    //点的个数,你的和我不一样,注意修改
    var len = 19;
    var points = [];
    //和value相关,value等于1的话,为max,则有红点
    let _this = this;
    var dataRaw = [];
    var req = new XMLHttpRequest();
    req.open("get", "heatmap.geojson");
    req.send(null);
    req.onload = function () {
      var data = JSON.parse(req.responseText);
      data.features.forEach((feature) => {
        var point = {
          lat: feature.geometry.coordinates[1],
          lon: feature.geometry.coordinates[0],
           //这里我再geojson中手动添加了coordinates[2],类似于z值
          value: feature.geometry.coordinates[2],
        };
        dataRaw.push(point);
      });
      for (var i = 0; i < len; i++) {
        var dataItem = dataRaw[i];
        var point = {
          x: Math.floor(
            ((dataItem.lon - _this.lonMin) / (_this.lonMax - _this.lonMin)) *
              _this.width
          ),
            //y和原博主不一样,解决偏移问题,方法来自原博主评论区
          y: Math.floor(
            _this.height -
              ((dataItem.lat - _this.latMin) / (_this.latMax - _this.latMin)) *
                _this.height
          ),
          value: Math.floor(dataItem.value),
        };
        _this.max = Math.max(_this.max, dataItem.value);
        points.push(point);
      }

      var heatmapInstance = h337.create({
        container: document.querySelector("#heatmap"),
      });

      var data = {
        max: _this.max,
        data: points,
      };
      //
      heatmapInstance.setData(data);

      _this.viewer._cesiumWidget._creditContainer.style.display = "none";

      _this.canvas = document.getElementsByClassName("heatmap-canvas");
      console.log(_this.canvas);
    };
  }

 以entitie形式添加热力图

因为我不想每次都创建画布,所以我分开写,点击按钮时执行移除和添加就行

(会出现问题,创建画布需要时间,若执行完addHeatmap马上执行addHeat会报错,自行解决)

addHeat() {
    this.heat = this.viewer.entities.add({
      name: "heatmap",
      rectangle: {
        coordinates: Cesium.Rectangle.fromDegrees(
          this.lonMin,
          this.latMin,
          this.lonMax,
          this.latMax
        ),
        material: new Cesium.ImageMaterialProperty({
          image: this.canvas[0],
          transparent: true,
        }),
      },
    });
    console.log(this.canvas);
  }
  removeHeat() {
    this.viewer.entities.remove(this.heat);
  }

效果图

 若要实现第二种,需要修改heatmap.js源码

 

13为半径,下方为颜色

记录遇到的坑,搞了一下午加晚上,先一直出现getcomputedstyle什么的报错,没解决,于是换了一种方法,有同学解决了可以告诉我,谢谢。

在qgis中打的点导出为geojson之后,明明改了坐标系为wgs84(大地坐标),导出后还是笛卡尔坐标,非常气恼,最后把打点的底层图层修改为了wgs84,再创建shp图层,才成功。

最后注意length的问题,注意加一或减一。

 本人小白,有错请谅解,有问题尽量解答。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Cesium中使用GroundPrimitive加载GeoJSON线数据,可以按照以下步骤进行操作: 1. 准备数据:将GeoJSON线数据准备好,并确保其符合GeoJSON规范。 2. 创建GroundPrimitive:使用Cesium的GroundPrimitive类创建一个实例。GroundPrimitive类可以用于在地面上呈现几何图形,例如线、面等。 3. 加载数据:使用CesiumGeoJsonDataSource类将GeoJSON数据加载到GroundPrimitive中。 4. 显示数据:将GroundPrimitive添加到Cesium的场景中即可显示数据。 以下是一个示例代码,演示如何加载GeoJSON线数据: ```javascript // 准备数据 var geojsonData = { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [-74.0059, 40.7128], [-77.0369, 38.9072] ] }, "properties": { "name": "New York to Washington DC" } } ] }; // 创建GroundPrimitive var groundPrimitive = new Cesium.GroundPrimitive({ geometryInstances: [] }); // 加载数据 var dataSource = Cesium.GeoJsonDataSource.load(geojsonData); dataSource.then(function(dataSource) { var entities = dataSource.entities.values; for (var i = 0; i < entities.length; i++) { var entity = entities[i]; var geometryInstance = new Cesium.GeometryInstance({ geometry: new Cesium.PolylineGeometry({ positions: entity.polyline.positions.getValue(), width: 5.0 }) }); groundPrimitive.geometryInstances.push(geometryInstance); } }); // 添加到场景中 viewer.scene.primitives.add(groundPrimitive); ``` 在该示例代码中,我们首先准备了一个包含一条线的GeoJSON数据。然后创建了一个GroundPrimitive实例,并使用GeoJsonDataSource类将数据加载到其中。最后,将GroundPrimitive添加到场景中即可显示数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值