MapWinGIS::SampleCode-CreatingPolygonSF

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 PolygonShapefile(const ShapefileName:string):boolean;
var
  FieldIndex,
  PointIndex,
  ShapeIndex,
  Data,
  i,j,
  EdgeCount     :integer;
  sf            :IShapefile;
  aField                :Ifield;
  aShape                :Ishape;
  aPoint                :Ipoint;
  cx,cy         :double;
begin
  try
    //Create a new polygon shapefile
    sf:=CoShapefile.Create;
    with sf do begin
      if not CreateNew(ChangeFileExt(Shapefilename,'.shp'),SHP_POLYGON)
      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:='Edges';
    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

    for i:=0 to 10 do begin //Create 10 random polygons

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

      EdgeCount:=Trunc(System.Random*4)+3; //Number of edges of the polygon
      cx:=System.Random*500; //Coordinates of the centre of the polygon
      cy:=System.Random*500;
      for j:=0 to EdgeCount-1 do begin

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

        //Set the coordinates of the point to be inserted
        aPoint.x:=Cos(j/EdgeCount*Pi*2)*System.Random*20+cx; //Minus sign to ensure clock-wise ordering
        aPoint.y:=Sin(j/EdgeCount*Pi*2)*System.Random*20+cy;

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

      //Last point is the same as the first to close the polygon
      aPoint:=CoPoint.Create;
      aPoint.x:=aShape.Point[0].x;
      aPoint.y:=aShape.Point[0].y;
      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 EdgeCount to the Edges attribute
      with sf do begin
        if not EditCellValue(FieldIndex, ShapeIndex, EdgeCount)
        then raise EInvalidOperation.Create('Error in adding data: '+ErrorMsg[LastErrorCode]);
      end;//with
      Inc(ShapeIndex);

    end;//for i

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

This page has been accessed 866 times. This page was last modified on 20 April 2009, at 13:25.