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

Draggable Popup with Silverlight 3

As an Asp.net developer, I always liked the AJAX Modal Popup Extenders and used it extensively. As I move into RIA technologies with Silverlight 3, I find that the out-of-the-box popup control does not have the drag feature. To me, this is not good because more often than not we would like such Popups to be drag-enabled. A little bit of work around was all it took to make this popup drag-enabled. Thought it is worth sharing …

XAML:


<Popup Name="pop" Width="150" Height="200" >
   <StackPanel
       Name="stkPopup"
       Width="150"
       Height="200"
       Background="Gray"
       MouseLeftButtonDown="MouseDown"
       MouseMove="MouseMove"
       MouseLeftButtonUp="MouseUp">
          <StackPanel
             Width="150"
             Height="18"
             Background="Blue" >
              <TextBlock
                 FontSize="14"
                 Width="100" >
                    Infomation
              </TextBlock>
          </StackPanel>
          <TextBlock>
             DATA: my data.
          </TextBlock>
          <Button
             x:Name="btnClose"
             Click="btnClose_Click"
             Height="15"
             Width="15"/>
   </StackPanel>
</Popup
>

C# Code:


private void MouseDown(object sender, MouseButtonEventArgs e)
{
   Popup item = (sender as StackPanel).Parent as Popup;
   mouseY = e.GetPosition(null).Y;
   mouseX = e.GetPosition(null).X;
   isMouseCaptured = true;
   (item.Child as StackPanel).CaptureMouse();
}

private void MouseMove(object sender, MouseEventArgs e)
{
   Popup item = (sender as StackPanel).Parent as Popup;
   if (isMouseCaptured)
   {
      double deltaV = e.GetPosition(null).Y - mouseY;
      double deltaH = e.GetPosition(null).X - mouseX;
      double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
      double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
      item.SetValue(Canvas.TopProperty, newTop);
      item.SetValue(Canvas.LeftProperty, newLeft);
      mouseY = e.GetPosition(null).Y;
      mouseX = e.GetPosition(null).X;
   }
}

private void MouseUp(object sender, MouseButtonEventArgs e)
{
   Popup item = (sender as StackPanel).Parent as Popup;
   isMouseCaptured = false;
   (item.Child as StackPanel).ReleaseMouseCapture();
   mouseY = -1;
   mouseX = -1;
}

This sufficed my need for now. But going ahead, I am planning to make it truly “Modal” and package into a reusable user control. But that’s for the next post. Till then … happy coding.