import json

def analyze_geojson(geojson_file):
  """
  GeoJSONファイルを解読し、鉄道会社の数、路線の数、駅の数をカウントする。
  また、駅の情報（路線名、鉄道会社名、駅名）を取得する。

  Args:
    geojson_file (str): GeoJSONファイルのパス

  Returns:
    tuple: (鉄道会社の数, 路線の数, 駅の数, 駅のリスト)
  """

  try:
    with open(geojson_file, 'r', encoding='utf-8') as f:
      data = json.load(f)
  except FileNotFoundError:
    print(f"ファイル '{geojson_file}' が見つかりません。")
    return 0, 0, 0, []
  except json.JSONDecodeError:
    print(f"ファイル '{geojson_file}' のJSONデコードに失敗しました。")
    return 0, 0, 0, []

  rail_companies = set()
  routes = set()
  stations = set()
  station_info = []

  if 'features' in data:
    for feature in data['features']:
      if 'properties' in feature:
        properties = feature['properties']

        # 路線名
        if 'N02_003' in properties:
          routes.add(properties['N02_003'])

        # 鉄道会社名
        if 'N02_004' in properties:
          rail_companies.add(properties['N02_004'])

        # 駅名
        if 'N02_005' in properties:
          stations.add(properties['N02_005'])
          station_info.append({
            '路線名': properties.get('N02_003', ''),
            '鉄道会社名': properties.get('N02_004', ''),
            '駅名': properties['N02_005']
          })
  
  num_rail_companies = len(rail_companies)
  num_routes = len(routes)
  num_stations = len(stations)

  return num_rail_companies, num_routes, num_stations, station_info

# ファイル名を指定
geojson_file = 'N02-22_Station.geojson'

# GeoJSONファイルの解析
num_rail_companies, num_routes, num_stations, station_info = \
  analyze_geojson(geojson_file)

# 結果の表示
print(f"鉄道会社の数: {num_rail_companies}")
print(f"路線の数: {num_routes}")
print(f"駅の数: {num_stations}")
print("\n駅の情報:")
for station in station_info:
  print(f"  路線名: {station['路線名']}, \
            鉄道会社名: {station['鉄道会社名']}, \
            駅名: {station['駅名']}")
