Having spent not only the good part of today, but many hours of many other days, I thought it prudent to share some findings on where the world stands when it comes to creating and writing to a new Shapefile from a C# .NET environment.

As you probably know, a Shapefile is a file format synonymous with ESRI's ArcGIS/Map/Info suite of products; it has been around for ages. The file format is open, in that it is well documented if you want to construct the file programatically (as in, "let's create a binary file from scratch") - sod that.

So what are the options? Well, there are many options, and I spent some time exploring them in depth...

Create the file manually, according to the file spec

This is appealing if you have all the time in the world, are willing to learn all the nitty gritty of a file spec and understand all the niggles and caveats associated with doing this by hand. You can have a look at the file spec and make your own mind up. The idea of doing this would probably sit well with seriously experienced C/C++ old schoolers - not really what you want to be doing in something as high level as C# .NET 3.5.

SharpMap - A Geospatial Application Framework for the CLR

I really admire the idea of the project - bringing everything GIS together in one nice (albeit large) package for .NET developers. However, the reality for me was somewhat different. Considering myself at an average to intermediate programmer, I was full of frustration and anguish as I a) tried to navigate the site and find relevant documentation and b) match any forum posts/hints to a meaningful version of the distribution. The 0.9 release officially has no support for writing Shapefiles, whilst the 2.0 version does. It seems that SVN trunk is NOT current, so after getting the v2.0 branch, I fiddled around for a bit but still nothing - the (relatively) recent forum posts on the matter were referencing interfaces that didn't even show up.

So, this one was a no go. Perhaps when the project unites a bit more, something will come of it.

NetTopologySuite - port of the popular Java JTS GIS Suite

This one was a bit more promising - good sources and some unit tests to get some ideas from. Unfortunately it was again an issue with finding the right version of the right files, then interfacing them with seemingly non-existent classes. It was getting late in the day when I tried this, but it all just seemed so big and bulky for writing one simple polygon Shapefile. This suite implements some of GeoTools.NET, which it supposedly uses for "capabilities of read/write data from and to file formats such as Shapefile format" - this may be the case, but there are some many levels of abstraction away from what you actually want to do, that you get embroiled in a confusing web of inheritance, abstraction and implementation.

I'm sure it's possible, but it happens to be very hard to make possible.

GeoTools.net - set of .Net classes useful when handing geographic information

It is never very inspiring to see "recent activity" from 2005, but the Shapefile format is pretty tried and tested, so I gave it a spin.

By this point I was getting remarkably frustrated. I remember that this package didn't work, just not sure what the reason was.

Convert simple polygon shapes to KML using ogr2ogr

Seriously, I was starting to implement this thinking it was the easiest way around. Installed ogr2ogr and FWTools before I realised this was a ridiculous route to take.

.NET Wrapper for shapelib

shapelib is a C library for reading and writing Shapefiles - someone has done a .NET wrapper for it. I got pretty far on with this, but kept cocking up the sync between DBF and SHP; started to come a bit of a headache so I carried on looking.

Free tools included with shapelib

There are some multi-platform tools available freely from the shapelib author(s) - specifically in shapelib128_bin_win.zip over on the download page. Basically you get command line applications which allow you to create and write to Shapefiles and the associated DBF data files.

At last, a solution that understood my simple problem of "draw a polygon from a list of lat/longs". The final implementation is tiny static class in C# to create new Shapefiles and write polygons to them. I wasn't that keen on relying one external utilities to accomplish this, but after my ordeal I would happily ship a CD full of utilities with the software if it meant it worked quickly and easily. Also worth a look is this little user guide on AntholoGIS which gives a few more examples.

In conclusion

I may be a failed hardcore developer, but I have all my Shapefile creation needs satisfied in less than 100 lines of code. This is a trade off I can live with.

Thank you to everyone who was ever involved in the shapelib project - it is truly a perfect, elegant solution to what seems like it is a common problem (in the GIS programming world, anyway).