JSAPI Three API Reference
    正在准备搜索索引...

    类 DataItem

    数据元素类,表示单个可渲染的数据项

    DataItem是数据源中的基本单元,用于存储和管理单个地理要素的数据。 它支持多种几何类型(点、线、面)及其multi-*变体,并能处理不同投影之间的坐标转换。

    每个DataItem包含:

    • 几何数据:坐标信息
    • 属性数据:与几何关联的业务数据
    • 投影信息:坐标的参考系统
    // 创建一个点要素
    const point = new DataItem({
    type: 'Feature',
    geometry: {
    type: 'Point',
    coordinates: [116.39, 39.9]
    },
    properties: {
    name: '北京',
    value: 100
    }
    });

    // 创建一个线要素
    const line = new DataItem({
    type: 'Feature',
    geometry: {
    type: 'LineString',
    coordinates: [
    [116.39, 39.9],
    [121.47, 31.23]
    ]
    },
    properties: {
    name: '北京-上海',
    value: 500
    }
    });
    索引

    构造函数

    • 创建数据元素实例

      参数

      • feature: any

        几何信息

        • Object: GeoJSON格式的Feature数据
        • Array: 点坐标数组,如[lng, lat]或[lng, lat, alt]
        • Vector3: Three.js的三维向量
      • 可选extraAttributes: object = {}

        附加属性对象

        • 特殊属性'id':用于标识数据元素
        • 特殊属性'crs':源投影信息
      • 可选forceProjected: boolean = false

        是否强制视为已投影坐标

      返回 DataItem

      // 从GeoJSON特征创建
      const item = new DataItem({
      type: 'Feature',
      geometry: {
      type: 'Point',
      coordinates: [116.39, 39.9]
      },
      properties: { name: '北京' }
      });

      // 从坐标数组创建点
      const point = new DataItem([116.39, 39.9]);

      // 从Three.js向量创建
      const vector = new THREE.Vector3(116.39, 39.9, 0);
      const vectorPoint = new DataItem(vector);

    方法

    • 获取指定投影下的坐标

      参数

      • projection: object

        投影对象

      • 可选index: number = 0

        多要素时的索引

      • sourceCoordType: any
      • targetCoordType: any

      返回 any[]

      投影后的坐标

      // 获取Web墨卡托投影下的坐标
      const projection = getProjection('EPSG:3857');
      const coords = dataItem.getProjectedCoordinates(projection);

      // 对于MultiLineString,获取第2条线的坐标
      const lineCoords = dataItem.getProjectedCoordinates(projection, 1);
    • 设置数据项的单个属性

      参数

      • key: string

        属性名

      • value: any

        属性值

      返回 void

      dataItem.setAttribute('name', '北京');
      dataItem.setAttribute('value', 100);
    • 设置数据项的多个属性

      参数

      • attributes: object

        属性对象

      返回 void

      dataItem.setAttributes({
      name: '北京',
      value: 100,
      color: [255, 0, 0]
      });
    • 设置数据项的坐标

      参数

      • coordinates: any[]

        新的坐标数组

      • 可选projection: object

        坐标所属的投影

      返回 void

      // 更新点的位置
      dataItem.setCoordinates([118.28, 40.22]);

      // 设置已投影的坐标
      const projection = getProjection('EPSG:3857');
      dataItem.setCoordinates([12620000, 4830000], projection);
    • 将数据转换为GeoJSON格式

      返回 object

      GeoJSON Feature对象

      const geojson = dataItem.toGeoJSON();
      console.log(geojson.geometry.type); // 'Point'等
      console.log(geojson.properties); // 属性对象

    访问器

    • get attributes(): object

      获取数据项的所有属性

      返回 object

    • get coordinates(): any[]

      获取数据项的经纬度坐标

      返回 any[]

    • get id(): string | number

      获取数据项的ID

      返回 string | number

    • set id(value: string | number): void

      设置数据项的ID,只能设置一次

      参数

      • value: string | number

        新的ID值

      返回 void

    • get isMulti(): boolean

      是否为多要素类型(MultiPoint, MultiLineString, MultiPolygon)

      返回 boolean

    • get isValid(): boolean

      数据项是否有效

      返回 boolean

    • get size(): number

      多要素时的要素数量,单要素时为1

      返回 number

    • get sourceProjectionName(): string

      获取数据项的源投影名称

      返回 string