@@ -62859,7 +62859,6 @@ class fileModel_FileModel {
62859
62859
let widgetsUtil = {
62860
62860
/**
62861
62861
* 获取上传文件类型
62862
- *
62863
62862
* @param fileName
62864
62863
*/
62865
62864
getFileType(fileName) {
@@ -62874,7 +62873,52 @@ let widgetsUtil = {
62874
62873
return FileTypes.GEOJSON;
62875
62874
}
62876
62875
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;
62877
62920
}
62921
+
62878
62922
};
62879
62923
// EXTERNAL MODULE: external "function(){try{return XLSX}catch(e){return {}}}()"
62880
62924
var external_function_try_return_XLSX_catch_e_return_ = __webpack_require__(24);
@@ -79088,13 +79132,19 @@ class openFileViewModel_OpenFileViewModel {
79088
79132
}
79089
79133
}
79090
79134
79091
- selectFileOnchange(e) {
79135
+ /**
79136
+ * @function L.supermap.widgets.OpenFileViewModel.prototype.selectFileOnchange
79137
+ * @description 选中文件并加载到底图
79138
+ * @param e
79139
+ * @return {boolean}
79140
+ */
79141
+ selectFileLoadToMap(e) {
79092
79142
let inputDom = e.target;
79093
79143
let file = inputDom.files[0];
79094
79144
//文件大小限制
79095
79145
if (file.size > this.fileModel.FileConfig.fileMaxSize) {
79096
79146
//todo 这里都用wegit?
79097
- alert("文件最大支持10M数据 ");
79147
+ alert("File supports up to 10M. ");
79098
79148
return false;
79099
79149
}
79100
79150
@@ -79103,7 +79153,7 @@ class openFileViewModel_OpenFileViewModel {
79103
79153
let fileType = widgetsUtil.getFileType(fileName);
79104
79154
//文件格式不支持
79105
79155
if (!fileType) {
79106
- alert("文件最大支持10M数据 ");
79156
+ alert("Unsupported data type. ");
79107
79157
return false;
79108
79158
}
79109
79159
//文件类型限制
@@ -79118,28 +79168,123 @@ class openFileViewModel_OpenFileViewModel {
79118
79168
fileType: fileType
79119
79169
});
79120
79170
//响应选中文件添加到地图
79121
- this.loadData ();
79171
+ this._loadData ();
79122
79172
}
79123
79173
}
79124
79174
79125
79175
/**
79126
- * 加载数据
79176
+ * @function L.supermap.widgets.OpenFileViewModel.prototype._loadData
79177
+ * @description 加载数据
79178
+ * @private
79127
79179
*/
79128
- loadData () {
79180
+ _loadData () {
79129
79181
//todo 需要测试另外两个
79130
79182
const me = this;
79131
- FileReaderUtil.readFile(this.fileModel.loadFileObject.fileType, {
79183
+ const type = this.fileModel.loadFileObject.fileType;
79184
+ FileReaderUtil.readFile(type, {
79132
79185
file: this.fileModel.loadFileObject.file,
79133
79186
path: this.fileModel.loadFileObject.filePath
79134
79187
}, (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));
79138
79190
}, (error) => {
79139
79191
throw new Error("Incorrect data format: " + error);
79140
79192
}, this);
79141
79193
}
79142
79194
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
+ }
79143
79288
}
79144
79289
79145
79290
external_L_default.a.supermap.widgets.OpenFileViewModel = openFileViewModel_OpenFileViewModel;
@@ -79192,8 +79337,7 @@ var OpenFileView = external_L_default.a.Control.extend({
79192
79337
this.fileInput.type = "file";
79193
79338
this.fileInput.accept = ".json,.geojson,.csv,.xlsx,.xls,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
79194
79339
79195
- //todo 解释一下bind
79196
- this.fileInput.onchange = this.viewModel.selectFileOnchange.bind(this.viewModel);
79340
+ this.fileInput.onchange = this.viewModel.selectFileLoadToMap.bind(this.viewModel);
79197
79341
79198
79342
return uploadContent;
79199
79343
}
0 commit comments