ESRI Shapefile Reader in .NET

Updated on Sunday, March 12, 2023

ESRI Shapefile Reader in C#/.NET

Catfood.Shapefile is a .NET read-only, forward-only parser for Esri shapefiles. With Catfood.Shapefile it’s easy to enumerate all the shapes in a shapefile together with their metadata.

From version 2.00 Catfood.Shapefile is available via NuGet. It has also been migrated to .NET Standard 2.0. This means it is compatible with .NET Framework 4.61 or better and .NET Core 2.0 or better. If you use an older version of .NET then you should continue using Catfood.Shapefile 1.60. The source code is on GitHub.

Esri (originally Environmental Systems Research Institute) shapefiles are a GIS (geographic information systems) file format that can store a wide variety of geographic information. I originally started working with these files to add country borders to Catfood Earth.

Shapefiles actually consist of a number of different files with different extensions. There are three mandatory extensions: .shp, .shx and .dbf and so a shapefile containing borders might be distributed as borders.shp, borders.shx and borders.dbf. The .shp file contains the actual shape data in vector format, often lines or polygons. The .shx file is an index to the shapes. Finally the .dbf file is a database of metadata associated with the shapes. Catfood.Shapefile requires these three files.

There are several optional extensions that might be included with a shapefile. .prj contains the geographical projection, .xml is an alternate metadata format, .sbn and .sbx index for spatial queries, and .cpg defines the code page used by the shapefile.

Here is some sample code for a .NET 6 console application to print basic information about a shapefile and the first point in the first polygon:

To use this, create a new console application, paste the code into Program.cs and add Catfood.Shapefile via NuGet.

The default constructor uses a database connection string that uses the Jet driver. This is only available for 32-bit applications and you will need to target Windows x86 to use it. An overload is available that allows you to specify a custom connection string to access metadata.

Catfood.Shapefile supports Null, Point, MultiPoint, PolyLine, PolyLineM and Polygon shapes. Three dimensional shapes (PointZ, PolyLineZ and PolygonZ) are not currently supported. There is also currently no support for writing shapefiles. If you add any of this please send a pull request on GitHub!

I am grateful to the developers who have contributed to Catfood.Shapefile over the years. Most are credited in the release notes below. If you need help or have feedback please leave a comment on this post.

Updates

Catfood.Shapefile 2.00

I just released Catfood.Shapefile 2.00, my .NET parser for ESRI Shapefiles.

The big change is that I have migrated to .NET Standard 2.0. This makes it possible to use from .NET Core as well as classic .NET Framework from 4.6.1 up. If you need to use an older version of .NET Framework then you'll want to stick with Catfood.Shapefile 1.60.

Catfood.Shapefile is now also available via NuGet. This is the recommended way to install. The source code is still available on GitHub.

Catfood.Shapefile 1.60

I just released Catfood.Shapefile 1.60. This contains a fix from Libor Weigl that factors out the enumerator so that you can still access the shapefile after enumeration.

Catfood.Shapefile is a .NET library for parsing ESRI Shapefiles.

(previously)

Shapefile Update

A few people have asked for 3D shape support in my ESRI Shapefile library. I've never got around to it, but CodePlex user ekleiman has forked a version in his ESRI Shapefile to Image Convertor that supports PointZ, PolygonZ and PolyLineZ shapes. If that's what you need please check it out.

Catfood.Shapefile 1.50

I've just released a small update to my C# Shapefile library on Codeplex. Catfood.Shapefile 1.50 fixes a couple of bugs related to metadata and adds the ability to access metadata records directly via IDataRecord. 

Catfood.Shapefile 1.40

I’ve just released a small update to Catfood.Shapefile. Stephan Stapel, who implemented PolyLineM support, has contributed a patch that improves the class hierarchy. CodePlex user originSH suggested supporting the ACE driver for 64-bit systems. I’ve added a constructor overload that allows you to use predefined Jet and ACE connection strings or provide your own templates if necessary. Thanks to Stephan and originSH.

Catfood.Shapefile is a .NET library for enumerating ESRI shapefiles. I originally wrote the library to help me build some complex layers in Catfood Earth. Since then it’s picked up thousands of users and some really valuable suggestions and patches from the CodePlex community. I’m very glad a took a couple of hours to open source the library back in 2009.

PolyLineM support in Catfood.Shapefile

I’ve just updated Catfood.Shapefile, my ESRI Shapefile parser for .NET, with PolyLineM support thanks to a contribution from Stephan Stapel. The solution for the new version has also been updated to Visual Studio 2010.

Download Catfood.Shapefile.dll 1.30 from CodePlex.

ESRI Shapefile Library Update

I've just released a small update for my ESRI Shapefile Reader project on GitHub. The only change is a patch from SolutionMania that fixes a problem when the shapefile name is also a reserved name in the metadata database. The patch escapes the name preventing an exception from being thrown.

Catfood.Shapefile.dll is a .NET 2.0 forward only parser for reading an ESRI Shapefile. Download 1.20 from GitHub.

Catfood.Shapefile.dll 1.10

I've just released v1.10 of my ESRI Shapefile Reader (Catfood.Shapefile.dll). This is a .NET 2.0 forward only parser for reading shapefile content.

Sharon Tickell was kind enough to report two bugs with suggested updates. These have both been fixed in 1.10.

While working on these fixes I also discovered that there are no 64-bit Jet drivers (since releasing the first version I've upgraded to a 64-bit box for development). This is an easy fix, just target any application using Catfood.Shapefile.dll at x86. I've updated the demo application accordingly.

Download Catfood.Shapefile.dll from GitHub.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: ESRI Shapefile Reader in .NET #code #shapefile #esri #github #codeplex #polylinem #c# #.net #nuget Catfood.Shapefile is a .NET library that provides easy access to Esri shapefiles (.shp, .shx, .dbf), available on NuGet and GitHub. )

Comments

Robert Ellison

I just released a significant update - Catfood.Shapefile 2.00 has been ported to .NET Standard 2.0 and is now available on NuGet.

Tanguy Drieu

About the 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.' issue, you need to install the new driver Microsoft Access Database Engine 2010 Redistributable.

Then target the Shapefile.ConnectionStringTemplateAce (Microsoft.ACE.OLEDB.12.0) instead of the Jet one when using the Shapefile constructor.

That worked for me.

Robert Ellison

Hi Jon, I'm not aware of any solution to the missing 64-bit Jet drivers. When I use this library I target x86. If you find anything useful please post back.

Jon Bastow

Hi,

Just trying to get this handy little library working in a .NET project in VS2022. Seeing some problems with the following exception:

Unhandled exception. System.InvalidOperationException: The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.

The only way I can get this working is targeting an x86 environment.

Are you aware of any options to get this running on 64-bit?

Cheers,

jon

Robert Ellison

Depends on the file. If you have a .prj that should have coordinate information. I usually understand this from the source of the shapefile, or plot it and see if it makes sense.

Ian Hannah

Hi,

How can you determine if the points read are in lat/long or easting/northing format?

Obviously I could probably try and work it out but is there an easier way to find out?

Thanks

Ian

Richie Rich

Hi mate, just a short "THANK YOU" for all your work on the catfood shapefile suite, I've just got it up and running on a vb.net project I'm working on. Thanks :)

Rob Ellison

Patrick, I haven't tried the 64-bit driver yet. This was the only reason for forcing the demo app to run as 32-bit so it should be easy to test by updating the project to "Any CPU" and trying it with a shapefile.

The library is not thread safe so you should enumerate on a single thread. This only applies to parsing the file, once you've got the shape objects you could use worker threads to perform whatever processing is needed. Does this help?

Patrick Cozzi

Rob, I'm interested to know if the 64-bit drivers work with the Shapefile dBase format. I am currently running as a 32-bit process to workaround the issue.

Also, do you know if I can read different shapefiles using different threads with your library?

Regards,
Patrick

Rob Ellison

Thanks Cat, will check it out and see if it works with the Shapefile dBase format.

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.

Newsletter

Related

CodePlex Shutdown: Shapefile, Orb, StackHash and Blogger2BlogML Migration