Skip to content

Commit 7488bea

Browse files
committed
添加本地数据微件支持csv,excel数据格式。
1 parent 8dbb495 commit 7488bea

File tree

10 files changed

+501
-55
lines changed

10 files changed

+501
-55
lines changed

dist/leaflet/iclient9-leaflet-es6.js

Lines changed: 157 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62859,7 +62859,6 @@ class fileModel_FileModel {
6285962859
let widgetsUtil = {
6286062860
/**
6286162861
* 获取上传文件类型
62862-
*
6286362862
* @param fileName
6286462863
*/
6286562864
getFileType(fileName) {
@@ -62874,7 +62873,52 @@ let widgetsUtil = {
6287462873
return FileTypes.GEOJSON;
6287562874
}
6287662875
return null;
62876+
},
62877+
62878+
/**
62879+
* 判断是否地理X坐标
62880+
*
62881+
* @param data
62882+
*/
62883+
isXField(data) {
62884+
var lowerdata = data.toLowerCase();
62885+
return (lowerdata === "x" || lowerdata === "smx" ||
62886+
lowerdata === "jd" || lowerdata === "经度" || lowerdata === "东经" || lowerdata === "longitude" ||
62887+
lowerdata === "lot" || lowerdata === "lon" || lowerdata === "lng");
62888+
},
62889+
62890+
/**
62891+
* 判断是否地理Y坐标
62892+
*
62893+
* @param data
62894+
*/
62895+
isYField(data) {
62896+
var lowerdata = data.toLowerCase();
62897+
return (lowerdata === "y" || lowerdata === "smy" ||
62898+
lowerdata === "wd" || lowerdata === "纬度" || lowerdata === "北纬" ||
62899+
lowerdata === "latitude" || lowerdata === "lat");
62900+
},
62901+
/**
62902+
* 字符串转为dataEditor 支持的csv格式数据
62903+
* @param string
62904+
* @param withoutTitle
62905+
*/
62906+
string2Csv(string, withoutTitle) {
62907+
// let rows = string.split('\r\n');
62908+
let rows = string.split('\n');
62909+
let result = {};
62910+
if (!withoutTitle) {
62911+
result["colTitles"] = rows[0].split(',');
62912+
} else {
62913+
result["colTitles"] = [];
62914+
}
62915+
result['rows'] = [];
62916+
for (let i = (withoutTitle) ? 0 : 1; i < rows.length; i++) {
62917+
rows[i] && result['rows'].push(rows[i].split(','));
62918+
}
62919+
return result;
6287762920
}
62921+
6287862922
};
6287962923
// EXTERNAL MODULE: external "function(){try{return XLSX}catch(e){return {}}}()"
6288062924
var external_function_try_return_XLSX_catch_e_return_ = __webpack_require__(24);
@@ -79088,13 +79132,19 @@ class openFileViewModel_OpenFileViewModel {
7908879132
}
7908979133
}
7909079134

79091-
selectFileOnchange(e) {
79135+
/**
79136+
* @function L.supermap.widgets.OpenFileViewModel.prototype.selectFileOnchange
79137+
* @description 选中文件并加载到底图
79138+
* @param e
79139+
* @return {boolean}
79140+
*/
79141+
selectFileLoadToMap(e) {
7909279142
let inputDom = e.target;
7909379143
let file = inputDom.files[0];
7909479144
//文件大小限制
7909579145
if (file.size > this.fileModel.FileConfig.fileMaxSize) {
7909679146
//todo 这里都用wegit?
79097-
alert("文件最大支持10M数据");
79147+
alert("File supports up to 10M.");
7909879148
return false;
7909979149
}
7910079150

@@ -79103,7 +79153,7 @@ class openFileViewModel_OpenFileViewModel {
7910379153
let fileType = widgetsUtil.getFileType(fileName);
7910479154
//文件格式不支持
7910579155
if (!fileType) {
79106-
alert("文件最大支持10M数据");
79156+
alert("Unsupported data type.");
7910779157
return false;
7910879158
}
7910979159
//文件类型限制
@@ -79118,28 +79168,123 @@ class openFileViewModel_OpenFileViewModel {
7911879168
fileType: fileType
7911979169
});
7912079170
//响应选中文件添加到地图
79121-
this.loadData();
79171+
this._loadData();
7912279172
}
7912379173
}
7912479174

7912579175
/**
79126-
* 加载数据
79176+
* @function L.supermap.widgets.OpenFileViewModel.prototype._loadData
79177+
* @description 加载数据
79178+
* @private
7912779179
*/
79128-
loadData() {
79180+
_loadData() {
7912979181
//todo 需要测试另外两个
7913079182
const me = this;
79131-
FileReaderUtil.readFile(this.fileModel.loadFileObject.fileType, {
79183+
const type = this.fileModel.loadFileObject.fileType;
79184+
FileReaderUtil.readFile(type, {
7913279185
file: this.fileModel.loadFileObject.file,
7913379186
path: this.fileModel.loadFileObject.filePath
7913479187
}, (data) => {
79135-
const result = JSON.parse(data);
79136-
const layer = external_L_default.a.geoJSON(result).addTo(me.fileModel.map);
79137-
me.fileModel.map.flyToBounds(layer.getBounds());
79188+
//将数据统一转换为 geoJson 格式加载到底图
79189+
me._newLayerToMap(me._processDatas(type, data));
7913879190
}, (error) => {
7913979191
throw new Error("Incorrect data format: " + error);
7914079192
}, this);
7914179193
}
7914279194

79195+
/**
79196+
* @function L.supermap.widgets.OpenFileViewModel.prototype._newLayerToMap
79197+
* @description 将数据创建为图层并加载到底图
79198+
* @param geojson
79199+
* @private
79200+
*/
79201+
_newLayerToMap(geojson) {
79202+
const layer = external_L_default.a.geoJSON(geojson);
79203+
this.fileModel.map.flyToBounds(layer.getBounds());
79204+
layer.addTo(this.fileModel.map);
79205+
}
79206+
79207+
/**
79208+
* @function L.supermap.widgets.OpenFileViewModel.prototype._processDatas
79209+
* @description 将读取回来得数据统一处理为 geoJson 格式
79210+
* @param type
79211+
* @param data
79212+
* @return {*}
79213+
* @private
79214+
*/
79215+
_processDatas(type, data) {
79216+
//数据处理
79217+
if (type === "EXCEL" || type === "CSV") {
79218+
return this._processExcelData(data);
79219+
} else if (type === 'JSON' || type === 'GEOJSON') {
79220+
let geojson = null;
79221+
let result = data;
79222+
79223+
//geojson、json未知,通过类容来判断
79224+
if ((typeof result) === "string") {
79225+
result = JSON.parse(result);
79226+
}
79227+
if (result.type === 'ISERVER') {
79228+
geojson = result.data.recordsets[0].features;
79229+
} else if (result.type === 'FeatureCollection') {
79230+
//geojson
79231+
geojson = result;
79232+
} else {
79233+
//不支持数据
79234+
throw new Error("Unsupported data type.");
79235+
// return false;
79236+
}
79237+
return geojson;
79238+
} else {
79239+
throw new Error("Unsupported data type.");
79240+
}
79241+
}
79242+
79243+
/**
79244+
* @function L.supermap.widgets.OpenFileViewModel.prototype._processExcelData
79245+
* @description 表格形式数据处理
79246+
* @param data
79247+
* @private
79248+
*/
79249+
_processExcelData(data) {
79250+
//处理为对象格式转化
79251+
let dataContent = widgetsUtil.string2Csv(data);
79252+
let fieldCaptions = dataContent.colTitles;
79253+
79254+
//位置属性处理
79255+
let xfieldIndex = -1,
79256+
yfieldIndex = -1;
79257+
for (let i = 0, len = fieldCaptions.length; i < len; i++) {
79258+
if (widgetsUtil.isXField(fieldCaptions[i])) {
79259+
xfieldIndex = i;
79260+
}
79261+
if (widgetsUtil.isYField(fieldCaptions[i])) {
79262+
yfieldIndex = i;
79263+
}
79264+
}
79265+
// feature 构建后期支持坐标系 4326/3857
79266+
let features = [];
79267+
for (let i = 0, len = dataContent.rows.length; i < len; i++) {
79268+
let row = dataContent.rows[i];
79269+
//if (featureFrom === "LonLat") {
79270+
let x = Number(row[xfieldIndex]),
79271+
y = Number(row[yfieldIndex]);
79272+
79273+
let point = external_L_default.a.point(x, y);
79274+
79275+
//属性信息
79276+
let attributes = {};
79277+
for (let index in dataContent.colTitles) {
79278+
let key = dataContent.colTitles[index];
79279+
attributes[key] = dataContent.rows[i][index];
79280+
}
79281+
79282+
let feature = external_L_default.a.supermap.themeFeature(point, attributes);
79283+
features.push(feature.toFeature());
79284+
}
79285+
let format = new GeoJSON_GeoJSON();
79286+
return JSON.parse(format.write(features));
79287+
}
7914379288
}
7914479289

7914579290
external_L_default.a.supermap.widgets.OpenFileViewModel = openFileViewModel_OpenFileViewModel;
@@ -79192,8 +79337,7 @@ var OpenFileView = external_L_default.a.Control.extend({
7919279337
this.fileInput.type = "file";
7919379338
this.fileInput.accept = ".json,.geojson,.csv,.xlsx,.xls,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
7919479339

79195-
//todo 解释一下bind
79196-
this.fileInput.onchange = this.viewModel.selectFileOnchange.bind(this.viewModel);
79340+
this.fileInput.onchange = this.viewModel.selectFileLoadToMap.bind(this.viewModel);
7919779341

7919879342
return uploadContent;
7919979343
}

dist/leaflet/iclient9-leaflet-es6.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)