1. FileUtil
1.1. new FileUtil()
常用文件资源处理工具类
1.2. 方法
1.2.1. FileUtil.createOrUpdateSnapShot(id, width, height, [canvas]) ⇒ Promise
创建当前地图视图快照,然后存入浏览器缓存中(indexDB)
Param | Type | Default | Description |
---|---|---|---|
id | String |
快照id |
|
width | Number |
图片缩放宽 |
|
height | Number |
图片缩放高 |
|
[canvas] | Object |
geomap.viewer.canvas |
HTMLCanvasElement对象,默认采用当前实例化的地图对象 |
Returns : Promise
-
返回promise
Example
let id = BOSGeo.Util.generateUUID();
BOSGeo.FileUtil.createOrUpdateSnapShot(id, 1920, 1080).then(() => {
console.log("存储成功");
})
.otherwise((e) => {
console.log("存储失败", e);
});
1.2.2. FileUtil.getSnapShot(id)
从浏览器缓存(indexDB)中获取特定快照
Param | Type | Description |
---|---|---|
id | String |
快照id |
Returns : Promise
-
返回promise
Example
BOSGeo.FileUtil.getSnapShot(id);
1.2.3. FileUtil.deleteSnapShot(id)
从浏览器缓存(indexDB)中删除特定快照
Param | Type | Description |
---|---|---|
id | String |
快照id |
Example
BOSGeo.FileUtil.deleteSnapShot(id);
1.2.4. FileUtil.exportCurrentSnapShot([options], [canvas]) ⇒ Object
导出当前地图视图的快照,可以保存图片到本地也可以返回base64的图片对象
Param | Type | Default | Description |
---|---|---|---|
[options] | Object |
包含以下参数的Object对象: |
|
[options.name] | String |
"未命名快照" |
快照名称; |
[options.type] | String |
ImageType.PNG |
导出格式,默认为png; |
[options.imageSize] | Array.<Number> |
图片长宽尺寸,默认为当前地图视图窗口大小。单位为像素,如[1920,1080]表示尺寸为1920*1080(像素)的图片。 |
|
[canvas] | Object |
geomap.viewer.canvas |
HTMLCanvasElement对象,默认采用当前实例化的地图对象 |
Returns : Object
-
返回base64的图片对象
Example
BOSGeo.FileUtil.exportCurrentSnapShot({type: BOSGeo.ImageType.JPEG});//导出jpeg格式的快照图片
1.2.5. FileUtil.saveJSON(data, filename)
导出json文件
Param | Type | Description |
---|---|---|
data | Object |
要导出的数据 |
filename | String |
要导出的文件名称 |
Example
let person = {
name: 'james',
age: 20
}
BOSGeo.FileUtil.saveJSON(person, 'per');
1.2.6. FileUtil.addJsonDataSource() ⇒ Promise
通过文件选择框导入json文件数据,需要通过另一个按钮的click事件触发调用该方法
Returns : Promise
-
包含json文件中数据信息的promise对象
Example
document.getElementById('demo').onclick = () => {
let dataPromise = BOSGeo.FileUtil.addJsonDataSource();
dataPromise.then(data => {
console.log(data);
})
}
1.2.7. FileUtil.parseShp(shpZipPath )
解析shp文件的Zip压缩包
Returns : geojson<Promise>
-
解析后的geojson,异步
Param | Type | Description |
---|---|---|
shpZipPath | String |
shp文件的Zip压缩包 |
Example
//创建jsonLayer图层
window.jsonLayer = layerManager.createLayer(BOSGeo.LayerType.GEOJSON, 'GEOJSON123', {customGroupId: 'GEOJSON1'});
let shpZipPath = 'https://bosgeo.boswinner.com/geoData/shp/shenzhenShp.zip';
BOSGeo.FileUtil.parseShp(shpZipPath).then(function (geojson) {
if (geojson) {
let json1 = jsonLayer.add({
url: geojson,
name: s + 'json',
zoomToTarget: true,
opacity: 0.5,
showPolyline: true,
extrudedHeightField: 10,
showLabelField: "name",
showPolygon: true,
showDynamicColorPolygon: true
});
//数据加载成功后的回调
jsonLayer.on(BOSGeo.LayerEventType.ADD, data => {
console.log("数据加载成功", data);
})
jsonLayer.zoomToLayer(); //缩放至模型
}
});
1.2.8. FileUtil.autoDetectSource(source, sourceType) ⇒ Promise
自动检测CZML、GeoJSON和KML资源文件,返回Promise对象
Returns : Promise
-
对数据解析后的Promise,异步
Param | Type | Description |
---|---|---|
source | String |
CZML / GeoJSON / KML数据源加载的url地址。 |
sourceType | String |
数据源类型:'czml'、'geojson'、'kml' |
Example
let loadPromise = BOSGeo.FileUtil.autoDetectSource('https://geoserver-alpha.boswinner.com/BOSGeo/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=BOSGeo%3Ahighway_primary&maxFeatures=50&outputFormat=application%2Fjson', 'geojson');
loadPromise.then((dataSource)=>{
viewer.dataSources.add(dataSource);
let entities = dataSource.entities.values
for (let i = 0; i < entities.length; i++) {
let entity = entities[i]
}
})
1.2.9. FileUtil.getDegreesFromGeojsonWFS(url) ⇒ Promise
从geojson或者WFS的服务中获取经纬度坐标信息
Kind: static method of FileUtil
Returns: Promise
-
含位置坐标信息的promise对象
Param | Type | Description |
---|---|---|
url | String |
GeoJSON / WFS数据源加载的url地址。 |
Example
let promise = new BOSGeo.FileUtil.getDegreesFromGeojsonWFS(url);
promise.then(data => {
console.log(data);
})