FAQ
ArcPy (ArcMap): ラインまたはポリゴンの頂点座標をテキスト ファイルに出力する

ナレッジ番号:2602 | 登録日:2023/05/29 | 更新日:2024/11/21

説明

ライン フィーチャクラスまたはポリゴン フィーチャクラスに格納されているすべてのフィーチャについて、頂点の X 座標と Y 座標をテキスト ファイルに出力するサンプル コードです。

Image

サンプル コード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#coding:cp932
# Description: ジオデータベースのポリゴンまたはライン フィーチャクラスの
#              すべての頂点座標をファイルに出力します。
# Author: ESRIジャパン
 
import arcpy
 
# フィーチャクラスのパス
infc = "c:/data/fgdb.gdb/polygon1"
 
# 出力先の csv ファイルを書き込みモードで開く
f = open("c:/data/output.txt", "w")
 
# ジオメトリを格納するフィールド名を取得
desc = arcpy.Describe(infc)
shapefieldname = desc.shapeFieldName
 
# サーチ カーソルの作成
rows = arcpy.SearchCursor(infc)
 
# すべてのフィーチャ(または行)に対してループで実行
for row in rows:
    # フィーチャ(または行)の座標値を取得する
    feat = row.getValue(shapefieldname)
     
    # フィーチャ ID をファイルに出力する
    outputstring = "フィーチャID %i:\n" % row.getValue(desc.OIDFieldName)
    f.write(outputstring)
    partnum = 0 # パート番号をリセット
 
    # フィーチャのパートごとの処理
    for part in feat:
        # パート番号をファイルに出力する
        outputstring = "パート番号 %i:\n" % partnum
        f.write(outputstring)
 
        # パートに含まれるポイントをファイルに出力する
        for pnt in feat.getPart(partnum):
            if pnt:
                # ポイントのX座標とY座標を出力用文字列に代入
                outputstring = str(pnt.X) + " " + str(pnt.Y) + "\n"
            else:
                # ポリゴンの内側のリング
                # パート番号を出力用文字列に代入
                partnum += 1
                outputstring = "パート番号 %i (内側のリング):\n" % partnum
            f.write(outputstring)
        partnum += 1
 
# オブジェクトへの参照を解放
del row, rows
 
# ファイルを閉じる
f.close()

関連する質問

メタデータ

種類

製品