🏠 Home 

GeoKMZer

geoKMZer is a JavaScript library designed to convert KMZ into KML files, use with GeoKMLer to convert to GeoJSON.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/527113/1538395/GeoKMZer.js


Ask a question, post a review, or report the script.
Author
JS55CT
Version
1.1.0
Created
2025-02-16
Updated
2025-02-16
Size
8.98 KB
License
MIT

GeoKMLer / GeoKMZer

A JavaScript library comprising two main components: GeoKMLer and GeoKMZer. These tools are designed to facilitate the conversion of KML data into GeoJSON format and handle the extraction of KML data from KMZ archives efficiently.

License

This project is free software licensed under the MIT License. See the LICENSE file for more details.

Overview

GeoKMLer / GeoKMZer provides a comprehensive API to parse, extract, and convert KML/KMZ files into a GeoJSON FeatureCollection. It is suitable for use in geographic data visualization and web mapping applications.

Features

Usage

GeoKMLer / GeoKMZer makes it easy to work with both KML files directly and KMZ archives. Here’s how you can use these tools in your application:

Example for GeoKMLer

var geoKMLer = new GeoKMLer(); // Create a new instance of GeoKMLer
// Sample KML data input
const kmlData = `...KML data string...`;
// Parse the KML data
const xmlDoc = geoKMLer.read(kmlData);
// Convert to GeoJSON
const geoJson = geoKMLer.toGeoJSON(xmlDoc);
console.log(geoJson);

Example for GeoKMZer

(async () => {
// Assume your KMZ file is loaded and represented as `fileBuffer`
const fileBuffer = ...; // Load your KMZ file here (e.g., from a fetch request or a file input)
try {
// Initialize instances of GeoKMZer and GeoKMLer
const geoKMZer = new GeoKMZer();
const geoKMLer = new GeoKMLer();
// Extract KML contents from the KMZ buffer
const kmlContentsArray = await geoKMZer.read(fileBuffer);
// Process each KML and convert to GeoJSON
kmlContentsArray.forEach(({ filename, content }) => {
console.log(`Processing file: ${filename}`);
// Parse the KML content
const kmlDoc = geoKMLer.read(content);
// Convert the KML document to a GeoJSON object
const geoJson = geoKMLer.toGeoJSON(kmlDoc);
// Output the GeoJSON to the console
console.log(`GeoJSON for ${filename}:`, geoJson);
});
} catch (error) {
console.error("Error processing KMZ file:", error);
}
})();

Key Methods

GeoKMLer

GeoKMZer

Acknowledgments

The structure and logic for this project are based on established methods for XML to GeoJSON conversion, leveraging modern JavaScript best practices.

Project Home

https://github.com/JS55CT/GeoKMLer