2023-04-24 coordinate transform

import pandas as pd
import numpy as np
from pathlib import Path
p = Path('.')
csv_files = [f for f in p.glob("*.csv")]
csv_files
df_ucs, df_wcs = [pd.read_csv(f, skiprows=[0]) for f in csv_files]
def retrive_2_points(df):
    '''
    retrive the 2 points from the source DataFram.
    '''
    colunms = ['Easting', 'Northing']
    pnt_start, pnt_end = [df.loc[i, colunms] for i in range(2)]
    pnt_start, pnt_end = [df.values for df in [pnt_start, pnt_end]]
    return pnt_start, pnt_end
    
def get_perpendicular_point(df):
    '''
    get a point that is perpendicular to the given two points.
    
    - caculate vector v by subtracting pnt_end to pnt_start: v = pnt_end - pnt_start
    - rotate v 90 degrees: v90 = R90@v
    - add v90 and pnt_start to get the perpendicular point: pnt_perpendicular = v90 + pnt_start
    '''
    pnt_start, pnt_end = retrive_2_points(df)
    
    v = pnt_end - pnt_start
    R90 = np.array([[0, -1], [1, 0]])
#     return v, R90
    v90 = R90@v
    
    pnt_perpendicular = v90 + pnt_start
    return pnt_perpendicular

get_perpendicular_point(df_ucs)

def construct_matrix_from_source_df(df):
    '''
    construct a matrix from three points.
    
    - 2 of the 3 points are retrieved form the source .csv file
    - the perpendicular point is construced by function get_perpendicular_point
    '''
    pnt_start, pnt_end = retrive_2_points(df)
    pnt_perpendicular = get_perpendicular_point(df)
    A = np.array([pnt_start, pnt_end, pnt_perpendicular])
    
    suffix = np.array([[1, 1, 1]]).T
    A =  np.concatenate((A, suffix), axis=1)
    
    return A.T

construct_matrix_from_source_df(df_ucs)

A_w, A_u = [construct_matrix_from_source_df(df) for df in [df_wcs, df_ucs]]
T_w_to_u = A_u@np.linalg.inv(A_w)
T_u_to_w = A_w@np.linalg.inv(A_u)

T_u_to_w@A_u - A_w

T_w_to_u
A_w.T
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容