# CityGMLの地物数を数える(不動産IDマッチングシステム用)
from lxml import etree # xmlデータを読み込むため
import csv # CSVデータのインポートやエクスポートを行うため
# 結果記入ファイル
fw = open('count.csv', "w")
fw.write( 'Filename, All_Count, Match_Count, appMember\n')
fm = open('score.csv', "w") # matchingScore
fm.write( 'Filename, MatchingScore\n')
# CityGMLのリスト読み込み
f = open('list.csv', "r")
reader = csv.reader(f)
for row in reader: # 一行読んでカンマで区切り配列に入れる。終わりまで繰り返す
#print(row[0]) # ファイル名
# CityGMLファイルのパース
tree = etree.parse(row[0])
# ルート要素の取得
root = tree.getroot()
#print( 'root tag = %s' % root.tag )
# CityGMLの名前空間(3.0に変更する必要あり?)
namespace = {'citygml': 'http://www.opengis.net/citygml/2.0'}
# CityGMLデータの取得
city_objects = root.findall('.//citygml:cityObjectMember', namespaces=namespace)
# 全データ取得
#all = 0
#for city_object in city_objects:
#print(etree.tostring(city_object, pretty_print=True).decode('utf-8'))
# all = all + 1
# マッチングデータ取得
all = 0
score = 0
app = 0
for element in tree.iter():
#print(element.tag)
if element.tag =='{http://www.opengis.net/citygml/2.0}cityObjectMember':
all = all + 1
if element.tag =='{https://www.geospatial.jp/iur/uro/3.0}matchingScore':
score = score + 1
#matchingScore表示
matchingScore = element.text.strip()
#print ('matchingScore = ', matchingScore)
fm.write( '%s, %s' %(row[0], matchingScore)+'\n')
if element.tag =='{http://www.opengis.net/citygml/appearance/2.0}appearanceMember':
app = app + 1
#全建物件数とマッチングした建物件数表示
print('Filename = %s All = %d Match = %d appMember = %d' %(row[0], all, score, app))
fw.write( '%s, %d, %d, %d' %(row[0], all, score, app)+'\n')