본문 바로가기

언어 꿀Tip/Python 꿀tip!

07_01_54. 'UTM-K' to 'WGS84' 변환

 

import pandas as pd

import geopandas as gpd
import pyproj
from fiona.crs import from_epsg
import os
# dw 연결
import ibm_db_dbi

# Setting
conn = ibm_db_dbi.connect("DRIVER={IBM DB2 ODBC DRIVER}; Database='db이름'; Hostname=hostip입력; Port=portnumber입력; PROTOCOL=TCPIP; UID=userid입력; PWD=비밀번호입력", "", "")

query_biz = '''
쿼리
;
'''
# Sql
df_dw = pd.read_sql( query_biz, conn)
# 컬럼명 소문자 변환
df_dw.columns = df_dw.columns.str.lower()

##### convert katec UTM-K to geometry
df_point = gpd.points_from_xy(df_dw.str_xcoor, df_dw.str_ycoor)

coulumns = ['cd',"nm","city"]
df_cd = gpd.GeoDataFrame(df_dw[coulumns], geometry=df_point,
                         crs='+proj=tmerc +lat_0=38 +lon_0=128 +k=0.9999 +x_0=400000 +y_0=600000 +ellps=bessel +units=m +no_defs +towgs84=-115.80,474.99,674.11,1.16,-2.31,-1.63,6.43')
##### 'UTM-K' => 'WGS84' 변환
df_cd = df_cd.to_crs(epsg = 4326).reset_index(drop=True)

df_cd['latitude'] = [re.findall('\d+\.\d+', str(df_cd['geometry'][x]))[1] for x in range(len(df_cd))]
df_cd['longitude'] = [re.findall('\d+\.\d+', str(df_cd['geometry'][x]))[0] for x in range(len(df_cd))]

df_cd["latitude_f"]=df_cd["latitude"].apply(pd.to_numeric)
df_cd["longitude_f"]=df_cd["longitude"].apply(pd.to_numeric)