【发布时间】:2020-03-10 02:02:53
【问题描述】:
我想上传一个 json/shp/kml/kmz 文件并将其转换为 geojson。
HTML
<input type="file" name="uploadfile" accept=".json,.geojson,.shp,.kml,.kmz" id="file_loader" hidden>
<input type="text" id="upload_name" placeholder="Name" aria-label="Name" >
Javascript
var jsonObj;
var fileExt = upload_name.split('.').pop(); // Get file extension
var reader = new FileReader(); // Read file into text
reader.readAsText($('#file_loader')[0].files[0]);
reader.onload = function(event) {
if (fileExt == 'geojson') {
jsonObj = JSON.parse(event.target.result); // Parse text into json
} else if (fileExt == 'shp') {
// how to convert event.target.result to geojson?
} else if (fileExt == 'json') {
//
} else if (fileExt == 'kml') {
//
} else if (fileExt == 'kmz') {
//
}
}
function addToMap(){
// This function add jsonObj as a layer to Mapbox
// No problem here
}
我尝试过使用
https://www.npmjs.com/package/gtran
通过npm install gtran 然后var gtran = require('gtran'); 在我的javascript文件中,但它给了我一个错误:
Could not find a declaration file for module 'gtran'
gtran 没有@types/gtran。
我的 javascript 文件由 webpack 使用 babel-loader 处理。不确定这是否与错误有关。
即使我成功地包含了 gtran,它的函数也需要文件名作为参数。我的数据是字符串而不是文件,我没有文件名。我该怎么办?我是否将字符串保存在一个文件中,然后 gtran 那个文件?
【问题讨论】:
标签: javascript mapbox geojson data-conversion file-conversion