How to convert KMZ file to KML using javascript?

送分小仙女□ 提交于 2019-12-06 18:06:27

问题


In am writing JavaScript code for adding kmz,kml and csv file from local folder.All the files will be displayed/added on esri Maps.I am able to add kml and csv, but not kmz.I dont want to include full zip/unzip code or library into my code.I dont need that all.Only need a small code for converting kmz to kml.

Also I don't want to store any unzipped or converted file(which will be kml) in any local folder.

Just want to read the contents and pass it to my add_kml() function on the fly. Only want to extract the contents from kmz file which will be kml. The code available everywhere is to store the extracted file in some folder.I dont want that.

function unZipAndProcessKMZ(blob) {
zip.createReader(new zip.BlobReader(blob),
function(reader) {// get all entries from the zip reader.getEntries( function(entries) { parseTransformKMZ(entries);} ); }, function(error) {/* onerror callback */ } ); }

    function parseTransformKMZ(entries) 
    {
        //Step 1: find doc.kml
        var docKMLentry = findFileByName(entries, '.kml', false);
        console.log('in Parse file: ', docKMLentry.filename);       
        docKMLentry.getData(    new zip.BlobWriter('text/xml'),
        function(kmlText) 
        {

    var fileReader = new FileReader();

                                       fileReader.addEventListener("loadend",  function() 
{                                                docKML = fileReader.result;

if (typeof docKML == 'string') {
docKML = ( new window.DOMParser() ).parseFromString(docKML, "text/xml");
                       }   
    var geojson = toGeoJSON[extension](docKML);                    
            //send data to esri converter
                  feats = geojson.features;
                 if (!feats || (feats.length === 0)) {
                 alert("No features found");
                 return;
        }     convertTo_esri();                                     

});


回答1:


Got the solution- simply passed blob object taken as input file to the zip.blobReader function in zip.js



来源:https://stackoverflow.com/questions/24243254/how-to-convert-kmz-file-to-kml-using-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!