Parse Shapefile using SharpMap

Our current project required us to read Shapefile and import the shape (from .shp file) and data (from .dbf file) from the same. We investigated into quite a few tools having the ability to parse a shape file and import the shape (also called Geometries) along with the data into the database. These tools were either not up to the mark or pretty costly. This is when I came across SharpMap which did the trick for us.  

SharpMap is an easy-to-use mapping library for use in web and desktop applications. It is an open source under GNU Lesser General license.  

Download and import the SharpMap dll  

 You can download the SharpMap dll from here. Import the compiled dll into the application by browsing downloaded location.  

Write code to import Shapefile data 

    
ShapeFileProvider sf = null;
sf = new ShapeFileProvider(filePath);
sf.Open(false);
BoundingBox ext = sf.GetExtents();
FeatureDataSet ds = new FeatureDataSet();
sf.ExecuteIntersectionQuery(ext, ds);
FeatureDataTable<uint> table = ds.Tables[0]
as FeatureDataTable<uint>//TODO:
//Read the .dbf data from the row.

foreach (FeatureDataRow row in table.Rows)
{
   Polygon polygon = row.Geometry
          as SharpMap.Geometries.Polygon;
   if(polygon != null)
  {
     foreach (var vertex in polygon.ExteriorRing.Vertices)
     {
        double latitude = vertex.Y;
        double longitude = vertex.X;

        //TODO:
        //Import to to the database.
     }
   }
}

 

 

You can write a program which would run once and import all the shapefile data into your database schema. You will, thus, have all the data to play at will. Try it out.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s