This project has moved and is read-only. For the latest updates, please go here.

Export to shapefile

Topics: Data Access, SharpMap Project, SharpMap v0.9 / v1.x, WinForms Controls
May 25, 2010 at 10:12 PM

Is is posible to write code to export data from database  to shapefile? Is there any code for this anyready writen? in need this functionality with sharpmap v0.9 / v1.x.

Thank you all for help

May 27, 2010 at 11:38 AM

Hi cybermkk, all the providers are readonly in 0.9/1. In v2 the shapefile provider is writable. There is some code in the NTS source tree for writing shapefiles but iirc there are issues opening the written shapefile in some gis packages.

hth jd

May 29, 2010 at 11:17 AM
Edited May 29, 2010 at 11:18 AM

I also had to write shape files and the files created by NTS were not read by ArcGIS. Finally after a lot of searching around I used Easy GIS DotNet library for Shape File writing.

You can find it at

Also, remember that all column names in the destination DBF file should be of max 8 characters. I had my hair splitting for a week over this :(



May 30, 2010 at 1:04 AM

Thank you for your contribution. Do you have the code for exporting to shapefile, with easygis? You can also send me the replay to the private mail if you think this shuld not be part of sharpmap discussion. I will foward it also to anyone after me that need export in 0.9. Thank you again for your time.


May 31, 2010 at 2:01 PM
Edited May 31, 2010 at 2:03 PM
Sure. I can mail you a sample application with the functionality demonstrated. You can ping me at kraghavk[at]gmail[dot]com. I will then send the sample app to you. I can also post it here if the SharpMap folks are ok with it! Raghu
May 31, 2010 at 8:17 PM

I would like to see your code too either posted here or as a link somewhere else.

- Dirk

May 31, 2010 at 9:32 PM

Hi Raghu you are more than welcome to post code either in the forum if it is a snippet or on the patches page if it is a larger entity. The patches page is linked off the source code tab.. cheers jd

Jun 2, 2010 at 8:10 AM
Edited Jun 2, 2010 at 8:21 AM

You will have to download Easy GIS.NET Desktop edition from and install it. After installation, if you look at the folder where the app was installed it will contain EGIS.ShapeFileLib.DLL. You'll need to add a reference to this DLL in your project. Below is a working code snippet for exporting polygons from a FeatureDataTable.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using EGIS.ShapeFileLib;
using SharpMap.Data;
using SharpMap.Geometries;

namespace ShpExporter
    class ShpExporter
        public void ExportToShapeFile()
            string shpFilePath = @"C:\Users\Raghu\Desktop\Shape Files\Test.shp";

            //Both these vars are required by ShapeFileWriter
            string baseDirName = Path.GetDirectoryName(shpFilePath); //Get the directory path from the shpFilePath
            string shpFileName = Path.GetFileNameWithoutExtension(shpFilePath); //Get the shpFile name alone without the Directory path and .shp extension

            /// The ShapeFileWriter also expects an array of DbFieldDesc structure,
            /// which basically defines the DBF file header that will go along with the shape file
            /// NOTE: 
            /// * All field names must be 8 or less characters, else most GIS software will refuse to load your shape file!
            /// * FieldLength may not be required for Numbers, bools etc.
            ///   But, I had still put them in my application initially when I was trying out various things to get it working.
            ///   Now that it is working, I have not tried to remove it and figure out if it works without them. :)
            ///   Pls let me know if any of you do discover if it is required or not!
            /// * Each field also needs to have a RecordOffset, (i.e., the offset of the field in a record)
            ///   For Field 0, RecordOffset = 0
            ///   For Field 1, RecordOffset = Length of Field 0
            ///   For Field N, RecordOffset = Length of Field0 to FieldN-1
            DbfFieldDesc[] fields = new DbfFieldDesc[] 
                new DbfFieldDesc { FieldName = "ID", FieldType = DbfFieldType.Number, FieldLength = 1, RecordOffset = 0 },
                new DbfFieldDesc { FieldName = "FeatrName", FieldType = DbfFieldType.Character, FieldLength = 50, RecordOffset = 1 },
                new DbfFieldDesc { FieldName = "FeatrDesc", FieldType = DbfFieldType.Character, FieldLength = 255, RecordOffset = 50 }

             * Also, remember that a Shape File can only contain features of same geometry type.
             * If your table has multiple geometry types such as Polygon, Point, LineString etc. then you will have to 
             * group your data by geometry type and export seperate shape files!
            ShapeFileWriter shpWriter = ShapeFileWriter.CreateWriter(baseDirName, shpFileName, ShapeType.Polygon, fields);

            FeatureDataTable table = new FeatureDataTable();

             * Data Retrieval logic ommitted. This is obviously speficic to your application

            foreach (FeatureDataRow row in table)
                //Since we are exporting only polygons, we right try to cast the row's geometry into a Polygon.
                Polygon p = row.Geometry as Polygon;

                //If the geometry was not a Polygon, then p will be null
                if (p != null)
                    //Attribute data associated with the geometry, needs to be passed in as a string array!
                    string[] fieldData = new string[]

                    //The vertices of the polygon needs to be copied onto an array of PointF
                    List<PointF> vertices = new List<PointF>();

                    foreach (SharpMap.Geometries.Point v in p.ExteriorRing.Vertices)
                        vertices.Add(new PointF((float)v.X, (float)v.Y));
                     * Now we are ready to write the stuff into Shape File
                     * Since this is a simple 2D polygon, a PointF[] would suffice.
                     * For various other overloads of ShapeFileWriter.AddRecord 
                     * see
                    shpWriter.AddRecord(vertices.ToArray(), vertices.Count, fieldData);

            //That's it, shpWriter needs to be closed to ensure all data is flushed and file handles released.
//Silly as it may sound, but this class does not implement IDisposable or a Finalizer!


Jun 4, 2010 at 5:23 PM

thank you. You code is working ok. i have just some problems with the ecoding of the shapefile. some characters are not correct. Do you know any way to set the encoding for the shapefile writer?

thank you, and regards Mike

Jun 5, 2010 at 6:38 AM

Oh, sorry to hear that. I had quick look at their APIs and did'nt seem to find anything related to setting the language, culture, encoding etc.

I guess you can contact the authors of this software on one of the mail ids provided in


Sep 16, 2010 at 10:49 AM

HI everybody!!!

I want to use a GIS control  and I have a doubt: Which of them do you prefer, EasyGIS or SharpMap????

The main requirements are::the possiblity of drawing images or .Net controls, zoom customized by a selected  area, possibility of grouping drew elements and are abel to program action ina a contextual menu for elements.

Thanks & Best regards!!!