批量转换坐标,定义类时出错

时间:2017-08-03 21:26:24

标签: python excel python-3.x class

我正在尝试调整现有脚本以启用批量转换坐标。

但是,我收到以下错误: NameError:名称'RDWGSConverter'未定义

我理解课程应该事先定义。但如果我没有遗漏某些事情,情况应该如此?

import urllib.parse
from openpyxl import load_workbook, Workbook


"""load datafile"""
workbook = load_workbook('convertor.xlsx')
worksheet = workbook.active

Xco=[]
Yco=[]

Xco = [row[1].value for row in worksheet.iter_rows() ]

Yco = [row[2].value for row in worksheet.iter_rows() ]
coordinates = [(Xco), (Yco)]

print(Xco[5])
print(range(len(Xco)))
class RDWGSConverter:
    for i in (range(len(Xco))):
        X0      = Xco[i]
        Y0      = Yco[i]
        phi0    = 52.15517440
        lam0    = 5.38720621
        print(Xco[i])

        def fromRdToWgs( self, coords ):
            Kp = [0,2,0,2,0,2,1,4,2,4,1]
            Kq = [1,0,2,1,3,2,0,0,3,1,1]
            Kpq = [3235.65389,-32.58297,-0.24750,-0.84978,-0.06550,-0.01709,-0.00738,0.00530,-0.00039,0.00033,-0.00012]

            Lp = [1,1,1,3,1,3,0,3,1,0,2,5]
            Lq = [0,1,2,0,3,1,1,2,4,2,0,0]
            Lpq = [5260.52916,105.94684,2.45656,-0.81885,0.05594,-0.05607,0.01199,-0.00256,0.00128,0.00022,-0.00022,0.00026]

            dX = 1E-5 * ( coords[0] - self.X0 )
            dY = 1E-5 * ( coords[1] - self.Y0 )

            phi = 0
            lam = 0

            for k in range(len(Kpq)):
                phi = phi + ( Kpq[k] * dX**Kp[k] * dY**Kq[k] )
            phi = self.phi0 + phi / 3600

            for l in range(len(Lpq)):
                lam = lam + ( Lpq[l] * dX**Lp[l] * dY**Lq[l] )
            lam = self.lam0 + lam / 3600

            return [phi,lam]

        def fromWgsToRd( self, coords ):

            Rp = [0,1,2,0,1,3,1,0,2]
            Rq = [1,1,1,3,0,1,3,2,3]
            Rpq = [190094.945,-11832.228,-114.221,-32.391,-0.705,-2.340,-0.608,-0.008,0.148]

            Sp = [1,0,2,1,3,0,2,1,0,1]
            Sq = [0,2,0,2,0,1,2,1,4,4]
            Spq = [309056.544,3638.893,73.077,-157.984,59.788,0.433,-6.439,-0.032,0.092,-0.054]

            dPhi = 0.36 * ( coords[0] - self.phi0 )
            dLam = 0.36 * ( coords[1] - self.lam0 )

            X = 0
            Y = 0

            for r in range( len( Rpq ) ):
                X = X + ( Rpq[r] * dPhi**Rp[r] * dLam**Rq[r] ) 
            X = self.X0 + X

            for s in range( len( Spq ) ):
                Y = Y + ( Spq[s] * dPhi**Sp[s] * dLam**Sq[s] )
            Y = self.Y0 + Y

            return [X,Y]

    def main():
        # Rotterdam Centraal Station
        coords = [91819, 437802]
        conv = RDWGSConverter()
        wgsCoords = conv.fromRdToWgs( coords )
        newCoords = conv.fromWgsToRd( wgsCoords )
        print( coords, newCoords, wgsCoords )

    if __name__ == "__main__":
        main()

感谢您帮助我发现此代码结构中的错误!

GRTS

1 个答案:

答案 0 :(得分:0)

我觉得你的缩进就好了。

代码发布的方式,def main()(以及调用它的行)是里面的 class RDWGSConverter的定义。在完成类的定义之前,您无法创建类的实例,这就是您收到报告错误的原因。但我认为这不是你打算做的事情。

您还在循环中定义fromRdToWgsfromWgsToRd,这可能不是您的意思。

Outdent

def main():

if __name__ == "__main__":

与类定义处于同一级别,以便您定义类拳,然后在函数main中开始使用它。

循环开始for i in (range(len(Xco))):也可能处于错误的级别,因为您将其作为类定义的一部分执行,我无法理解您为什么要这样做。