【问题标题】:Upload json/shp/kml/kmz file and convert it into geojson上传json/shp/kml/kmz文件并转换成geojson
【发布时间】: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


    【解决方案1】:

    您不能在前端 javascript 文件中使用 var gtran = require('gtran');。它用于Node.js

    但是,您可以借助 Browserify 之类的扩展名来使用它。有关更多详细信息,请参阅此post

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多