본문 바로가기

Code Snippets/Pandas

중부원점(EPSG:2097) 위도·경도(WGS84)로 변환 (연산 속도 높이기)

공공데이터로 받은 자료가 위치 정보를 가지고 있지만,

데이터가 위도 경도가 아닌 중부원점 좌표로 되어 있다.

 

현재 위치 기준으로 거리 계산, 지도에 표시 등의 작업을 위해서는 WGS84 좌표계로 변환하는 작업이 필요하다.

 

이 작업은 pyproj 라이브러리를 이용해서 아래와 같이 비교적 간단히 수행할 수 있다.

 

# 라이브러리 가져오기
import 
from pyproj import Proj, transform
import pandas as pd



# Projection 정의
# 중부원점(Bessel): 서울 등 중부지역 EPSG:2097
proj_1 = Proj(init='epsg:2097')

# WGS84 경위도: GPS가 사용하는 좌표계 EPSG:4326
proj_2 = Proj(init='epsg:4326')

DataFrame = df.copy()

x_list = []
y_list = []

for idx, row in DataFrame.iterrows():
    x, y = row['x'], row['y']
    x_, y_ = transform(proj_1, proj_2, x, y)
    x_list.append(x_)
    y_list.append(y_)
    
df['lon'] = x_list
df['lat'] = y_list

# 출처 : https://wooiljeong.github.io/python/transform_coord/

 

그런데 위 코드로  하니까 4만행이 넘어가는 데이터프레임에 대해서는 연산 속도가 매우 느리다.

 

왜 그런지 확인해보니, 모든 행마다 transform() method을 적용하면서 시간이 많이 걸리게 되는 것이다.

 

아래와 같이 한번에 transform으로 처리하면 연산 시간이 반 이상 단축된다.

 

proj_1 = Proj(init='epsg:2097')
proj_2 = Proj(init='epsg:4326')

converted = transform(proj_1, proj_2, df['X'].values, df['Y'].values)
df['lon'] = converted[0]
df['lat'] = converted[1]