FAQ
ArcObjects におけるポリゴンの作成方法

ナレッジ番号:5274 | 登録日:2023/07/26 | 更新日:2024/10/09

概要

プログラムでポリゴン ジオメトリを新規に作成する方法は、以下の通り複数あります。

  • ポイントから作成する方法
  • セグメントを連ねて作成する方法
  • 既存のジオメトリから作成する方法
  • 既存の矩形(Envelope)から作成する方法

それぞれについてサンプルコードとともに紹介します。

サンプル コード

ポイントからポリゴンを作成する方法

この方法は、入力として 一連の頂点がある場合に適しています。 結果のポリゴン リングは直線のライン セグメントだけを含みます。
public void CreatePolygonByPoints()
{
    IGeometryBridge2 pGeometryBridge2 = new GeometryEnvironmentClass();
    IPointCollection4 pPointCollection4 = new PolygonClass();
    //pointCollection4.SpatialReference = '新しく作成するポリゴンの空間参照を定義する。
    WKSPoint[] aWKSPointBuffer = null;
    long cPoints = 4; //最初のパートのポイント数
    aWKSPointBuffer = new WKSPoint[System.Convert.ToInt32(cPoints - 1) + 1];

    //ポイントバッファにポイントを読み込む。
    aWKSPointBuffer[0].X = 0;
    aWKSPointBuffer[0].Y = 0;
    aWKSPointBuffer[1].X = 0;
    aWKSPointBuffer[1].Y = 100;
    aWKSPointBuffer[2].X = 100;
    aWKSPointBuffer[2].Y = 100;
    aWKSPointBuffer[3].X = 100;
    aWKSPointBuffer[3].Y = 0;

    pGeometryBridge2.SetWKSPoints(pPointCollection4, ref aWKSPointBuffer);
    //pointCollection4 が定義された。
    //pointCollection4 を IPolygon 等にキャストし、ポリゴンとして扱う。

}
Public Sub CreatePolygonByPoints()
    Dim pGeometryBridge2 As IGeometryBridge2 = New GeometryEnvironmentClass
    Dim pPointCollection4 As IPointCollection4 = New PolygonClass
    'pointCollection4.SpatialReference = '新しく作成するポリゴンの空間参照を定義する。    
    Dim aWKSPointBuffer() As WKSPoint
    Dim cPoints As Long = 4 '最初のパートのポイント数
    ReDim aWKSPointBuffer(0 To CInt(cPoints - 1))

    'ポイント バッファにポイントを読み込む。
    aWKSPointBuffer(0).X = 0
    aWKSPointBuffer(0).Y = 0
    aWKSPointBuffer(1).X = 0
    aWKSPointBuffer(1).Y = 100
    aWKSPointBuffer(2).X = 100
    aWKSPointBuffer(2).Y = 100
    aWKSPointBuffer(3).X = 100
    aWKSPointBuffer(3).Y = 0

    pGeometryBridge2.SetWKSPoints(pPointCollection4, aWKSPointBuffer)
    'pointCollection4 が定義された。
    'pointCollection4 を IPolygon 等にキャストし、ポリゴンとして扱う。
End Sub

セグメントからポリゴンを作成する方法

以下のコード例では、マルチパートのポリゴンがセグメント単位で構築されます。 高度な構成やカーブしているセグメントを利用する時に、一番制御できる方法です。
public void CreatePolygonBySegments()
{
    IPolygon pPolygon = new PolygonClass();
    ICircularArc pCircularArc = new CircularArcClass();
    ISegment segment1 = (ISegment)pCircularArc;
    ISegmentCollection ring1 = new RingClass();
    ring1.AddSegment(segment1);

    IBezierCurveGEN pBezierCurve = new BezierCurveClass();
    ISegment segment2 = (ISegment)pBezierCurve;
    ISegmentCollection ring2 = new RingClass();
    ring2.AddSegment(segment2);

    IGeometryCollection pGeometryCollection = (IGeometryCollection)pPolygon;

    IGeometry geometry1 = (IGeometry)ring1;
    pGeometryCollection.AddGeometry(geometry1);

    IGeometry geometry2 = (IGeometry)ring2;
    pGeometryCollection.AddGeometry(geometry2);

    IPoint point = new PointClass();
    point.X = -10;
    point.Y = 0;

    pCircularArc.PutCoordsByAngle(point, 0, 2 * 3.14159265358979, 10.0);
    //1つ目のリングの座標値を定義。ここでは円。

    IPoint[] pntArray = new IPoint[4];
    long i = 0;
    for (i = 0; i <= 3; i++)
    {
        pntArray[System.Convert.ToInt32(i)] = new PointClass();
    }

    pntArray[0].X = 10;
    pntArray[0].Y = 0;
    pntArray[1].X = 10;
    pntArray[1].Y = 10;
    pntArray[2].X = 20;
    pntArray[2].Y = 10;
    pntArray[3].X = 10;
    pntArray[3].Y = 0;
    pBezierCurve.PutCoords(ref pntArray);
    //2つ目めのリングの座標値を定義。ここではベジェカーブ。

    pGeometryCollection.GeometriesChanged();
}
Public Sub CreatePolygonBySegments()

    Dim pPolygon As IPolygon = New PolygonClass
    Dim pCircularArc As ICircularArc = New CircularArcClass
    Dim segment1 As ISegment = CType(pCircularArc, ISegment)
    Dim ring1 As ISegmentCollection = New RingClass
    ring1.AddSegment(segment1)

    Dim pBezierCurve As IBezierCurveGEN = New BezierCurveClass
    Dim segment2 As ISegment = CType(pBezierCurve, ISegment)
    Dim ring2 As ISegmentCollection = New RingClass
    ring2.AddSegment(segment2)

    Dim pGeometryCollection As IGeometryCollection = CType(pPolygon, IGeometryCollection)

    Dim geometry1 As IGeometry = CType(ring1, IGeometry)
    pGeometryCollection.AddGeometry(geometry1)

    Dim geometry2 As IGeometry = CType(ring2, IGeometry)
    pGeometryCollection.AddGeometry(geometry2)
    Dim point As IPoint = New PointClass
    point.X = -10
    point.Y = 0

    pCircularArc.PutCoordsByAngle(point, 0, 2 * 3.14159265358979, 10.0#)
    '1つ目のリングの座標値を定義。ここでは円。

    Dim pntArray(0 To 3) As IPoint
    Dim i As Long
    For i = 0 To 3
        pntArray(CInt(i)) = New PointClass
    Next i

    pntArray(0).X = 10
    pntArray(0).Y = 0
    pntArray(1).X = 10
    pntArray(1).Y = 10
    pntArray(2).X = 20
    pntArray(2).Y = 10
    pntArray(3).X = 10
    pntArray(3).Y = 0
    pBezierCurve.PutCoords(pntArray)
    '2つ目のリングの座標値を定義。ここではベジェカーブ。

    pGeometryCollection.GeometriesChanged()

End Sub

既存のジオメトリから作成する方法

既存のポリゴンから作成

ポリゴンは既存のジオメトリの間の位相的な関係に基づいて作成することができます。 以下のコード例では、2つの既存のポリゴンをまとめることでポリゴンを生成します。
public void CreatePolygonFromExistingGeometries(ref IPolygon polygon1, ref IPolygon
    polygon2)
{
    //2つの既存のポリゴンを合成することで新しいポリゴンを作成する。
    ITopologicalOperator2 pTopologicalOperator2 = (ITopologicalOperator2)polygon1;

    //単純化
    pTopologicalOperator2.IsKnownSimple_2 = false;
    pTopologicalOperator2.Simplify();

    IGeometry geometry = pTopologicalOperator2.Union(polygon2);
    IPolygon polygonCombined = (IPolygon)geometry;
    //polygonCombined は新しく生成されたポリゴン。
}
Public Sub CreatePolygonFromExistingGeometries(ByRef polygon1 As IPolygon, ByRef polygon2 As IPolygon)
    
    '2つの既存のポリゴンを合成することで新しいポリゴンを作成する。
    Dim pTopologicalOperator2 As ITopologicalOperator2 = CType(polygon1, ITopologicalOperator2)
    
    '単純化
    pTopologicalOperator2.IsKnownSimple_2 = False
    pTopologicalOperator2.Simplify()
    
    Dim geometry As IGeometry = pTopologicalOperator2.Union(polygon2)
    Dim polygonCombined As IPolygon = CType(geometry, IPolygon)  
    'polygonCombined は新しく生成されたポリゴン。
End Sub

既存の矩形(Envelope)から作成

public void CreatePolygonByenvelope(IEnvelope penv)
{
    ISegmentCollection pSegmentCollection = new PolygonClass();
    pSegmentCollection.SetRectangle(penv);
}
 Public Sub CreatePolygonByEnvelope(penv As IEnvelope)
    Dim pSegmentCollection As IPolygon
    pSegmentCollection = New Polygon
    pSegmentCollection.SetRectangle(penv)
End Sub

メタデータ

機能

種類

製品