DEV Community

Bogdan Alexandru Militaru
Bogdan Alexandru Militaru

Posted on • Originally published at boobo94.github.io on

Create kml/kmz files in Nodejs

Libraries used:

Create kml file - https://www.npmjs.com/package/@maphubs/tokml Create geojson - https://www.npmjs.com/package/geojson Create kmz using zip - https://www.npmjs.com/package/jszip

Create KML File


const points = [
    {latitude: 39.984, longitude: -75.343},
    {latitude: 39.284, longitude: -75.833},
    {latitude: 39.123, longitude: -74.534},
    {line: [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]],}
]

const geojsonObject = geojson.parse(points, {
    Point: ['latitude', 'longitude'],
    LineString: 'line',
}), {
    documentName: 'Document Name',
    documentDescription: 'KML Export'
}

  const response = tokml(geojsonObject);

Enter fullscreen mode Exit fullscreen mode

Content-type for kml:

'Content-Type': 'application/vnd.google-earth.kml+xml',

Enter fullscreen mode Exit fullscreen mode

Create KMZ File

  const zip = new JSZip();
  zip.file('doc.kml', kmlFile);

  return zip.generateAsync({ type: 'nodebuffer' });

Enter fullscreen mode Exit fullscreen mode

Content-type for kml:

'Content-Type': 'application/vnd.google-earth.kmz',

Enter fullscreen mode Exit fullscreen mode

Top comments (0)