DEV Community

Bogdan Alexandru Militaru
Bogdan Alexandru Militaru

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

3

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay