[COMPAS 광양시] 100 x 100 격자 데이터 활용 각 읍면동별 차량 등록 정보 데이터 만들기

솜씨좋은장씨

·

2020. 9. 26. 09:54

데이터 분석 활용시간

2020년 9월 26일 07시 30분 ~ 09시 30분 ( 2시간 )

 

사용 데이터

03.광양시_자동차등록현황_격자(100X100).geojson

19.광양시_법정경계(읍면동).geojson

 

활용 코드 및 내용

필요 라이브러리 설치

$ pip install shapely

필요 라이브러리 import

import geopandas as gpd
from shapely.geometry import Point, Polygon
from tqdm import tqdm

데이터 load 하기

- 읍면동 ( 법정경계 ) 정보

# file_list[14] == 19.광양시_법정경계(읍면동).geojson
emd_gdf = gpd.read_file("./data/" + file_list[14])
emd_gdf

 

- 100 X 100 별 자동차 등록현황 정보

# file_list[2] == 03.광양시_자동차등록현황_격자(100X100).geojson
car_gdf = gpd.read_file("./data/" + file_list[2])
car_gdf.head()

각 읍면동 별 MULTIPOLYGON 정보를 POLYGON으로 변경하기

umd_polys = {}

multi_polys = list(emd_gdf['geometry'])

for i in tqdm(range(len(multi_polys))):
    polys = []
    if multi_polys[i].geom_type == "MultiPolygon":
        for poly in multi_polys[i]:
            polys.append(poly)
    umd_polys[emd_gdf['EMD_KOR_NM'].iloc[i]] = polys
umd_polys
{'황금동': [<shapely.geometry.polygon.Polygon at 0x7fae782100b8>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210e80>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210cf8>],
 '황길동': [<shapely.geometry.polygon.Polygon at 0x7fae782101d0>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210eb8>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210f60>],
 '도이동': [<shapely.geometry.polygon.Polygon at 0x7fae78210d68>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210ac8>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210b70>,
  <shapely.geometry.polygon.Polygon at 0x7fae782108d0>,
  <shapely.geometry.polygon.Polygon at 0x7fae78210978>],
 '성황동': [<shapely.geometry.polygon.Polygon at 0x7fae78210780>],
 '중군동': [<shapely.geometry.polygon.Polygon at 0x7fae78210588>],
 '중동': [<shapely.geometry.polygon.Polygon at 0x7fae78210390>],
 '마동': [<shapely.geometry.polygon.Polygon at 0x7fae782092e8>],
 '광영동': [<shapely.geometry.polygon.Polygon at 0x7fae78209f60>,
  <shapely.geometry.polygon.Polygon at 0x7fae78209710>],
 '태인동': [<shapely.geometry.polygon.Polygon at 0x7fae782094e0>],
 '금호동': [<shapely.geometry.polygon.Polygon at 0x7fae78209780>,
  <shapely.geometry.polygon.Polygon at 0x7fae78209278>],
 '광양읍': [<shapely.geometry.polygon.Polygon at 0x7fae78209b70>,
  <shapely.geometry.polygon.Polygon at 0x7fae782096d8>,
  <shapely.geometry.polygon.Polygon at 0x7fae78209b00>,
  <shapely.geometry.polygon.Polygon at 0x7fae78209eb8>],
 '봉강면': [<shapely.geometry.polygon.Polygon at 0x7fae782106a0>],
 '옥룡면': [<shapely.geometry.polygon.Polygon at 0x7fae78209588>],
 '옥곡면': [<shapely.geometry.polygon.Polygon at 0x7fae78209978>],
 '진상면': [<shapely.geometry.polygon.Polygon at 0x7fae782090b8>],
 '진월면': [<shapely.geometry.polygon.Polygon at 0x7fae78209e80>,
  <shapely.geometry.polygon.Polygon at 0x7fae78209be0>],
 '다압면': [<shapely.geometry.polygon.Polygon at 0x7fae782099e8>]}

Polygon의 contains 메소드를 활용하여 각 격자가 각 읍면동 Polygon에 속하는지 여부를 판단하여

각 격자의 id를 각 읍면동 리스트에 추가하기

umd_dict = {"황금동": [], "황길동": [], "도이동": [], "성황동": [], "중군동": [], "중동": [], "마동": [], "광영동": [], 
        "태인동": [], "태인동": [], "금호동": [], "금호동": [], "광양읍": [], "봉강면": [], "옥룡면": [], "옥곡면": [],
        "진상면": [], "진월면": [], "다압면": []}

umd_key  = list(umd_polys.keys())

for c in tqdm(range(len(list(car_gdf['geometry'])))):
    
    for i, key in enumerate(umd_key):
        umd_poly = umd_polys[key]
        for poly in umd_poly:
            if poly.contains(car_gdf['geometry'].iloc[c]):
#                 print(sigungu_gdf['EMD_KOR_NM'].iloc[i])
#                 print("-"*100)
                umd_dict[emd_gdf['EMD_KOR_NM'].iloc[i]].append(car_gdf['id'].iloc[c])
#         print(sigungu_gdf['EMD_KOR_NM'].iloc[i])

id 값을 기준으로 각 읍면동에 속한 격자에 담긴 차량 등록 정보를 합하여 다시 dictionary를 만들기

umd_car_infos = {}

keys = list(umd_dict.keys())

for key in keys:
    umd_ids = umd_dict[key]

    total = 0

    for i in tqdm(range(len(umd_ids))):
        temp_gdf = car_gdf[car_gdf['id'] == umd_ids[i]]

        total = total + temp_gdf['totale'].iloc[0]

    umd_car_infos[key] = total

umd_car_infos
{'황금동': 26,
 '황길동': 44,
 '도이동': 112,
 '성황동': 49,
 '중군동': 26,
 '중동': 4043,
 '마동': 1555,
 '광영동': 1204,
 '태인동': 452,
 '금호동': 1549,
 '광양읍': 4749,
 '봉강면': 209,
 '옥룡면': 285,
 '옥곡면': 382,
 '진상면': 271,
 '진월면': 295,
 '다압면': 173}