Read from ShapeFile

Topics: Algorithms, Data Access
Oct 18, 2012 at 12:14 PM

Apologies if this has already been asked but as I am new to SharpMap and relatively new to .NET, I was wondering how to read data from a shapefile. For example i have a road Shapefile that has national primary network and I want to be able to read in a road (polyline) and calculate its length

Coordinator
Oct 18, 2012 at 9:37 PM

var sf = new SharpMap.Data.Providers.ShapeFile("primaryrd.shp");
sf.Open();
var numFeatures = sf.GetFeatureCount();
for (uint i = 1; i <= numFeatures; i++)
{
   var fdr = sf.GetFeature(i);
   var g = fdr.Geometry;
   var length = g.Length;
   //...
}

sf.Close();
Sth like this...

Oct 19, 2012 at 10:52 AM
Edited Oct 19, 2012 at 10:53 AM

Thanks for the reply. I tried your code but get an error with

var length = g.Length;

It says Geometry does not contain, here is the exact error

Error    1    'SharpMap.Geometries.Geometry' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'SharpMap.Geometries.Geometry' could be found (are you missing a using directive or an assembly reference?)
Coordinator
Oct 19, 2012 at 11:53 AM

You have two options:

  1. You grab the sharpmap source code and compile the solution yourself This way you have access to full topology support.
  2. Cast the geometry to a SharpMap.Geometries.Curve/SharpMap.Geometries.MultiCurve and grab the length from there.
Oct 25, 2012 at 7:54 PM

Hey! Look at this:
http://shapefile.codeplex.com/

if you want some use examples ask to me

Jul 18, 2013 at 3:41 PM
Hey ,why I get an error while using the code:

var sf = new SharpMap.Data.Providers.ShapeFile("primaryrd.shp");

The error message is " Cannot use GeometryServiceProvider without an assigned IGeometryServices class",
will somebody tell me why?
I am new using in SharpMap.
Thank you.
Coordinator
Jul 18, 2013 at 9:21 PM
Try using this when application starts
GeometryServiceProvider.Instance = new NtsGeometryServices();    
Nov 19, 2014 at 10:25 AM
thankyou