Search code examples
c#exceptionshapefilenettopologysuiteesri-maps

Create point shapefile with NetTopologySuite in c#


I'm trying to create a shapefile, but every time I get some kind of error. I'm tried use DotSpatial and NetTopologySuite libraries too, but it still not working.

I'm tried do with ShapefileDataWriter but i get exception (System.ApplicationException: 'Header must be set first!') how can I solve it?

using System;
using System.IO;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;

namespace SHP
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = Path.Combine(@"D:\SHP", "point_Created.shp");

            var featureCollection = new FeatureCollection();
            var geometryFactory = new GeometryFactory();
            var header = new ShapefileHeader();
            var point = geometryFactory.CreatePoint(new Coordinate(10, 20)); // x,y coordinates of the point
            var attributes = new AttributesTable { { "Name", "My Point" } };
            var feature = new Feature(point, attributes);
            featureCollection.Add(feature);

            
            var writer = new  ShapefileDataWriter(filePath);
            writer.Write(featureCollection); //System.ApplicationException:'Header must be set first!'
            

            
        }
    }
}

Solution

  • You are using a dicontinued package. You should use the NetTopologySuite.IO.Esri.Shapefile package.
    Here is how to write shapefiles with it: https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri#writing-shapefiles-using-c-code