
We may have Geographic data available in many formats supported by various systems. In certain cases, we may need to export geographic data from ESRI Shapefile (SHP) format to Keyhole Markup Language (KML) format. The Shapefile format is ESRI’s one of the most common geospatial vector data formats used to display geographical information. The KML is an XML-based format, used to share geographic data with non-GIS users. The KML format is used to display geographic data in Google Earth or Google Maps. In this article, we will learn how to convert a shapefile to KML in C#.
This article shall cover the following topics:
- C# Shapefile to KML Converter
- Steps to Convert Shapefile to KML
- Export ShapeFile to KML
- [Handle Invalid Coordinates During Conversion][26]
C# Shapefile to KML Converter API - Free Download
To convert a shapefile to KML, we will be using the Aspose.GIS for .NET API. It allows exporting shapefile to KML without needing software like ArcGIS, ArcMap, QGIS or Google Earth. The API also allows the rendering of maps, creating, reading, and converting geographic data available in the supported file formats.
The ConversionOptions class of the API provides options for converting data between formats. The SpatialReferenceSystem class provides a list of different spatial reference systems to use in mapping coordinates to places on Earth. We have the VectorLayer class that represents a vector layer in the API. The Convert(string, FileDriver, string, FileDriver, ConversionOptions) method of this class converts a layer to a different format.
Please either download the DLL of the API or install it using NuGet.
PM> Install-Package Aspose.GIS
How to Convert Shapefile to KML in C#
We can easily convert a shapefile to KML using Aspose.GIS for .NET by following the steps given below:
- Assign the Wgs84 spatial reference to the destination layer.
- Load and convert file format from SHP to KML.
Now, let’s see how to perform these steps in C# to convert a shapefile file to a KML format.
Export ShapeFile to KML in C#
Please follow the steps given below to export data from a shapefile to KML:
- Initialize an instance of the ConversionOptions class.
- Set the DestinationSpatialReferenceSystem as Wgs84.
- Finally, call the VectorLayer.Convert() method to convert source shapefile to KML.
The following code sample shows how to convert a shapefile to KML using C#.
Handle Invalid Coordinates During Conversion
In some cases, a shapefile may contain coordinates that cannot be transformed into the WGS 84 coordinate system required by KML. These include placeholder “no data” values, out-of-range coordinates, or data that does not match its .prj file. By default, the conversion then stops with a TransformationException, even if only one record is invalid. Starting with version 26.6, Aspose.GIS for .NET provides the OperationErrorCollector class, which lets the conversion skip invalid features, keep all valid data, and report exactly which records failed. To learn how to use it, read Why Your Shapefile Conversion Fails and How to Fix It in 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 a shapefile to KML in C#. We have also seen how to define conversion options programmatically. Besides, you can explore how to work with several other GIS file formats and learn more about Aspose.GIS for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on our forum.