Web API Reference | MapGuide Open Source

- Overview
- Modules
- Classes

# MgPolygon Class Reference
[MgPolygon]

An MgPolygon is a region, defined by 1 exterior boundary and 0 or more interior boundaries. More...

Inherits MgRegion.

List of all members.

## Public Member Functions
virtual MgGeometricEntity * | Copy ()
virtual MgCoordinateIterator * | GetCoordinates ()
| Returns an iterator over the coordinates included in this geometric entity.
virtual INT32 | GetDimension ()
virtual MgLinearRing * | GetExteriorRing ()
| Gets the exterior ring of the polygon.
virtual INT32 | GetGeometryType ()
virtual MgLinearRing * | GetInteriorRing (INT32 index)
virtual INT32 | GetInteriorRingCount ()
virtual bool | IsClosed ()
virtual bool | IsEmpty ()
virtual MgGeometricEntity * | Transform (MgTransform *transform)
| Returns a transformed copy of this geometric entity.

## Detailed Description

An MgPolygon is a region, defined by 1 exterior boundary and 0 or more interior boundaries.

Remarks:
  Each interior boundary defines a hole in the polygon. The interior and exterior boundaries are defined with linear interpolation between points. Each consecutive pair of points defines a line segment. An object of this type is constructed by calling a non-static MgGeometryFactory::CreatePolygon() method and, once constructed, is immutable.

Example (PHP)
The following example code creates a square polygon, whose side is 3 units in length, and which contains a "hole" at its center. The "hole" is also a square, whose side is 1 unit in length.

     $geometryFactory = new MgGeometryFactory();
     $coordinateCollection = new MgCoordinateCollection();
     $interiorRingCollection = new MgLinearRingCollection();
     $wktReaderWriter = new MgWktReaderWriter();
     // note that the coordinates for the external boundary
     // are added to the coordinate collection in a
     // counterclockwise order
     // create first coordinate for external boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(0,0);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create second coordinate for external boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(3,0);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create third coordinate for external boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(3,3);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create fourth coordinate for external boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(0,3);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create fifth coordinate for external boundary that is the same as
     // the first to close the boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(0,0);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create LinearRing that defines the external boundary
     $exteriorRing = $geometryFactory->CreateLinearRing($coordinateCollection);
     // clear the collection before reusing for internal boundary
     $coordinateCollection->Clear();

     // note that the coordinates for the internal boundary
     // are added to the coordinate collection in a
     // clockwise order
     // create first coordinate for internal boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(1,1);

     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create second coordinate for internal boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(1,2);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create third coordinate for internal boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(2,2);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create fourth coordinate for internal boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(2,1);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create fifth coordinate for internal boundary that is the same as
     // the first to close the boundary
     $coordinate = $geometryFactory->CreateCoordinateXY(1,1);
     $index = $coordinateCollection->Add($coordinate);
     echo "Coordinate added to coordinate collection at index: $index\n";

     // create LinearRing that defines the internal boundary
     $interiorRing =
     $geometryFactory->CreateLinearRing($coordinateCollection);

     // add the interior ring to the interior ring collection
     $index = $interiorRingCollection->Add($interiorRing);

     // create the MgPolygon geometry
     $polygon = $geometryFactory->CreatePolygon($exteriorRing, $interiorRingCollection);

     // print out the Agf Text string for the geometry
     $polygonAgfText = $wktReaderWriter->Write($polygon);
     echo "AGF Text representation of line string: $polygonAgfText\n";

Example (C#)
The following example code creates a square polygon, whose side is 3 units in length, and which contains a "hole" at its center. The "hole" is also a square, whose side is 1 unit in length.

     using OSGeo.MapGuide;

     private MgWktReaderWriter wktReaderWriter;
     private MgGeometryFactory geometryFactory;
     // this polygon has one exterior ring and one interior ring
     private MgPolygon pner1141441411ir2223333222;
     // the following array is used for the exterior ring of a polygon
     // the points are in counter-clockwise order
     private double[,] da1141441411 = { { 1, 1 }, { 4, 1 }, { 4, 4 }, { 1, 4 }, { 1, 1 } };
     // the following array is used for the interior ring of a polygon
     // the points are in clockwise order
     private double[,] da2223333222 = { { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 }, { 2, 2 } };
     // this array contains an array that will be used for an exterior ring
     // and one that will be used for an interior ring
     private double[][,] pnEr1141441411Ir2223333222Data;
     private String geometryAgfText;

     public MgLinearRing CreateALinearRingXY(double[,] ringData)
     {
         MgCoordinateCollection coords = new MgCoordinateCollection();
         for (int i = 0; i < ringData.GetLength(0); i++)
         {
             coords.Add(geometryFactory.CreateCoordinateXY(ringData[i, 0], ringData[i, 1]));
         }
         return geometryFactory.CreateLinearRing(coords);
     }

     public MgPolygon CreateAPolygonXY(double[][,] ringsData)
     {
         MgLinearRing exteriorRing = CreateALinearRingXY(ringsData[0]);
         MgLinearRingCollection interiorRings = new MgLinearRingCollection();
         for (int i = 1; i < ringsData.GetLength(0); i++)
         {
             interiorRings.Add(CreateALinearRingXY(ringsData[i]));
         }
         return geometryFactory.CreatePolygon(exteriorRing, interiorRings);
     }

     geometryFactory = new MgGeometryFactory();
     pnEr1141441411Ir2223333222Data = new double[2][,];
     pnEr1141441411Ir2223333222Data[0] = da1141441411;
     pnEr1141441411Ir2223333222Data[1] = da2223333222;
     // create a polygon using the geometry factory
     pner1141441411ir2223333222 = CreateAPolygonXY(pnEr1141441411Ir2223333222Data);

     // print out the Agf Text string for the geometry
     wktReaderWriter = new MgWktReaderWriter();
     geometryAgfText = wktReaderWriter.Write(pner1141441411ir2223333222);
     // geometryAgfText now contains:
     // "POLYGON XY ((1 1, 4 1, 4 4, 1 4, 1 1), (2 2, 2 3, 3 3, 3 2, 2 2))"

Comments or suggestions? Send us feedback.