MapWindow Home
Search MapWindow.org


  • Home
    • Download
    • Why MW
    • FAQ
    • Contact
  • Products
    • MapWindow
    • ActiveX
    • Plug-ins
  • Software Dev
    • Sample Code
    • Scripts
    • Source
    • Bugs
    • Support
    • Languages
  • Site Tools
    • Forums
    • Tutorials
    • Mailing Lists
    • Team
    • Site Stats
    • Links
  • Get Stuff
    • Buy a CD
    • Buy a T-Shirt
    • MW Book
    • ActiveX Book
    • Donate
    • Buy Support
  • Documentation
    • MWGIS App. User Guide
    • GISTools User Guide
    • MW Plug-ins Dev. Guide
    • MWGIS ActiveX Dev. Guide
    • MWGeoProc Dev. Guide

MapWinGIS:Shapefile CreateNew

From MapWindow GIS

Jump to: navigation, search

CreateNew

Creates a new shapefile with the specified filename and type. After a shapefile is created, the attribute table and shapefile are automatically in editing mode. At least one field is required in the table to be a valid shapefile.
See also ShpfileType


VB.NET Usage

Function CreateNew(ShapefileName As String, ShapefileType As MapWinGIS.ShpfileType) As Boolean

            Parameters

ShapefileName

The filename to use for the new shapefile.
ShapefileType The type of shapefile to be created.
ReturnValue A boolean value representing the success or failure of creating the shapefile.

Sample Code

    Private Sub NewShapefile()
        Dim sf As New MapWinGIS.Shapefile()
        Dim success As Boolean
        'Create a new polygon shapefile
        success = sf.CreateNew("test.shp", MapWinGIS.ShpfileType.SHP_POLYGON)
    End Sub
  

VB 6 Usage
Function CreateNew(ShapefileName As String, ShapefileType As ShpfileType) As Boolean
            Parameters

ShapefileName

The filename to use for the new shapefile.
ShapefileType The type of shapefile to be created.
ReturnValue A boolean value representing the success or failure of creating the shapefile.

Sample Code

 Function newShapefile(filename As String) As Boolean
 On Error GoTo Error_handler
 
   Dim success As Boolean
   Dim fieldindex As Long
   Dim shapeIndex As Long
   Dim data As Integer
   'objects
   Dim sf As MapWinGIS.Shapefile
   Dim field As MapWinGIS.field
   Dim shape As MapWinGIS.shape
   Dim point As MapWinGIS.point
   
   'Create a new polygon shapefile
   Set sf = New MapWinGIS.Shapefile
   With sf
     success = .CreateNew(filename, MapWinGIS.ShpfileType.SHP_POINT)
     If Not success Then MsgBox ("Error in creating shapefile: " & .ErrorMsg(.LastErrorCode))
   End With 'sf
   'After a shapefile is created, the attribute table and
   'shapefile are automatically in editing mode
   
   'At least one field is required in the table to be a valid shapefile.
   Set field = New MapWinGIS.field
     
   field.name = "MWShapeID"
   field.Width = 10
   field.Type = MapWinGIS.FieldType.INTEGER_FIELD
   
   fieldindex = 0
   With sf
     success = .EditInsertField(field, fieldindex)
     If Not success Then
       MsgBox ("Error in adding field: " & .ErrorMsg(.LastErrorCode))
       GoTo Error_handler
     End If
   End With ' sf
   
   'Create and add shape
   Set shape = New MapWinGIS.shape
     
   'Create a new Point shape
   With shape
     success = .Create(MapWinGIS.ShpfileType.SHP_POINT)
     If Not success Then
       MsgBox ("Error in creating shape: " & .ErrorMsg(.LastErrorCode))
       GoTo Error_handler
     End If
   End With ' shape
     
   Set point = New MapWinGIS.point
   
   'Set the values for the point to be inserted
   point.x = 100
   point.y = 100
   'Insert the point into the shape
   With shape
     success = .InsertPoint(point, shape.numPoints)
     If Not success Then
       MsgBox ("Error in adding point: " & .ErrorMsg(.LastErrorCode))
       GoTo Error_handler
     End If
   End With ' shape
   
   'Insert the shape into the shapefile
   shapeIndex = 0
   With sf
     success = .EditInsertShape(shape, shapeIndex)
     If Not success Then
       MsgBox ("Error in adding field: " & .ErrorMsg(.LastErrorCode))
       GoTo Error_handler
     End If
   End With ' sf
   
   'Add value to at least one attribute
   'Use shapeindex as dummy value:
   data = shapeIndex
   With sf
     success = .EditCellValue(fieldindex, shapeIndex, data)
     If Not success Then
       MsgBox ("Error in adding field: " & .ErrorMsg(.LastErrorCode))
         GoTo Error_handler
     End If
   End With ' sf
   
   'Stop editing shapes in the shapefile, saving changes to shapes,
   'also stopping editing of the attribute table
   With sf
     success = sf.StopEditingShapes(True, True)
     If Not success Then
       MsgBox ("Error in adding field: " & .ErrorMsg(.LastErrorCode))
       GoTo Error_handler
     End If
   End With ' sf
   
   newShapefile = True
   
 Cleanup:
   On Error Resume Next
   Set field = Nothing
   Set shape = Nothing
   Set point = Nothing
   Set sf = Nothing
   
   Exit Function
 
 Error_handler:
   newShapefile = False
   GoTo Cleanup
 End Function
Retrieved from "http://www.mapwindow.org/wiki/index.php/MapWinGIS:Shapefile_CreateNew"
Views
  • Page
  • Discussion
  • View source
  • History
Personal tools
  • Log in / create account
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link

Geospatial Software Lab

Copyright © 2008 - MapWindow Open Source Team
( last updated: December 03 2008.)
Home | Donate | Download | MapWindow GIS Desktop | MapWinGIS.ocx | Plug-ins | Contact | Lists | Bugs | Sample Code | Documentation
Code Repository | Open Source License | Discussion Forum | Wiki | Join the Team | Site Statistics | Links