Skip to content

Instantly share code, notes, and snippets.

@abfo
Last active February 8, 2023 01:39
Show Gist options
  • Save abfo/154a828d9803fd8d2a6a4da703618c44 to your computer and use it in GitHub Desktop.
Save abfo/154a828d9803fd8d2a6a4da703618c44 to your computer and use it in GitHub Desktop.
Sample code to read an Esri shapefile in .NET using Catfood.Shapefile. For full details please see https://ithoughthecamewithyou.com/post/esri-shapefile-reader-in-net
using Catfood.Shapefile;
using (Shapefile shapefile = new Shapefile(@"fips10c.shp"))
{
Console.WriteLine($"Shapefile type {shapefile.Type}, contains {shapefile.Count:n0} shapes.");
Console.WriteLine($"Bounding box is {shapefile.BoundingBox.Top:n2}, {shapefile.BoundingBox.Left:n2} - {shapefile.BoundingBox.Bottom:n2}, {shapefile.BoundingBox.Right:n2}");
foreach(Shape shape in shapefile)
{
string[] metadataNames = shape.GetMetadataNames();
foreach(string metadataName in metadataNames)
{
Console.WriteLine($"Shape {shape.RecordNumber} ({shape.Type})");
Console.WriteLine("Metadata:");
Console.WriteLine($" {metadataName}: {shape.GetMetadata(metadataName)}");
ShapePolygon? polygon = shape as ShapePolygon;
if (polygon != null)
{
foreach (PointD[] part in polygon.Parts)
{
foreach (PointD point in part)
{
Console.WriteLine($"First point: {point.X:n2}, {point.Y:n2}");
break;
}
break;
}
}
}
// only print the first shape
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment