主题
DataFacade — 数据子门面
通过 app.data 访问数据源和要素管理功能。
数据加载
loadGeoJson(urlOrData, options)
加载 GeoJSON 数据。
ts
loadGeoJson(urlOrData: string | object, options?: { intoLayer?: string }): Promise<FeatureRef[]>| 参数 | 类型 | 描述 |
|---|---|---|
| urlOrData | string | object | GeoJSON URL 或数据对象(必填) |
| intoLayer | string | 目标图层 ID(可选) |
ts
// 从对象加载
const features = await app.data.loadGeoJson({
type: 'FeatureCollection',
features: [],
});
// 从 URL 加载
const features2 = await app.data.loadGeoJson('https://example.com/data.geojson');
// 加载到指定图层
await app.data.loadGeoJson(myData, { intoLayer: 'myLayer' });loadFile(file, options)
加载文件数据。
ts
loadFile(file: File | unknown, options?: { format?: string }): Promise<FeatureRef[]>| 参数 | 类型 | 描述 |
|---|---|---|
| file | File | unknown | 文件对象(必填) |
| format | string | 格式标识(如 'geojson'、'kml') |
ts
const features = await app.data.loadFile(file, { format: 'geojson' });数据源管理
addSource(id, spec)
添加数据源。
ts
addSource(id: string, spec?: object): SourceRef| 参数 | 类型 | 描述 |
|---|---|---|
| id | string | 数据源唯一标识(必填) |
| spec | object | 数据源规格 |
ts
const src = app.data.addSource('src1', { type: 'geojson', data: {} });getSource(id)
获取数据源引用。
ts
getSource(id: string): SourceRef | nullts
const src = app.data.getSource('src1');
if (src) {
src.update({ data: newData });
}removeSource(id)
移除数据源。
ts
removeSource(id: string): voidts
app.data.removeSource('src1');要素管理
addFeatures(layer, features)
向图层添加要素。
ts
addFeatures(layer: string | LayerRef, features: object[]): FeatureRef[]| 参数 | 类型 | 描述 |
|---|---|---|
| layer | string | LayerRef | 目标图层(ID 或引用) |
| features | object[] | 要素数组 |
ts
const refs = app.data.addFeatures('myLayer', [
{ type: 'Feature', geometry: { type: 'Point', coordinates: [116, 39] } },
]);updateFeatures(layer, features)
更新图层中的要素。
ts
updateFeatures(layer: string | LayerRef, features: object[]): voidts
app.data.updateFeatures('myLayer', updatedFeatures);removeFeatures(layer, ids)
从图层移除要素。
ts
removeFeatures(layer: string | LayerRef, ids: Array<string | number>): voidts
app.data.removeFeatures('myLayer', [1, 2, '3']);查询
query(layer, options)
查询图层中的要素。
ts
query(layer: string | LayerRef, options?: { where?: string; geometry?: unknown; spatialRel?: string }): Promise<FeatureRef[]>| 参数 | 类型 | 描述 |
|---|---|---|
| layer | string | LayerRef | 目标图层 |
| where | string | 属性过滤表达式 |
| geometry | unknown | 空间过滤几何 |
| spatialRel | string | 空间关系('intersects'、'contains' 等) |
ts
const results = await app.data.query('myLayer', { where: 'id > 0' });toGeoJson(layer)
导出图层数据为 GeoJSON。
ts
toGeoJson(layer: string | LayerRef): objectts
const geojson = app.data.toGeoJson('myLayer');
// geojson: { type: 'FeatureCollection', features: [...] }SourceRef
数据源引用对象:
| 方法 | 参数 | 说明 |
|---|---|---|
| update(spec) | object | 更新数据源配置 |
| refresh() | - | 刷新数据 |
| remove() | - | 移除数据源 |
ts
src.update({ data: newData });
src.refresh();
src.remove();