我想上传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在我的javascript文件中按npm install gtran
,然后按var gtran = require('gtran');
,但它给了我一个错误:
Could not find a declaration file for module 'gtran'
gtran没有@ types / gtran。
[我的javascript文件已通过babel-loader被webpack处理。不知道这是否与错误有关。
即使我成功包含了gtran,其功能也需要文件名作为参数。我的数据在字符串中而不是文件中,我没有文件名。我该怎么办?是否将字符串保存到文件中,然后gtran该文件?
您不能在前端javascript文件中使用var gtran = require('gtran');
。在Node.js
中使用。
但是,您可以通过Browserify之类的扩展名使用它。有关更多详细信息,请参见此post。