Skip to content

Instantly share code, notes, and snippets.

@sanvarie
Last active February 9, 2016 22:53
Show Gist options
  • Save sanvarie/407e79edca629f1a126c to your computer and use it in GitHub Desktop.
Save sanvarie/407e79edca629f1a126c to your computer and use it in GitHub Desktop.
フィーチャクラスのサブタイプごとのアイテム数を取得するスクリプト(複数のGDBには対応していません)
# -*- coding: utf-8 -*-
import arcpy
from os.path import join
import pandas as pd
import numpy as np
def getInfo(dataSource,fList):
#サブタイプを取得
subtypefields = arcpy.da.ListSubtypes(dataSource)
subDict = {}
for name in subtypefields.keys():
groupDict = subtypefields[name]
for stkey in list(groupDict.keys()):
if stkey == 'SubtypeField':
fields = groupDict[stkey]
#サブタイプが設定されていない場合
if fields == "":
outputstring = fList[0] \
+ "," + fList[1] \
+ "," + fList[2] \
+ "," + fList[3] \
+ "," + "" \
+ "," + str(fList[4]) + "\n"
f.write(outputstring.encode("SHIFT-JIS"))
#サブタイプが設定されている場合
else:
for stkey in list(groupDict.keys()):
if stkey == 'Name':
#フィーチャをnumpyに変換してpandasに格納
column = arcpy.da.FeatureClassToNumPyArray(dataSource,("SUBTYPE_CD"),null_value=-9999)
dfWater = pd.DataFrame(column)
fields = groupDict[stkey]
subDict[fields] = name
#サブタイプごとのデータを抽出
dfWaterValue = pd.DataFrame(dfWater[dfWater.SUBTYPE_CD == subDict[fields]])
outputstring = fList[0] \
+ "," + fList[1] \
+ "," + fList[2] \
+ "," + fList[3] \
+ "," + fields \
+ "," + str(len(dfWaterValue)) + "\n"
f.write(outputstring.encode("SHIFT-JIS"))
# ファイルの出力先を指定
f = open("", "w") #r"D:\python\featureclass/list.csv"
# 対象のGDBが格納されているフォルダを指定
folder = "" #'C:\ArcPySample'
#GDB名を指定
gdb = "" #"ArcPyTest.gdb"
data = join(folder,gdb)
# ファイルにヘッダを出力
outputstring = u"GDB,レイヤ名,エイリアス,ジオメトリタイプ,サブタイプ,アイテム数\n"
f.write(outputstring.encode("SHIFT-JIS"))
arcpy.env.workspace = data
# フィーチャクラスから属性を取得
for fc in arcpy.ListFeatureClasses():
fList = []
dataSource = join(data,fc)
#フィーチャクラスの名称を取得
name = arcpy.Describe(dataSource).Name
#フィーチャクラスのエイリアスを取得
aliasName = arcpy.Describe(dataSource).AliasName
#フィーチャの件数取得
cnt = arcpy.GetCount_management(dataSource)
#フィーチャタイプ取得
fType = arcpy.Describe(dataSource).FeatureType
#ジオメトリタイプ取得
gType = arcpy.Describe(dataSource).shapeType
#アノテーションの場合(アノテーションのジオメトリタイプはポリゴンになってしまうのでこの処理を追加)
if fType == "Annotation":
gType = "Annotation"
fList.append(gdb)
fList.append(name)
fList.append(aliasName)
fList.append(gType)
fList.append(int(cnt.getOutput(0)))
getInfo(dataSource,fList)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment