MapWinGIS:SampleCode-C Sharp Net:SequentialNearestNeighbor

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

Sequential Nearest Neighbor

This code does a very simple iteration through the shapes and obtains the one closest to the specified point.

 using System.Windows.Forms;
 namespace OCXTestCS
 {
     public static class NearestNeighbor
     {
 
         /// <summary>
         /// This method tests each point in the shapefile and returns the point that is closest to the specified point.
         /// This does not use any form of tree to speed up the search.
         /// </summary>
         /// <param name="position">The point to use for calculating distances</param>
         /// <param name="shapefile">The point shapefile to test</param>
         /// <returns>An integer index specifying the value.</returns>
         public static int SequentialNearestNeighbor(MapWinGIS.Point position, MapWinGIS.Shapefile shapefile)
         {
             double shortestSqrDist = double.MaxValue;
             int shortestIndex = -1;
             if (shapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POINT &&
                 shapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POINTM &&
                 shapefile.ShapefileType != MapWinGIS.ShpfileType.SHP_POINTZ)
             {
                 MessageBox.Show("The specified shapefile isn't a point shapefile.");
                 return -1;
             }
             for (int shp = 0; shp < shapefile.NumShapes; shp++)
             {
                 MapWinGIS.Point pt = shapefile.QuickPoint(shp, 0);
                 double dx = position.x - pt.x;
                 double dy = position.y - pt.y;
                 double sqrDist = dx * dx + dy * dy;
                 if (sqrDist < shortestSqrDist)
                 {
                     shortestSqrDist = sqrDist;
                     shortestIndex = shp;
                 }
             }
             return shortestIndex;
         }
     }
 }
 

Using the Class

 using System;
 using System.Windows.Forms;
 
 namespace OCXTestCS
 {
     public partial class Form1 : Form
     {
         private MapWinGIS.Shapefile _points;
         private int _layer;
         public Form1()
         {
             InitializeComponent();
             axMap1.SendMouseUp = true;
             axMap1.MouseUpEvent += new AxMapWinGIS._DMapEvents_MouseUpEventHandler(axMap1_MouseUpEvent);
         }
 
         void axMap1_MouseUpEvent(object sender, AxMapWinGIS._DMapEvents_MouseUpEvent e)
         {
             double x = 0, y = 0;
             axMap1.PixelToProj(e.x, e.y, ref x, ref y);
             MapWinGIS.Point pt = new MapWinGIS.Point();
             pt.x = x;
             pt.y = y;
             int index = NearestNeighbor.SequentialNearestNeighbor(pt, _points);
             MessageBox.Show("The index of the nearest shape is: " + index.ToString());
         }
 
       
 
         private void openToolStripMenuItem_Click(object sender, EventArgs e)
         {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "Shapefiles | *.shp";
             if (ofd.ShowDialog(this) != DialogResult.OK) return;
             _points = new MapWinGIS.Shapefile();
             if (_points.Open(ofd.FileName, null) == false)
             {
                 MessageBox.Show("Could not open file:" + _points.get_ErrorMsg(_points.LastErrorCode));
                 return;
             }
             _layer = axMap1.AddLayer(_points, true);
             
         }
     }
 }

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

This page has been accessed 1,362 times. This page was last modified on 9 December 2008, at 17:23.