Skip to content

DataFacade — 数据子门面

通过 app.data 访问数据源和要素管理功能。

数据加载

loadGeoJson(urlOrData, options)

加载 GeoJSON 数据。

ts
loadGeoJson(urlOrData: string | object, options?: { intoLayer?: string }): Promise<FeatureRef[]>
参数类型描述
urlOrDatastring | objectGeoJSON URL 或数据对象(必填)
intoLayerstring目标图层 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[]>
参数类型描述
fileFile | unknown文件对象(必填)
formatstring格式标识(如 'geojson''kml'
ts
const features = await app.data.loadFile(file, { format: 'geojson' });

数据源管理

addSource(id, spec)

添加数据源。

ts
addSource(id: string, spec?: object): SourceRef
参数类型描述
idstring数据源唯一标识(必填)
specobject数据源规格
ts
const src = app.data.addSource('src1', { type: 'geojson', data: {} });

getSource(id)

获取数据源引用。

ts
getSource(id: string): SourceRef | null
ts
const src = app.data.getSource('src1');
if (src) {
  src.update({ data: newData });
}

removeSource(id)

移除数据源。

ts
removeSource(id: string): void
ts
app.data.removeSource('src1');

要素管理

addFeatures(layer, features)

向图层添加要素。

ts
addFeatures(layer: string | LayerRef, features: object[]): FeatureRef[]
参数类型描述
layerstring | LayerRef目标图层(ID 或引用)
featuresobject[]要素数组
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[]): void
ts
app.data.updateFeatures('myLayer', updatedFeatures);

removeFeatures(layer, ids)

从图层移除要素。

ts
removeFeatures(layer: string | LayerRef, ids: Array<string | number>): void
ts
app.data.removeFeatures('myLayer', [1, 2, '3']);

查询

query(layer, options)

查询图层中的要素。

ts
query(layer: string | LayerRef, options?: { where?: string; geometry?: unknown; spatialRel?: string }): Promise<FeatureRef[]>
参数类型描述
layerstring | LayerRef目标图层
wherestring属性过滤表达式
geometryunknown空间过滤几何
spatialRelstring空间关系('intersects''contains' 等)
ts
const results = await app.data.query('myLayer', { where: 'id > 0' });

toGeoJson(layer)

导出图层数据为 GeoJSON。

ts
toGeoJson(layer: string | LayerRef): object
ts
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();

四川省交通运输综合地理服务平台 地图开发框架