python实现四参数七参数坐标转换

四参和七参转换,转换坐标没有什么问题,我附录了一些坐标信息检验,不过七参数的数值有些问题,不知道是哪里有些错误,希望大家指正,下面是完整代码:

在这里插入代码片
```import numpy as np
from numpy import linalg

def four():
    def four_number(x1,x2,x3,x4,xx1,xx2,xx3):
        
        """由三个共同点求两个不同平面直角坐标系的坐标转换"""

        def caculate_four(x4,old,new):
                
            """计算新坐标"""        
            mid=new-old
            A_=np.array([[1,0,x4[0],-x4[1]],
                         [0,1,x4[1],x4[0]]])

            old_=np.array([[x4[0]],
                           [x4[1]]])

            xxx=linalg.lstsq(A,mid,rcond=-1)

            v=np.dot(A_,xxx[0])
            new_=old_+v
            return new_

        A=np.array([[1.0,0,x1[0],-x1[1]],
                    [0,1.0,x1[1], x1[0]],
                    [1.0,0,x2[0],-x2[1]],
                    [0,1.0,x2[1], x2[0]],
                    [1.0,0,x3[0],-x3[1]],
                    [0,1.0,x3[1], x3[0]]])

        old=np.array([[x1[0]],
                      [x1[1]],
                      [x2[0]],
                      [x2[1]],
                      [x3[0]],
                      [x3[1]]])
        
        new=np.array([[xx1[0]],
                      [xx1[1]],
                      [xx2[0]],
                      [xx2[1]],
                      [xx3[0]],
                      [xx3[1]]])
        
        return caculate_four(x4,old,new)
    
    x1 = [4031675.025,552545.805]
    xx1 = [4031634.029,552496.906]
    x2 = [4032992.528,547359.488]
    xx2 = [4032951.494,547310.569]
    x3 = [4026900.438,534348.966]
    xx3 = [4026859.289,534300.075]

    x4 = [4027240.551,541727.683]
    print("平面直角新坐标为\n",four_number(x1,x2,x3,x4,xx1,xx2,xx3))


## 四参数转换数据
## 旧:a 4031675.025,552545.805
## b 4032992.528,547359.488
## c 4026900.438,534348.966
## 
## 新:a 4031634.029,552496.906
## b 4032992.528,547359.488
## c 4026900.438,534348.966
##
## 待求旧:4027240.551,541727.683
## 待求新:4027199.461,541678.802
four()


def seven():
    def seven_number(x1,x2,x3,x4,xx1,xx2,xx3):
        
        """由三个共同点求两个不同空间直角坐标系的坐标转换"""
        
        def caculate_seven(x4,old,new):
            
            """计算新坐标"""
            
            mid=new-old
            A_=np.array([[1,0,0,x4[0],0,-x4[2],x4[1]],
                        [0,1,0,x4[1],x4[2],0,-x4[0]],
                        [0,0,1,x4[2],-x4[1],x4[0],0]])

            old_=np.array([[x4[0]],
                          [x4[1]],
                          [x4[2]]])

            xxx=linalg.lstsq(A,mid,rcond=-1)

            v=np.dot(A_,xxx[0])
            new_=old_+v
            return new_

        A=np.array([[1,0,0,x1[0],0,-x1[2],x1[1]],
                    [0,1,0,x1[1],x1[2],0,-x1[0]],
                    [0,0,1,x1[2],-x1[1],x1[0],0],
                    [1,0,0,x2[0],0,-x2[2],x2[1]],
                    [0,1,0,x2[1],x2[2],0,-x2[0]],
                    [0,0,1,x2[2],-x2[1],x2[0],0],
                    [1,0,0,x3[0],0,-x3[2],x3[1]],
                    [0,1,0,x3[1],x3[2],0,-x3[0]],
                    [0,0,1,x3[2],-x3[1],x3[0],0]])

        old=np.array([[x1[0]],
                      [x1[1]],
                      [x1[2]],
                      [x2[0]],
                      [x2[1]],
                      [x2[2]],
                      [x3[0]],
                      [x3[1]],
                      [x3[2]]])
        
        new=np.array([[xx1[0]],
                      [xx1[1]],
                      [xx1[2]],
                      [xx2[0]],
                      [xx2[1]],
                      [xx2[2]],
                      [xx3[0]],
                      [xx3[1]],
                      [xx3[2]]])
        return caculate_seven(x4,old,new)
       
    #共同点坐标
    x1=[3516952.028,519078.188,36.393]
    x2=[3516556.925,519081.738,31.720]
    x3=[3516569.509,518700.605,31.097]
    xx1=[3516904.452,519026.990,34.565]
    xx2=[3516509.353,519030.538,29.893]
    xx3=[3516521.939,518649.409,29.272]
    #待转换坐标
    x4=[3516587.828,518636.550,30.246]

    print("\n空间直角新坐标为\n",seven_number(x1,x2,x3,x4,xx1,xx2,xx3))

## 七参数转换数据
## 旧:a 3516952.028,519078.188,36.393
## b 3516556.925,519081.738,31.720
## c 3516569.509,518700.605,31.097
##
## 新:a 3516904.452,519026.990,34.565
## b 3516509.353,519030.538,29.893
## c 3516521.939,518649.409,29.272
##
## 待求旧:d 3516587.828,518636.550,30.246
## 待求新:d 3516540.258,518585.355,28.421
    
seven()    

    原文作者:矫恣
    原文地址: https://blog.csdn.net/weixin_44269793/article/details/103310824
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞