MapWinGIS::SampleCode-CreatingSpiralLineSF

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

function SpiralShapefile(const ShapefileName:string):boolean;
var
  FieldIndex,
  PointIndex,
  ShapeIndex,
  Data,
  i             :integer;
  sf            :IShapefile;
  aField                :Ifield;
  aShape                :Ishape;
  aPoint                :Ipoint;
begin
  try
    //Create a new polyline shapefile
    sf:=CoShapefile.Create;
    with sf do begin
      if not CreateNew(ChangeFileExt(Shapefilename,'.shp'),SHP_POLYLINE)
      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;
    PointIndex:=0;
    ShapeIndex:=0;

    with sf do begin
      if not EditInsertField(aField,FieldIndex,nil)
      then raise EInvalidOperation.Create('Error in adding field: '+ErrorMsg[LastErrorCode]);
    end;//with

    //Create and add shape
    aShape:=CoShape.Create;
    with aShape do begin
      if not Create(SHP_POLYLINE)
      then raise EInvalidOperation.Create('Error in creating shape: '+ErrorMsg[LastErrorCode]);
    end;//with

    for i:=0 to 720 do begin //Create a spiralling line

      //Create a new Point shape
      aPoint:=CoPoint.Create;

      //Set the coordinates of the point to be inserted
      aPoint.x:=Cos(i/180*Pi)*i;
      aPoint.y:=Sin(i/180*Pi)*i;

      //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

    end;//for

    //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

    //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::SampleCode-CreatingSpiralLineSF"

This page has been accessed 731 times. This page was last modified on 20 April 2009, at 12:59.