MapWinGIS:SampleCode-VB Net:IteratePointsAttributes

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

     'Example Code written by Ted Dunsford April 28, 2008
     'This code illustrates some tips and tricks for iterating through the points
     'in a shapefile and also shows looping through the attribute values.
     Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
         Dim ofd As New OpenFileDialog
         Dim iShape, iField, iPoint As Integer
         Dim point As MapWinGIS.Point
         Dim value As Object
 
         ' Dimension a new shapefile
         sf = New MapWinGIS.Shapefile
 
         ' Open the shapfile
         ofd.Filter = "Shapefiles (*.shp) |*.shp"
         If Not ofd.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then Return
         sf.Open(ofd.FileName)
 
         ' Iterating through a shapefile is much faster than creating each shape and iterating through it
         ' because the values are stored in the shapefile, but "shape" objects are created dynamically.
         For iShape = 0 To sf.NumShapes
             For iPoint = 0 To sf.numPoints(iShape)
                 point = sf.QuickPoint(iShape, iPoint)
                 ' point has the x, y, Z and m values for the coordinates.
             Next
         Next
 
         ' Don't do this because it would be slower.  This is still ok, however, if you need
         ' some charactersitic that is on the shape object but not in the shapefile.
         For iShape = 0 To sf.NumShapes
             Dim myShape As MapWinGIS.Shape = sf.Shape(iShape)
             For iPoint = 0 To myShape.numPoints
                 point = myShape.Point(iPoint)
             Next
         Next
 
         ' This last example is CATASTROPHICALLY SLOW!  don't do this:
         For iShape = 0 To sf.NumShapes
             For iPoint = 0 To sf.Shape(iShape).numPoints
                 point = sf.Shape(iShape).Point(iPoint)
             Next
         Next
         ' The reason the above code is slow is that you are instantiating a 
         ' new shape object twice for each point and filling it with all the 
         ' points for that shape over and over again.
 
         ' Cycle through all the field values in the attribute table
         For iShape = 0 To sf.NumShapes
             For iField = 0 To sf.NumFields
                 value = sf.CellValue(iField, iShape)
                 ' value can be a double, integer or string.
             Next
         Next
     End Sub

Posted by Ted Dunsford April 28, 2008

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-VB_Net:IteratePointsAttributes"

This page has been accessed 3,255 times. This page was last modified on 28 April 2008, at 17:11.