MapWinGIS:SampleCode-VB Net:CopyShapes

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

Copy Shapes matching specific Field Criteria

This code is two basic parts. The first is in a simple Form, the second is a dialog form that is used simply to get an idea of the field and value that need to be copied to the newly created shapefile.

 Imports System.IO
 Public Class Form1
     Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
         Dim sourceShapefile As New MapWinGIS.Shapefile
         Dim destShapefile As New MapWinGIS.Shapefile
         Dim copyField As Integer
         Dim copyValue As Object
         Dim result As Boolean
 
         ' Use a file dialog to open an existing shapefile
         Dim ofd As New OpenFileDialog()
         ofd.Filter = "Shapefiles | *.shp"
         ofd.Title = "Select a shapefile to copy shapes from"
         If ofd.ShowDialog(Me) <> DialogResult.OK Then Exit Sub
         result = sourceShapefile.Open(ofd.FileName)
         If result = False Then
             MessageBox.Show(sourceShapefile.ErrorMsg(sourceShapefile.LastErrorCode))
             Exit Sub
         End If
 
 
         ' Create a new shapefile of the same type with a new name
         Dim sfd As New SaveFileDialog
         sfd.Filter = "Shapefiles | *.shp"
         sfd.Title = "Choose the destination filename"
         If sfd.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub
         destShapefile = New MapWinGIS.Shapefile()
 
         ' Delete the file if it exists
         Dim f As String = sfd.FileName
         If File.Exists(f) Then File.Delete(f)
         f = Path.ChangeExtension(f, ".shx")
         If File.Exists(f) Then File.Delete(f)
         f = Path.ChangeExtension(f, ".dbf")
         If File.Exists(f) Then File.Delete(f)
 
         result = destShapefile.CreateNew(sfd.FileName, sourceShapefile.ShapefileType)
         If result = False Then
             MessageBox.Show(sourceShapefile.ErrorMsg(sourceShapefile.LastErrorCode))
             Exit Sub
         End If
 
         ' use a dialog to get the field index and value to copy to the new shapefile
         Dim myFrmMembers As New frmMembers
         myFrmMembers.Shapefile = sourceShapefile
         If myFrmMembers.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then
             Exit Sub
         End If
         copyField = myFrmMembers.FieldIndex
         copyValue = myFrmMembers.FieldValue
 
 
         ' Start Editing the destination Shapefile
         result = destShapefile.StartEditingShapes()
         If result = False Then
             MessageBox.Show(destShapefile.ErrorMsg(destShapefile.LastErrorCode))
             Exit Sub
         End If
 
         ' Copy all the fields to the new shapefile
         Dim sourceField As MapWinGIS.Field
         Dim outField As Integer = 0
         For iField As Integer = 0 To sourceShapefile.NumFields - 1
             sourceField = sourceShapefile.Field(iField)
             outField = iField
             destShapefile.EditInsertField(sourceField, outField)
         Next
         destShapefile.StartEditingTable()
         ' Copy shapes, but only if they match the criteria
         Dim outShape As Integer = 0
         For shp As Integer = 0 To sourceShapefile.NumShapes - 1
             If sourceShapefile.CellValue(copyField, shp) = copyValue Then
                 Dim test As Object = sourceShapefile.CellValue(copyField, shp)
                 ' First copy the vector points
                 destShapefile.EditInsertShape(sourceShapefile.Shape(shp), outShape)
                 ' Then copy the attribute values
                 For iField As Integer = 0 To sourceShapefile.NumFields - 1
                     Dim val As Object = sourceShapefile.CellValue(iField, shp)
                     result = destShapefile.EditCellValue(iField, outShape, val)
                     If result = False Then
                         MessageBox.Show(destShapefile.ErrorMsg(destShapefile.LastErrorCode))
                         Exit Sub
                     End If
                 Next
                 outShape += 1
             End If
         Next
 
         ' Close the destination shapefile to save it
         destShapefile.StopEditingShapes(True, True)
 
     End Sub
 
 End Class



 Imports System.Windows.Forms
 
 Public Class frmMembers
     Inherits Form
 
     Public Sub New()
         InitializeComponent()
     End Sub
 
     Private MyShapefile As MapWinGIS.Shapefile
 
 
     'Required by the Windows Form Designer
     Private components As System.ComponentModel.IContainer
 
     'NOTE: The following procedure is required by the Windows Form Designer
     'It can be modified using the Windows Form Designer.  
     'Do not modify it using the code editor.
     <System.Diagnostics.DebuggerStepThrough()> _
     Private Sub InitializeComponent()
         Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
         Me.OK_Button = New System.Windows.Forms.Button
         Me.Cancel_Button = New System.Windows.Forms.Button
         Me.cmbField = New System.Windows.Forms.ComboBox
         Me.lblField = New System.Windows.Forms.Label
         Me.lblCriteria = New System.Windows.Forms.Label
         Me.cmbCriteria = New System.Windows.Forms.ComboBox
         Me.TableLayoutPanel1.SuspendLayout()
         Me.SuspendLayout()
         '
         'TableLayoutPanel1
         '
         Me.TableLayoutPanel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
         Me.TableLayoutPanel1.ColumnCount = 2
         Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
         Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
         Me.TableLayoutPanel1.Controls.Add(Me.OK_Button, 0, 0)
         Me.TableLayoutPanel1.Controls.Add(Me.Cancel_Button, 1, 0)
         Me.TableLayoutPanel1.Location = New System.Drawing.Point(94, 80)
         Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
         Me.TableLayoutPanel1.RowCount = 1
         Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
         Me.TableLayoutPanel1.Size = New System.Drawing.Size(146, 29)
         Me.TableLayoutPanel1.TabIndex = 0
         '
         'OK_Button
         '
         Me.OK_Button.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.OK_Button.Location = New System.Drawing.Point(3, 3)
         Me.OK_Button.Name = "OK_Button"
         Me.OK_Button.Size = New System.Drawing.Size(67, 23)
         Me.OK_Button.TabIndex = 0
         Me.OK_Button.Text = "OK"
         Me.OK_Button.Enabled = False
         '
         'Cancel_Button
         '
         Me.Cancel_Button.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.Cancel_Button.DialogResult = System.Windows.Forms.DialogResult.Cancel
         Me.Cancel_Button.Location = New System.Drawing.Point(76, 3)
         Me.Cancel_Button.Name = "Cancel_Button"
         Me.Cancel_Button.Size = New System.Drawing.Size(67, 23)
         Me.Cancel_Button.TabIndex = 1
         Me.Cancel_Button.Text = "Cancel"
         '
         'cmbField
         '
         Me.cmbField.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                     Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
         Me.cmbField.FormattingEnabled = True
         Me.cmbField.Location = New System.Drawing.Point(62, 12)
         Me.cmbField.Name = "cmbField"
         Me.cmbField.Size = New System.Drawing.Size(175, 21)
         Me.cmbField.TabIndex = 1
         '
         'lblField
         '
         Me.lblField.AutoSize = True
         Me.lblField.Location = New System.Drawing.Point(24, 15)
         Me.lblField.Name = "lblField"
         Me.lblField.Size = New System.Drawing.Size(32, 13)
         Me.lblField.TabIndex = 2
         Me.lblField.Text = "Field:"
         '
         'lblCriteria
         '
         Me.lblCriteria.AutoSize = True
         Me.lblCriteria.Location = New System.Drawing.Point(15, 48)
         Me.lblCriteria.Name = "lblCriteria"
         Me.lblCriteria.Size = New System.Drawing.Size(42, 13)
         Me.lblCriteria.TabIndex = 3
         Me.lblCriteria.Text = "Criteria:"
         '
         'cmbCriteria
         '
         Me.cmbCriteria.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                     Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
         Me.cmbCriteria.FormattingEnabled = True
         Me.cmbCriteria.Location = New System.Drawing.Point(62, 45)
         Me.cmbCriteria.Name = "cmbCriteria"
         Me.cmbCriteria.Size = New System.Drawing.Size(175, 21)
         Me.cmbCriteria.TabIndex = 4
         '
         'frmMembers
         '
         Me.AcceptButton = Me.OK_Button
         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
         Me.CancelButton = Me.Cancel_Button
         Me.ClientSize = New System.Drawing.Size(252, 121)
         Me.Controls.Add(Me.cmbCriteria)
         Me.Controls.Add(Me.lblCriteria)
         Me.Controls.Add(Me.lblField)
         Me.Controls.Add(Me.cmbField)
         Me.Controls.Add(Me.TableLayoutPanel1)
         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
         Me.MaximizeBox = False
         Me.MinimizeBox = False
         Me.Name = "frmMembers"
         Me.ShowInTaskbar = False
         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
         Me.Text = "Select A Field and Criteria"
         Me.TableLayoutPanel1.ResumeLayout(False)
         Me.ResumeLayout(False)
         Me.PerformLayout()
 
     End Sub
     Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
     Friend WithEvents OK_Button As System.Windows.Forms.Button
     Friend WithEvents cmbField As System.Windows.Forms.ComboBox
     Friend WithEvents lblField As System.Windows.Forms.Label
     Friend WithEvents lblCriteria As System.Windows.Forms.Label
     Friend WithEvents cmbCriteria As System.Windows.Forms.ComboBox
     Friend WithEvents Cancel_Button As System.Windows.Forms.Button
 
     Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
         Me.DialogResult = System.Windows.Forms.DialogResult.OK
         Me.Close()
     End Sub
 
     Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
         Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
         Me.Close()
     End Sub
 
     Public Property Shapefile() As MapWinGIS.Shapefile
         Get
             Return MyShapefile
         End Get
         Set(ByVal value As MapWinGIS.Shapefile)
             MyShapefile = value
             
         End Set
     End Property
 
     Public ReadOnly Property FieldIndex() As Integer
         Get
             Return cmbField.SelectedIndex
         End Get
     End Property
 
     Public ReadOnly Property FieldValue() As Object
         Get
             Return cmbCriteria.SelectedItem
         End Get
     End Property
 
 
     'Form overrides dispose to clean up the component list.
     <System.Diagnostics.DebuggerNonUserCode()> _
     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
         Try
             If disposing AndAlso components IsNot Nothing Then
                 components.Dispose()
             End If
         Finally
             MyBase.Dispose(disposing)
         End Try
     End Sub
 
     ' Populate the criteria box based on the field value
     Private Sub cmbField_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbField.SelectedIndexChanged
         Dim values As New ArrayList
         For shp As Integer = 0 To MyShapefile.NumShapes
             Dim uniqueVal As Object = MyShapefile.CellValue(cmbField.SelectedIndex, shp)
             If values.Contains(uniqueVal) = False And uniqueVal IsNot Nothing Then
                 values.Add(uniqueVal)
             End If
         Next
         values.Sort()
         cmbCriteria.Items.Clear()
         For I As Integer = 0 To values.Count - 1
             cmbCriteria.Items.Add(values(I))
         Next
         OK_Button.Enabled = False
     End Sub
 
     ' Allow ok once a field and criteria have been selected
     Private Sub cmbCriteria_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCriteria.SelectedIndexChanged
         OK_Button.Enabled = True
     End Sub
 
     Private Sub frmMembers_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
         cmbField.Items.Clear()
         For I As Integer = 0 To MyShapefile.NumFields - 1
             cmbField.Items.Add(MyShapefile.Field(I).Name)
         Next
     End Sub
 End Class


Posted by Shade1974 on 11/22/2008

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

This page has been accessed 1,729 times. This page was last modified on 23 November 2008, at 17:42.