MapWinGIS:SampleCode-VB Net:ExportSelectedShapes

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

Export Selected Shapes

Copies seleced shapes into a new shapefile.

     Private Sub doExportSelected()
         '***********************************************************************
         'Added 01.26.2006 - ADK
         'Exports all of the features in the current selection out to a shapefile
         '***********************************************************************
         'Declarations
         Dim Result As Boolean
         Dim cdlSave As New SaveFileDialog
         Dim sLayerName, sFileName, sLayerType As String
         Dim iFileCnt As Integer = 1
         Dim SelectedShape As SHRIMP.SelectedShape
         Dim myShapeFile, newShapefile As MapWinGIS.Shapefile
         Dim myShape As MapWinGIS.Shape
         Dim ShapefileType As MapWinGIS.ShpfileType
         Dim iShapeHandle, iFieldCnt As Integer
 
         'First check to see if any features have been selected
         If m_View.SelectedShapes.NumSelected <= 0 Then Exit Sub
 
         'Warn user if they are trying to export alot of shapes
         If m_View.SelectedShapes.NumSelected > 1000 Then
             If MessageBox.Show("Warning: More than 1,000 features selected.  Export process may take a long time. " & _
              vbCrLf & "Are you sure you want to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = _
             Windows.Forms.DialogResult.No Then
                 Exit Sub
             End If
         End If
         Try
             'Iterate through each of the selected feature
             For i As Integer = 0 To m_View.SelectedShapes.NumSelected - 1
                 'Set to the selected shape
                 SelectedShape = m_View.SelectedShapes(i)
 
                 'Check to see if this is the first iteration of the layer (selection is grouped by layer)
                 If sLayerName <> Strings.LCase(m_layers.Item(SelectedShape.Layer).Name) Then
                     sLayerName = Strings.LCase(m_layers.Item(SelectedShape.Layer).Name) 'Set layer name
 
                     If i > 0 Then
                         'clean up - not first pass
                         newShapefile.Close()
                         newShapefile = Nothing
                         iFileCnt = 1
                     End If
 
                     'Get the shapefile of the selected shape
                     myShapeFile = CType(frmMain.MapMain.get_GetObject(SelectedShape.Layer), MapWinGIS.Shapefile)
 
                     'This converts the selected shape to a MapWinGIS.Shape
                     myShape = myShapeFile.Shape(SelectedShape.ShapeIndex)
                     If IsNothing(myShape) Then GoTo ERRORHANDLER
 
                     'Determine if shape is polygon, line, or point
                     sLayerType = Strings.LCase(m_layers.Item(SelectedShape.Layer).LayerType.ToString)
                     If InStr(sLayerType, "line", CompareMethod.Text) > 0 Then
                         'line shapefile
                         ShapefileType = MapWinGIS.ShpfileType.SHP_POLYLINE
                     ElseIf InStr(sLayerType, "polygon", CompareMethod.Text) > 0 Then
                         'polygon shapefile
                         ShapefileType = MapWinGIS.ShpfileType.SHP_POLYGON
                     ElseIf InStr(sLayerType, "point", CompareMethod.Text) > 0 Then
                         'point shapefile
                         ShapefileType = MapWinGIS.ShpfileType.SHP_POINT
                     Else
                         'Could not determine layer type
                         GoTo ERRORHANDLER
                     End If
                     'Ask the user where they would like to save this new layer - default to their own directory
                     'Get a new file name that does not already exist
                     Do Until System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\" & m_layers.Item(SelectedShape.Layer).Name & iFileCnt & ".shp") = False
                         iFileCnt += 1
                     Loop
                     sFileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\" & m_layers.Item(SelectedShape.Layer).Name & iFileCnt & ".shp"
                     cdlSave.FileName = sFileName
                     cdlSave.Filter = "Shapefile (*.shp)|*.shp"
                     cdlSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
                     cdlSave.ValidateNames = True
                     If (cdlSave.ShowDialog = DialogResult.Cancel) Then
                         'User clicked cancel
                         MessageBox.Show("User canceled save of selected features.")
                         Exit For
                     Else
                         'Save the new shapefile
                         sFileName = Trim(cdlSave.FileName)
                         If System.IO.File.Exists(sFileName) Then
                             System.IO.File.Delete(sFileName)
                         End If
                         'Create the new shapefile
                         newShapefile = New MapWinGIS.Shapefile
                         Result = newShapefile.CreateNew(sFileName, ShapefileType)
                         If Result = False Then GoTo ErrorHandler
 
                         'The new shapefile has no fields at this point
                         For iFieldCnt = 0 To myShapeFile.NumFields - 1
                             newShapefile.EditInsertField(myShapeFile.Field(iFieldCnt), iFieldCnt)
                         Next iFieldCnt
 
                         'Start an edit session in the shapefile
                         Result = newShapefile.StartEditingShapes(True, Nothing)
                         If Result = False Then GoTo ErrorHandler
 
                         'insert the selected shape
                         iShapeHandle = newShapefile.NumShapes
                         Result = newShapefile.EditInsertShape(myShape, iShapeHandle)
                         If Result = False Then GoTo ErrorHandler
 
                         'Populate the aspatial data
                         For iFieldCnt = 0 To myShapeFile.NumFields - 1
                             newShapefile.EditCellValue(iFieldCnt, iShapeHandle, myShapeFile.CellValue(iFieldCnt, SelectedShape.ShapeIndex))
                         Next iFieldCnt
 
                         'end the edit session
                         Result = newShapefile.StopEditingShapes(True, True)
                         If Result = False Then GoTo ErrorHandler
 
                         Result = newShapefile.Close
                         If Result = False Then GoTo ErrorHandler
 
                     End If
                 Else
                     'Not the first iteration of the layer - just move shape over
                     Result = newShapefile.Open(sFileName, Nothing)
                     If Result = False Then GoTo ErrorHandler
 
                     'Start an edit session in the shapefile
                     Result = newShapefile.StartEditingShapes(True, Nothing)
                     If Result = False Then GoTo ErrorHandler
 
                     'This converts the selected shape to a MapWinGIS.Shape
                     myShape = myShapeFile.Shape(SelectedShape.ShapeIndex)
                     If IsNothing(myShape) Then Stop
 
                     'insert the selected shape
                     iShapeHandle = newShapefile.NumShapes
                     Result = newShapefile.EditInsertShape(myShape, iShapeHandle)
                     If Result = False Then GoTo ErrorHandler
 
                     'Populate the aspatial data
                     For iFieldCnt = 0 To myShapeFile.NumFields - 1
                         newShapefile.EditCellValue(iFieldCnt, iShapeHandle, myShapeFile.CellValue(iFieldCnt, SelectedShape.ShapeIndex))
                     Next iFieldCnt
 
                     'end the edit session
                     Result = newShapefile.StopEditingShapes(True, True)
                     If Result = False Then GoTo ErrorHandler
                 End If
                 SelectedShape = Nothing
                 myShape = Nothing
                 GC.Collect()
             Next i  'Move to next selected shape
 
             MessageBox.Show("Done exporting " & m_View.SelectedShapes.NumSelected & " features.")
 
         Catch ex As Exception
             ShowError(ex)
         Finally
             'Clean Up
             SelectedShape = Nothing
             myShape = Nothing
             newShapefile = Nothing
             myShapeFile = Nothing
         End Try
         Exit Sub
 ERRORHANDLER:
         'ToDo:  Generate error
 
     End Sub

Code originally posted in phorum by Amydalanya on 1/26/2006

Code added to Wiki by Shade1974 on 1/30/2006

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

This page has been accessed 3,728 times. This page was last modified on 30 January 2006, at 18:38.