MapWinGIS:SampleCode-C Sharp Net:AddLineAngles

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

Add an Attribute Field with Line Angles

This example was intended for line shapefiles. This example calculates the angle from the startpoint to the endpoint of each shape. If the shape is multipart, only the first part is considered. The calculated angle is added to the attribute table as "Angle"

 using System;
 using System.Collections.Generic;
 using System.Collections;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 
 namespace MapWinGISTEST
 {
     public partial class Form1 : Form, MapWinGIS.ICallback
     {
         System.Collections.Hashtable m_Shapefiles;
         System.Collections.Hashtable m_Grids;
         
         // During initialization this sets some basic default parameters
         public Form1()
         {
             InitializeComponent();
             axMap1.SendMouseMove = true;
             axMap1.SendMouseDown = true;
 
             // Initialize hashtables so we don't get null object exceptions later
             m_Shapefiles = new System.Collections.Hashtable();
             m_Grids = new System.Collections.Hashtable();
             
         }
 
         /// <summary>
         /// This function allows the user to use a file dialog to add shapefiles to the demo project
         /// </summary>
         /// <param name="sender">Part of the windows forms architecture.  In this case the menu object.</param>
         /// <param name="e">The Parameters for the menu item click event.</param>
         private void addShapefileToolStripMenuItem_Click(object sender, EventArgs e)
         {
             bool result;
             OpenFileDialog OFD = new OpenFileDialog();
             MapWinGIS.Shapefile mwShapefile = new MapWinGIS.Shapefile();
             OFD.Filter = "Shapefiles (*.shp)|*.shp";
             if (OFD.ShowDialog() != DialogResult.OK) return;
             result = mwShapefile.Open(OFD.FileName, this);
             if (!result)
             {
                 MessageBox.Show(mwShapefile.get_ErrorMsg(mwShapefile.LastErrorCode));
             }
             int lyr = axMap1.AddLayer(mwShapefile, true);
             m_Shapefiles.Add(lyr, mwShapefile);
         }
   
         // Warning!  This example will add a new field to your shapefile
         // Make a copy first if you don't want to alter the original
         private void addDirectionsToolStripMenuItem_Click(object sender, EventArgs e)
         {
             if (m_Shapefiles.Count == 0) return;
             MapWinGIS.Shapefile mwShapefile = new MapWinGIS.Shapefile();
             MapWinGIS.Shape mwShape;
            
             IDictionaryEnumerator en = m_Shapefiles.GetEnumerator();
             
             int NumShapes;
             int FieldColumn = -1;
             bool result;
 
             // I stored the shapefiles in a hash table, so use an enumerator to get them
             while (en.MoveNext())
             {
                 mwShapefile = en.Value as MapWinGIS.Shapefile;
                 // We only will do this for Polyline type shapefiles
                 if (mwShapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POLYLINE &&
                    mwShapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POLYLINEM &&
                    mwShapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POLYLINEZ) continue;
                 result = mwShapefile.StartEditingTable(this);
                 if (!result)
                 {
                     MessageBox.Show(mwShapefile.get_ErrorMsg(mwShapefile.LastErrorCode));
                 }
                 // Try to find a field named Angle
                 int Mark = 1;
                 string Name = "Angle";
                 for (int fld = 0; fld < mwShapefile.NumFields; fld++)
                 {
                     if (mwShapefile.get_Field(fld).Name == Name)
                     {
                         // We found an angle field, but it was the wrong datatype
                         if (mwShapefile.get_Field(fld).Type != MapWinGIS.FieldType.DOUBLE_FIELD)
                         {
                             Name = "Angle" + Mark.ToString(); // try adding numbers to get a new column
                             Mark++;
                             fld = -1; // restart loop with new name
                             break;
                         }
                         // The "Angle" field already exists, so retain fld
                         FieldColumn = fld;
                         break;
                     }
                 }
                 if (FieldColumn == -1)
                 {
                     FieldColumn = mwShapefile.NumFields;
                     MapWinGIS.Field mwField = new MapWinGIS.Field();
                     mwField.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
                     mwField.Name = Name;
                     // This will add the new field and store the new field index value in FieldColumn
                     result = mwShapefile.EditInsertField(mwField, ref FieldColumn, this);
                     if (!result)
                     {
                         MessageBox.Show(mwShapefile.get_ErrorMsg(mwShapefile.LastErrorCode));
                     }
                 }
                 NumShapes = mwShapefile.NumShapes;
                 for (int shp = 0; shp < NumShapes; shp++)
                 {
                     mwShape = mwShapefile.get_Shape(shp);
                     MapWinGIS.Point pt1;
                     MapWinGIS.Point pt2;
                     if (mwShape.NumParts > 1)
                     {
                         // Only consider the first part listed
                         pt1 = mwShape.get_Point(0);
                         pt2 = mwShape.get_Point(mwShape.get_Part(1)-1);
                     }
                     else
                     {
                         // Since our field only allows us to store one value for each shape, just approximate the direction as the first to last point
                         pt1 = mwShape.get_Point(0);
                         pt2 = mwShape.get_Point(mwShape.numPoints-1);
                     }
                     double Angle = Math.Atan2(pt2.y - pt1.y, pt2.x - pt1.x);
                     // Angle now should range from -PI to PI
                     // Convert to degrees if you like
                     Angle = Angle * 180 / Math.PI;
                     result = mwShapefile.EditCellValue(FieldColumn, shp, Angle);
                     if (!result)
                     {
                         MessageBox.Show(mwShapefile.get_ErrorMsg(mwShapefile.LastErrorCode));
                     }
                     toolStripProgressBar1.Value = (int)((shp * 100) / mwShapefile.NumShapes);
                 }
                 result = mwShapefile.StopEditingTable(true, this);
                 if (!result)
                 {
                     MessageBox.Show(mwShapefile.get_ErrorMsg(mwShapefile.LastErrorCode));
                 }
 
             }
 
         }
 
         void axMap1_MouseMoveEvent(object sender, AxMapWinGIS._DMapEvents_MouseMoveEvent e)
         {
             double X = 0;
             double Y = 0;
             axMap1.PixelToProj(e.x, e.y, ref X, ref Y);
             toolStripStatusLabel1.Text = "X: " + X.ToString() + ", Y: " + Y.ToString();
         }
 
 
         #region ICallback Members
 
         void MapWinGIS.ICallback.Error(string KeyOfSender, string ErrorMsg)
         {
             MessageBox.Show(ErrorMsg, "Error");
         }
 
         void MapWinGIS.ICallback.Progress(string KeyOfSender, int Percent, string Message)
         {
             toolStripStatusLabel1.Text = Message;
             toolStripProgressBar1.Value = Percent;
             Application.DoEvents(); // Allow the interface to refresh
         }
 
         #endregion
 
     }
 }

Posted By Shade1974 on 4/18/2007

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

This page has been accessed 2,443 times. This page was last modified on 12 February 2010, at 11:58.