数组循环只能手动输入,我想要达到自动化,将数组循环的值循环进入下面我想要的公式
def trans(self):
for r in range(50, 3550, 50):
workbook = xlrd.open_workbook(r'D:\桌面\盘县.xls') # 文件路径
names = workbook.sheet_names()
# 通过sheet索引获得sheet对象,可以通过“索引”的方式获得,也可以通过“表名”的方式获得。
worksheet = workbook.sheet_by_index(0)
nrows = worksheet.nrows # 获取该表总行数
ncols = worksheet.ncols # 获取该表总列数
row_data, col_data = worksheet.col_values(1), worksheet.col_values(2) # 获取第一列的内容,一般读取表格数据时,从零开始,即零等于第一列。这里可以读取自己想要的列
list1 = []
list2 = []
list1.append(row_data)
list1 = np.array(list1)
row = list1.reshape(500, 1)
row1 = numpy.array(row)
list2.append(col_data)
list2 = np.array(list2)
cow = list2.reshape(500, 1)
cow1 = numpy.array(cow)
RE = 6371.393 * 1000 # 地球半径
d = 12.5 # 距离
c = int(r / d) # 取点数量
i=0
while i<500:
lon_dem ,lat_dem= row1[i],cow1[i]
i=+1
if i>=500:
break
lon = (np.ones((c * 8)) *lon_dem )
lat = (np.ones((c * 8)) *lat_dem )
re = RE * np.cos(lat / 180 * np.pi) # 纬度圈半径
amuith = np.repeat(np.arange(0, 360, 45), repeats=c) # 角度、范围是0-360,以每次45°递增,每个方向取c个点
distance = (np.repeat(np.arange(1, c + 1) * d, repeats=8)) # 等距离取点,点之间的距离为30,8个方向都取c个点
distance = distance.reshape(c, 8) # c行8列的一个数组
distance = np.rot90(distance) # 旋转90°变成8行c列
distance = distance.ravel() # 数组展平(二维转一维度)
lon2 = lon + distance * np.sin(amuith / 180 * np.pi) / (re * 2 * np.pi) * 360 # 求经纬度公式
lat2 = lat + distance * np.cos(amuith / 180 * np.pi) / (RE * 2 * np.pi) * 360
lat2 = np.round(lat2, decimals=8) # 保留8位小数,下同
lon2 = np.round(lon2, decimals=8)
print(r)
list = []
for i in range(c * 8):
latitude = lat2[i-1]
longitude = lon2[i-1]
x =self. dem(latitude, longitude) # 调用dem方法提取高程值
list.append(x)
data = np.array(list)
data = data.reshape(8, c) # 8行c列
new_rows, new_cols = data.shape
list5 = []
list6 = []
for i in range(new_rows):
dem_yuandian = self.dem(lat_dem, lon_dem)
dem = max(np.array(data[i, 0:c+1 ])) #
min_dem = min(np.array(data[i, 0:c + 1])) #
a = numpy.argwhere(data[i, 0:c + 1])
b = numpy.argwhere(data[i, 0:c + 1])
a = int(a[0]+1)
b = int(b[0]+1)
distance_new1 = 30 * a#最大值离原点的距离
distance_new2 = 30 * b#最小值离原点距离
tianding = 90-((np.arctan((dem - dem_yuandian) / distance_new1))*180 / np.pi)
tiandi = 90 + ((np.arctan((min_dem - dem_yuandian) / distance_new2))*180 / np.pi)
list5.append(tianding)
list6.append(tiandi)
tianding_numpy = np.array(list5)
tiandi_numpy = np.array(list6)
tiding,tidi = np.average(tianding_numpy), np.average(tiandi_numpy)
print(tiding)
print(tidi)
p1=Pant()
print(p1.trans())
结果显示需要很久
将row1和cow1的数组进入下面的循环,是依次循环