Convert OSM to KML in C#

OpenStreetMap (OSM) is an open-source project. It aims to create a free, editable geographical database of the entire world. The OpenStreetMap file (.osm) is an extract of the OpenStreetMap database stored as XML. In certain cases, we may need to export map data from OSM to display on Google Earth. Google Earth uses KML files to display geographic data. In this article, we will learn how to convert an OSM to KML in C#.

This article shall cover the following topics:

  1. C# OSM to KML Converter
  2. Steps to Convert OSM to KML
  3. Convert OSM to KML

C# OSM to KML Converter API - Free Download

For converting OpenStreetMap (OSM) file to KML, we will be using the Aspose.GIS for .NET API. The API allows exporting OSM to KML without needing software like ArcGIS, ArcMap, QGIS or Google Earth. It also allows the rendering of maps, creating, reading, and converting geographic data available in the supported file formats.

The API provides the VectorLayer class that represents a vector layer. It provides the Convert(string, FileDriver, string, FileDriver, ConversionOptions) that converts a layer to a different format. The SpatialReferenceSystem class provides a list of different spatial reference systems to use for the map.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.GIS

How to Convert OSM to KML in C#

We can easily convert OpenStreetMap (OSM) file to KML using Aspose.GIS for .NET by following the steps given below:

  1. Assign the Wgs84 spatial reference to the destination layer.
  2. Convert file format from OSM to KML.

The following section describes how to transform these steps into C# and convert an OSM to KML format.

Convert OSM to KML in C#

We can export data from OSM to KML by following the steps given below:

  1. Firstly, create an instance of the ConversionOptions class.
  2. After that, set the DestinationSpatialReferenceSystem as Wgs84.
  3. Finally, call the VectorLayer.Convert() method to convert source OSM to KML.

The following code sample shows how to convert an OSM to KML using C#.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to convert an OSM to KML in C#. Besides, you can explore how to work with several other GIS file formats and learn more about the API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also