1

I'm writing a ArcGIS JS project, in which user will upload KMZ which will be displayed on map, since ArcGIS doesn't have a magic trick to do it, I want to convert KMZ to JSON (or KML) and then display that JSON.

I've already tried https://github.com/sntran/kmz-geojson , but I don't think it works anymore. It says package is moved and asks to install 'toGeojson' Package which doesn't support KMZ.

I've tried doing it with zip.js but stuck at zip.blobreader(blob) is not a constructor. I've tried usong jsZIP but still no luck.

1
  • I want to convert KMZ to JSON (or KML) to convert KMZ to KML just unzip it. Note though that not all KML is vector data, so it might not then all easily convert the KML to JSON.
    – nmtoken
    Commented Jun 6, 2019 at 10:01

2 Answers 2

7

If you include jsZip and toGeoJson in the header of your page and add this to the body

<input type="file" id="f">
<textarea id="output" rows="20" cols="70"></textarea>
<script>
    let getDom = xml => (new DOMParser()).parseFromString(xml, "text/xml")
    let getExtension = fileName => fileName.split(".").pop()

    let getKmlDom = (kmzFile) => {
        var zip = new JSZip()
        return zip.loadAsync(kmzFile)
            .then(zip => {
                let kmlDom = null
                zip.forEach((relPath, file) => {
                    if (getExtension(relPath) === "kml" && kmlDom === null) {
                        kmlDom = file.async("string").then(getDom)
                    }
                })
                return kmlDom || Promise.reject("No kml file found")
            });
    }
    var f = document.getElementById("f")
    f.addEventListener("change", e => {
        let geoJson = getKmlDom(e.target.files[0]).then(kmlDom => {
            let geoJsonObject = toGeoJSON.kml(kmlDom)
            return JSON.stringify(geoJsonObject)
        })
        geoJson.then(gj => document.getElementById("output").innerText = gj)
    })
</script>

you should be able to select a KMZ file and see the converted geoJson in the textbox. You can then test with a few local files. As mentioned by nmtoken and Christiaan Adams the conversion from KML til geojson depends on the exact content of the KML file. I don't know which tags are parsed by the toGeoJson library so you need to do some manual testing.

Here's a working demo JSFiddle.

1
  • Works well on KMZ's from Google Earth Pro -- but keep in mind that some of the fields may be excluded, such as Earth's description with images, links, etc.
    – ashleedawg
    Commented Jul 27, 2022 at 3:21
1

To convert KMZ to KML, all you need to do is unzip it, and extract the KML file that's inside. Every KMZ is simply a ZIP archive containing a KML file, sometimes along with other assets like images, etc. Use your favorite zip software (like pkzip) to open it up and extract the files, or if you're on Windows, rename the .kmz extension to .zip to let the windows file explorer recognize it as a zip file and open it directly.

Converting to JSON is another matter, and depends on what kind of data the zip file contains. If it's simple vector data (points, lines, polygons), then you should be able to use a variety of conversion tools. If it's an image/ground overlay (raster), or if it includes more advanced KML tags (eg: Tour, 3D model, Time, etc.) then you'll have to look at other tools and formats.

2
  • How can I unzip KMZ on web using JavaScript? Commented Jun 6, 2019 at 16:34
  • rename to .zip then extract there will be a doc.kml inside
    – Mapperz
    Commented Jun 6, 2019 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.