MapWinGIS:SampleCode-C Sharp Net:BufferedBoundaries

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

Buffered Boundary Class

This example takes a typical network of roads for a given area. It combines them into a single geometry. It uses a buffer in order to create polygons that surround the road networks. Then, it takes only the outer shell for the resulting buffer and returns the outer shells as a single shape in a polygon shapefile.

 // requires a reference to:
 // MapWindGeoProc.dll
 // MapWindow.Interfaces.dll
 // Errors were fixed to make this work so be sure to download the latest MapWinGeoProc from the svn repository (version 7307 or later)
 // svn.MapWindow.org/svnroot
 using System.Windows.Forms;
 using MapWindow.Interfaces.Geometries;
 using MapWinGeoProc;
 using MapWinGeoProc.NTS.Topology.Geometries;
 namespace OCXTestCS
 {
     /// <summary>
     /// This tool takes a given polyline shapefile, merges the lines into a single shape,
     /// calculates a buffer and returns only the outer shells of the buffer as a multi-polygon.
     /// </summary>
     public static class BufferedBoundary
     {
         const double BUFFER_DISTANCE = .001; // This depends on the projection or situation.
 
         /// <summary>
         /// This method actually calculates the boundary for the given lines.
         /// </summary>
         /// <param name="lineShapefile">The line shapefile to calculate the boundary for.</param>
         /// <param name="filename">The filename for the output shapefile</param>
         /// <returns>A Line shapefile.</returns>
         public static MapWinGIS.Shapefile CalculateBoundary(MapWinGIS.Shapefile lineShapefile, string filename)
         {
             IGeometry mergedLines = null;
             IGeometry bufferedLines;
             Geometry result;
             for (int shp = 0; shp < lineShapefile.NumShapes; shp++)
             {
                 Geometry geom = NTS_Adapter.ShapeToGeometry(lineShapefile.get_Shape(shp));
                 if (mergedLines == null)
                 {
                     mergedLines = geom;
                 }
                 else
                 {
                     mergedLines = mergedLines.Union(geom);
                 }
             }
             bufferedLines = mergedLines.Buffer(BUFFER_DISTANCE);
             MultiPolygon mp = bufferedLines as MultiPolygon;
             if (mp != null)
             {
                 Polygon[] shells = new Polygon[mp.NumGeometries];
                 for (int i = 0; i < mp.NumGeometries; i++)
                 {
                     Polygon p = mp.GetGeometryN(i) as Polygon;
                     shells[i] = new Polygon(p.Shell as LinearRing);
                 }
                 result = new MultiPolygon(shells);
             }
             else
             {
                 Polygon p = bufferedLines as Polygon;
                 result = new Polygon(p.Shell as LinearRing);
             }
             MapWinGIS.Shape shape = NTS_Adapter.GeometryToShape(result);
             MapWinGIS.Shapefile resultShapefile = new MapWinGIS.Shapefile();
             resultShapefile.CreateNew(filename, MapWinGIS.ShpfileType.SHP_POLYGON);
             resultShapefile.StartEditingShapes(true, null);
             
             MapWinGIS.Field field = new MapWinGIS.Field();
             field.Name = "ID";
             field.Type = MapWinGIS.FieldType.INTEGER_FIELD;
             int fldIndex = 0;
             if (resultShapefile.EditInsertField(field, ref fldIndex, null) == false)
             {
                 MessageBox.Show(resultShapefile.get_ErrorMsg(resultShapefile.LastErrorCode), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return null;
             }
             int shpIndex = 0;
             if (resultShapefile.EditInsertShape(shape, ref shpIndex) == false)
             {
                 MessageBox.Show(resultShapefile.get_ErrorMsg(resultShapefile.LastErrorCode), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return null;
             }
             resultShapefile.StopEditingShapes(true, true, null);
             return resultShapefile;
         } 
 
     }
 }
 

Using the Class

 using System;
 using System.Windows.Forms;
 
 namespace OCXTestCS
 {
     public partial class Form1 : Form
     {
         private MapWinGIS.Shapefile _lineShapefile;
         public Form1()
         {
             InitializeComponent();
         }
 
         private void openToolStripMenuItem_Click(object sender, EventArgs e)
         {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "Shapefiles |*.shp";
             if (ofd.ShowDialog(this) != DialogResult.OK) return;
             _lineShapefile = new MapWinGIS.Shapefile();
             _lineShapefile.Open(ofd.FileName, null);
             axMap1.AddLayer(_lineShapefile, true);
             
         }
 
         private void cmdTest_Click(object sender, EventArgs e)
         {
             if (_lineShapefile == null)
             {
                 MessageBox.Show("Open a line shapefile first.");
                 return;
             }
             MapWinGIS.Shapefile boundary = BufferedBoundary.CalculateBoundary(_lineShapefile, "C:\\testBounds.shp");
             axMap1.AddLayer(boundary, true);
 
         }
     }
 }

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-C_Sharp_Net:BufferedBoundaries"

This page has been accessed 1,549 times. This page was last modified on 10 December 2008, at 17:31.