How to Create a Shapefile in C#

This quick tutorial explains how to create a Shapefile in C# without installing any other third-party tool. You can create a vector layer by using the Shapefile driver however there are a number of other options available to create different types of vector layers. You can create SHP file in C# by adding attributes, geometry points, and features in the vector layer that is saved as a SHP file.

Steps to Create New Shapefile in C#

  1. Configure the development environment to add Aspose.GIS for .NET from the Nuget package manager
  2. Create a vector layer using the VectorLayer class object by providing the SHP file name and respective driver
  3. Set different attributes to the Attributes collection in the newly created vector layer
  4. Create a feature using the ConstructFeature function in the VectorLayer object
  5. Instantiate the Point class object and set the Geometry property of the new feature
  6. Add this feature to the vector layer

These steps explore the process how to make a Shapefile in C# by providing the configuration details, necessary classes, namespaces, and methods required to develop the application. Here the stepwise approach is shared for developing the application to generate Shapefile in C# where first a vector layer of type Shapefile is created and then different attributes are added to it. Similarly, for the newly created vector shape, a feature is constructed, and then its geometry position and features values are set.

Code to Create a Shape File in C#

using System;
using Aspose.Gis;
namespace AsposeProjects
{
class Program
{
static void Main(string[] args) // Main function to create a Shapefile in C#
{
// Initialize a license
Aspose.Gis.License lic = new Aspose.Gis.License();
lic.SetLicense(@"Aspose.Total.lic");
// Create a vector layer
using (VectorLayer vecLayer = VectorLayer.Create("sample.shp", Drivers.Shapefile))
{
// Set attributes
vecLayer.Attributes.Add(new FeatureAttribute("ProductName", AttributeDataType.String));
vecLayer.Attributes.Add(new FeatureAttribute("City", AttributeDataType.String));
vecLayer.Attributes.Add(new FeatureAttribute("Price", AttributeDataType.Integer));
vecLayer.Attributes.Add(new FeatureAttribute("Expiry", AttributeDataType.DateTime));
// Set features
Feature firstFeature = vecLayer.ConstructFeature();
firstFeature.Geometry = new Aspose.Gis.Geometries.Point(34.02, 71.52);
firstFeature.SetValue("ProductName", "Butter");
firstFeature.SetValue("City", "Peshawar");
firstFeature.SetValue("Price", 300);
firstFeature.SetValue("Expiry", new DateTime(2022, 12,15));
// Add feature to the layer
vecLayer.Add(firstFeature);
}
System.Console.WriteLine("Done");
}
}
}

This code uses Drivers.Shapefile option to create the vector layer however you may use other options also like GeoJson, Kml, Gpx, Gml, TopoJson, etc. to create a different type of a vector layer. This Vectorlayer class contains not only the attributes and features but contains a lot of conversion options as well where you can convert a layer to different formats.

This tutorial has taught us to create shape file in C# however if you are interested in converting SHP to GPX format, refer to the article on how to convert SHP to GPX in C#.

 English