当前位置: 首页 > news >正文

Cesium中的DataSource和Entity关系

本章主要探讨一下Cesium中的DataSource和Entity。

介绍

首先简单说一下Entity与Primitive。

Cesium为开发者提供了丰富的图形绘制和空间数据管理的API,可以分为两类,一类是面向图形开发人员的低层次API,通常被称为Primitive API,另一类是用于驱动数据可视化的高层次API,称为Entity API。

总的来说,Primitive偏底层,图形绘制和数据加载效率较高,但开发难度更大,而Entity基于Primitive做了进一步封装,调用便捷。

对于绝大多数开发者来说,我们添加各种对象(点、模型、线等)都是使用Entity方式的,通过DataSource加载后其内部也是Entity对象方式的。而Entity最终转换为 Primitive对象来显示的。

  • 添加entity
var viewer = new Cesium.Viewer('cesiumContainer');

var citizensBankPark = viewer.entities.add({
    name : 'Citizens Bank Park',
    position : Cesium.Cartesian3.fromDegrees(-75.166493, 39.9060534),
    point : {
        pixelSize : 5,
        color : Cesium.Color.RED,
        outlineColor : Cesium.Color.WHITE,
        outlineWidth : 2
    },
    label : {
        text : 'Citizens Bank Park',
        font : '14pt monospace',
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        outlineWidth : 2,
        verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
        pixelOffset : new Cesium.Cartesian2(0, -9)
    }
});
  • 添加Primitive
// 1. Draw a translucent ellipse on the surface with a checkerboard pattern
const instance = new Cesium.GeometryInstance({
  geometry : new Cesium.EllipseGeometry({
      center : Cesium.Cartesian3.fromDegrees(-100.0, 20.0),
      semiMinorAxis : 500000.0,
      semiMajorAxis : 1000000.0,
      rotation : Cesium.Math.PI_OVER_FOUR,
      vertexFormat : Cesium.VertexFormat.POSITION_AND_ST
  }),
  id : 'object returned when this instance is picked and to get/set per-instance attributes'
});
scene.primitives.add(new Cesium.Primitive({
  geometryInstances : instance,
  appearance : new Cesium.EllipsoidSurfaceAppearance({
    material : Cesium.Material.fromType('Checkerboard')
  })
}));
  • 添加DataSource
const viewer = new Cesium.Viewer('cesiumContainer');
// 添加 GeoJson类型的数据
viewer.dataSources.add(Cesium.GeoJsonDataSource.load('../../SampleData/ne_10m_us_states.topojson', {
  stroke: Cesium.Color.HOTPINK,
  fill: Cesium.Color.PINK,
  strokeWidth: 3,
  markerSymbol: '?'
}));

//	添加czml类型的数据
viewer.dataSources.add(Cesium.CzmlDataSource.load("../SampleData/simple.czml"));

从以上例子中,我们可以看出:

  1. 添加一个新的Primitive对象是存放在scene中的primitives集合(PrimitiveCollection类)里的;
  2. 添加一个新的Entity对象是存放在viewer里的enitites集合(EntityCollection类)里的;
  3. 添加一个新的DataSource是存放在viewer里的dataSources集合(DataSourceCollection类)里的

DataSource与Entity

DataSource有多种类型文件形式,如czml,GeoJson等,不同的文件类型只是为了不同方式的输入数据结构而已,本质上内部还是转换为Entity对象保存。

我们来看看Viewer里的entites和dataSources属性定义,源代码文件位于:“Source\Widgets\Viewer\Viewer.js”,此处仅列出了关节的代码片段,并添加了部分注释。


//	从构造函数里获取dataSources,如果没有就新建DataSourceCollection对象
var dataSourceCollection = options.dataSources;
var destroyDataSourceCollection = false;
if (!defined(dataSourceCollection)) {
  dataSourceCollection = new DataSourceCollection();
  destroyDataSourceCollection = true;
}

// 新建dataSourceDisplay对象(DataSourceDisplay),并把前面的dataSourceCollection对象作为参数赋值
//	DataSourceDisplay类型里的属性:dataSources实际上就是传进来的dataSourceCollection,
//	也就是说viewer.dataSource和dataSourceDisplay里的dataSource是同一个对象

var dataSourceDisplay = new DataSourceDisplay({
   scene: scene,
   dataSourceCollection: dataSourceCollection,
 });

this._dataSourceCollection = dataSourceCollection;
this._dataSourceDisplay = dataSourceDisplay;

Object.defineProperties(Viewer.prototype, {
	//...
	
  /**
   * Gets the collection of entities not tied to a particular data source.
   * This is a shortcut to [dataSourceDisplay.defaultDataSource.entities]{@link Viewer#dataSourceDisplay}.
   * @memberof Viewer.prototype
   * @type {EntityCollection}
   * @readonly
   */
  entities: {
    get: function () {
      return this._dataSourceDisplay.defaultDataSource.entities;
    },
  },

  /**
   * Gets the set of {@link DataSource} instances to be visualized.
   * @memberof Viewer.prototype
   * @type {DataSourceCollection}
   * @readonly
   */
  dataSources: {
    get: function () {
      return this._dataSourceCollection;
    },
  },
  
   /**
   * Gets the display used for {@link DataSource} visualization.
   * @memberof Viewer.prototype
   * @type {DataSourceDisplay}
   * @readonly
   */
  dataSourceDisplay: {
    get: function () {
      return this._dataSourceDisplay;
    },
  },
  //...

从代码中可以看出,viewer.dataSources与其viewer.dataSourceDisplay.dataSources是同一个对象;
而viewer.entities是viewer._dataSourceDisplay.defaultDataSource.entities属性。

就是说,viewer里的enities和dataSources实际上都是viewer里的dataSourceDisplay里属性。而每个dataSource里都有enties属性。

  • 我们手动通过viewer.entites.add方法添加的所有entity对象都放在viewer.dataSourceDisplay里的defaultDataSource.enities里;
  • 而通过viewer.dataSources.add方法添加方式首先将整个单个dataSource保存在dataSources集合里,而每个dataSource里的所有entity集合都存放在各自的dataSource.entites对象中;

下面是对应的数据结构图
Viewer与DataSource的数据结构

相关文章:

  • 微信小程序图书商城毕业设计,图书商城购物小程序系统设计与实现,图书商城购物小程序商城毕设作品参考
  • 这 13 个可能是你一直未使用过的超级棒的 Python 用法
  • 【数据结构初阶】链式二叉树接口实现+痛苦的OJ题
  • 【Linux】基本指令(三)
  • R语言与RStudio的下载与安装方法
  • java-php-python-ssm网上拍卖系统2021计算机毕业设计
  • 基于JAVA后台微信校园疫情防控小程序系统 开题报告
  • 免费分享一个springboot+vue学生选课管理系统,挺漂亮的
  • 卡塔尔世界杯--程序员的诗和远方
  • 【9种优化算法比较】CGO、SCA、GWO、CSA、SSA、HHO、WOA、PSO、TSO智能优化算法比较(Matlab代码实现)
  • RK3568平台开发系列讲解(音频篇)Android AudioRecord 采集音频
  • 哈希(Hash)
  • Python系列-Django
  • Python一炮句搞定网页登录验证码自动输入
  • MyBatis trim标签起什么作用呢?
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 07.Android之多媒体问题
  • Android Volley源码解析
  • CSS相对定位
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • JavaScript创建对象的四种方式
  • Javascript设计模式学习之Observer(观察者)模式
  • PhantomJS 安装
  • Promise初体验
  • vue学习系列(二)vue-cli
  • Web设计流程优化:网页效果图设计新思路
  • 从零开始在ubuntu上搭建node开发环境
  • 二维平面内的碰撞检测【一】
  • 工作手记之html2canvas使用概述
  • 欢迎参加第二届中国游戏开发者大会
  • 前端之React实战:创建跨平台的项目架构
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 算法系列——算法入门之递归分而治之思想的实现
  • 小程序button引导用户授权
  • 小李飞刀:SQL题目刷起来!
  • 鱼骨图 - 如何绘制?
  • ​2021半年盘点,不想你错过的重磅新书
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​卜东波研究员:高观点下的少儿计算思维
  • #Lua:Lua调用C++生成的DLL库
  • (11)MATLAB PCA+SVM 人脸识别
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (十一)图像的罗伯特梯度锐化
  • (一)u-boot-nand.bin的下载
  • (一)基于IDEA的JAVA基础12
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)Sublime Text3配置Lua运行环境
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉