Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ESRIデータコレクションの詳細地図.lyrの情報をCSV出力するスクリプトです
# -*- coding: utf-8 -*-
import arcpy
import pandas as pd
import numpy as np
fields = ""
dataSource = ""
subfields = ""
def output_csv(info,f,flg,subTypeCd):
global dataSource
global fields
global subfields
if flg == 0:
#フィーチャをnumpyに変換してpandasに格納
numFeature = arcpy.da.FeatureClassToNumPyArray(dataSource,("OID@"),null_value=-9999)
dfFeature = pd.DataFrame(numFeature)
outputstring = info[0] \
+ "," + info[1] \
+ "," + info[2] \
+ "," + info[3] \
+ "," + info[4] \
+ "," + info[5] \
+ "," + str(info[6]) \
+ "," + str(info[7]) \
+ "," + "" \
+ "," + str(len(dfFeature)) + "\n"
f.write(outputstring.encode("SHIFT-JIS"))
else:
#フィーチャをnumpyに変換してpandasに格納
numFeature = arcpy.da.FeatureClassToNumPyArray(dataSource,(fields),null_value=-9999)
dfFeature = pd.DataFrame(numFeature)
#サブタイプごとのデータを抽出
if fields == "LAYERCODE":
dfFeatureValue = pd.DataFrame(dfFeature[dfFeature.LAYERCODE == subTypeCd])
elif fields == "TYPE":
dfFeatureValue = pd.DataFrame(dfFeature[dfFeature.TYPE == subTypeCd])
outputstring = info[0] \
+ "," + info[1] \
+ "," + info[2] \
+ "," + info[3] \
+ "," + info[4] \
+ "," + info[5] \
+ "," + str(info[6]) \
+ "," + str(info[7]) \
+ "," + subfields \
+ "," + str(len(dfFeatureValue)) + "\n"
f.write(outputstring.encode("SHIFT-JIS"))
def get_layer_info(layer,f,group1,group2,group3,group4):
global fields
global dataSource
global subfields
info = []
dataSource = layer.dataSource
#フィーチャレイヤの場合
if layer.isFeatureLayer:
#フィーチャタイプ取得
fType = arcpy.Describe(dataSource).FeatureType
#アノテーションの場合(アノテーションのジオメトリタイプはポリゴンになってしまうのでこの処理を追加)
if fType == "Annotation":
gType = "Annotation"
else:
#ジオメトリタイプ取得
gType = arcpy.Describe(dataSource).shapeType
else:
gType = arcpy.Describe(dataSource).format
info.append(group1)
info.append(group2)
info.append(group3)
info.append(group4)
info.append(layer.datasetName)
info.append(gType)
info.append(layer.maxScale)
info.append(layer.minScale)
#フィーチャレイヤの場合
if layer.isFeatureLayer:
#サブタイプを取得
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 == "":
output_csv(info,f,0,"")
#サブタイプが設定されている場合
else:
for stkey in list(groupDict.keys()):
if stkey == 'Name':
subfields = groupDict[stkey]
subDict[subfields] = name
output_csv(info,f,1,subDict[subfields])
else:
output_csv(info,f,0,"")
if __name__ == '__main__':
#レイヤファイルのパスを指定
filepath = "" #ur"D:\python\datacollection\詳細地図\詳細地図.lyr"
#CSV出力先を指定
f = open("", "w")
# CSVにヘッダを出力
outputstring = u"グループ1,グループ2,グループ3,グループ4,フィーチャクラス,ジオメトリタイプ,最大縮尺,最小縮尺,サブタイプ,アイテム数\n"
f.write(outputstring.encode("SHIFT-JIS"))
#レイヤオブジェクト取得
layer = arcpy.mapping.Layer(filepath)
for lay in layer:
if lay.name == u"詳細図" or lay.name == u"広域地図" or lay.name == u"背景":
if lay.isGroupLayer:
for ly in lay:
if ly.isGroupLayer:
for l in ly:
if l.isGroupLayer:
for ll in l:
get_layer_info(ll,f,lay.name,ly.name,l.name,ll.name)
else:
get_layer_info(l,f,lay.name,ly.name,l.name,"")
else:
if ly.longName.find("\\") == -1:
get_layer_info(ly,f,lay.name,ly.name,"","")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment