Convert Shapefile to SVG in C#

The Shapefile format is ESRI’s one of the most common geospatial vector data formats used to display geographical information. In certain cases, we may need to export geographic data from ESRI Shapefile (SHP) format to SVG format. The SVG is an XML-based text format for describing the appearance of an image. In this article, we will learn how to convert a shapefile to SVG in C#.

This article shall cover the following topics:

  1. C# Shapefile to SVG Converter
  2. Steps to Convert Shapefile to SVG
  3. Render ShapeFile to SVG

C# Shapefile to SVG Converter API - Free Download

To convert a shapefile to SVG, we will be using the Aspose.GIS for .NET API. It allows exporting shapefile to SVG 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 Map class of the API is a collection of layers that can be rendered on top of each other via Renderer. The SpatialReferenceSystem class provides a list of different spatial reference systems to use for the map. We can define a simple vector line symbolizer using the SimpleLine class. We have the VectorLayer class that represents a vector layer in the API. We can load the shapefile into a vector layer using the Open(string, FileDriver) method of this class. It opens the layer for reading.

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

PM> Install-Package Aspose.GIS

How to Convert Shapefile to SVG in C#

We can easily convert a shapefile to SVG using Aspose.GIS for .NET by following the steps given below:

  1. Initialize a Map.
  2. Assign the Wgs84 spatial reference to the map.
  3. Open a shapefile as a vector layer.
  4. Add the shapefile layer to the map.
  5. Render the map in SVG format.

Now, let’s see how to transform these steps into C# code and convert a shapefile file to SVG format.

Render ShapeFile to SVG in C#

Please follow the steps given below to render a shapefile as SVG:

  1. Firstly, create an instance of the Map class.
  2. Next, set the SpatialReferenceSystem as Wgs84.
  3. Then, define a simple line symbolizer using the SimpleLine class.
  4. Meanwhile, load a shapefile using the VectorLayer.Open() method.
  5. After that, add the shapefile layer to the map using the Map.Add() method.
  6. Finally, call the Map.Render() method to save as SVG. It takes the output SVG file path and Renderers.Svg as arguments.

The following code sample shows how to convert a shapefile to SVG 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 a shapefile to SVG in C#. We have also seen how to define a map and load a shapefile programmatically. 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