简体   繁体   English

上传json/shp/kml/kmz文件并转换成geojson

[英]Upload json/shp/kml/kmz file and convert it into geojson

I want to upload a json/shp/kml/kmz file and convert it into geojson.我想上传一个 json/shp/kml/kmz 文件并将其转换为 geojson。

HTML 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 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
}

I've tried using https://www.npmjs.com/package/gtran by npm install gtran and then var gtran = require('gtran');我试过使用https://www.npmjs.com/package/gtran by npm install gtran然后var gtran = require('gtran'); in my javascript file, but it gives me an error:在我的 javascript 文件中,但它给了我一个错误:

Could not find a declaration file for module 'gtran'

gtran doesn't have a @types/gtran. gtran 没有@types/gtran。

My javascript file gets processed by webpack with babel-loader.我的 javascript 文件由带有 babel-loader 的 webpack 处理。 Not sure if that has anything to do with the error.不确定这是否与错误有关。

Even if I successfully include gtran, its functions need filename as argument.即使我成功地包含了 gtran,它的函数也需要文件名作为参数。 My data is in a string and not a file, I don't have a filename.我的数据是字符串而不是文件,我没有文件名。 What do I do?我该怎么办? Do I save the string in a file and then gtran that file?我是否将字符串保存在一个文件中,然后 gtran 那个文件?

You can't use var gtran = require('gtran');你不能使用var gtran = require('gtran'); in your frontend javascript file.在您的前端 javascript 文件中。 It is used in Node.js .它用于Node.js

However, you can use it with the aid of an extension like Browserify .但是,您可以借助Browserify之类的扩展来使用它。 See this post for more details.有关更多详细信息,请参阅此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM