MapWinGIS:Shapefile CreateNew

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

Contents

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 and CreateNewWithShapeID

If you pass NULL as the ShapefileName an in-memory shapefile will be created.


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

Delphi 6 usage

Function CreateNew(ShapefileName:string; ShapefileType:ShpfileType):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

This code creates a shapefile containing 100 points located at random positions

function NewPointShapefile(const ShapefileName:string):boolean;
{This code creates a shapefile containing 100 points located at random positions}
var
  FieldIndex,
  PointIndex,
  ShapeIndex,
  Data,
  i             :integer;
  sf            :IShapefile;
  aField                :Ifield;
  aShape                :Ishape;
  aPoint                :Ipoint;
begin
  try
    //Create a new polygon shapefile
    sf:=CoShapefile.Create;
    with sf do begin
      if not CreateNew(ChangeFileExt(Shapefilename,'.shp'),SHP_POINT)
      then raise EInOutError.Create('Error in creating shapefile: '+ErrorMsg[LastErrorCode]);
    end;//with
    //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.
    aField:=CoField.Create;
    aField.Name:='ID';
    aField.Width:=10;
    aField.Type_:=INTEGER_FIELD;
 
    FieldIndex:=0;
    ShapeIndex:=0;
    PointIndex:=0;
 
    with sf do begin
      if not EditInsertField(aField,FieldIndex,nil)
      then raise EInvalidOperation.Create('Error in adding field: '+ErrorMsg[LastErrorCode]);
    end;//with
 
    for i:=1 to 100 do begin // Insert 100 random points
 
      //Create and add shape
      aShape:=CoShape.Create;
      with aShape do begin
        if not Create(SHP_POINT)
        then raise EInvalidOperation.Create('Error in creating shape: '+ErrorMsg[LastErrorCode]);
      end;//with
 
      //Create a new Point shape
      aPoint:=CoPoint.Create;
 
      //Set the coordinates of the point to be inserted
      aPoint.x:=System.random*100;
      aPoint.y:=System.random*100;
 
      //Insert the point into the shape
      PointIndex:=aShape.numPoints;
      with aShape do begin
        if not InsertPoint(aPoint,PointIndex)
        then raise EInvalidOperation.Create('Error in adding point: '+ErrorMsg[LastErrorCode]);
      end;// with
 
      //Insert the shape into the shapefile
      with sf do begin
        if not EditInsertShape(aShape, ShapeIndex)
        then raise EInvalidOperation.Create('Error in adding shape: '+ErrorMsg[LastErrorCode]);
      end;//with
 
      //Add value to at least one attribute
      //Use shapeindex as dummy value:
      Data:=ShapeIndex;
      with sf do begin
        if not EditCellValue(FieldIndex, ShapeIndex, Data)
        then raise EInvalidOperation.Create('Error in adding data: '+ErrorMsg[LastErrorCode]);
      end;//with
      Inc(ShapeIndex);
 
    end;//for
 
    //Stop editing shapes in the shapefile, saving changes to shapes,
    //also stopping editing of the attribute table
    with sf do begin
      if not StopEditingShapes(True, True, nil)
      then raise EInvalidOperation.Create('Error in stopping editing: '+ErrorMsg[LastErrorCode]);
    end;//with
 
  finally
    {Cleanup}
    {All interfaces are automatically freed by Delphi when leaving this procedure}
  end;//finally
end;

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:Shapefile_CreateNew"

This page has been accessed 6,801 times. This page was last modified on 14 April 2010, at 08:17.