增加了基于周围几个点差值求高程的功能。

This commit is contained in:
n3040 2022-01-15 23:42:49 +08:00
parent d41c0615b2
commit e56399cccb
1 changed files with 101 additions and 5 deletions

106
dem.py
View File

@ -1,3 +1,6 @@
import math
import ezdxf
from osgeo import gdal
import numpy as np
@ -50,8 +53,8 @@ class Dem:
# print("\nThe 6 GeoTransform parameters of DEM are:\n", gt)
N_site = site_latlng.shape[0]
Xgeo = site_latlng[:, 1] # longitude
Ygeo = site_latlng[:, 0] # latitude
Xgeo = site_latlng[:, 0]
Ygeo = site_latlng[:, 1]
site_ele = np.zeros(N_site)
for i in range(N_site):
# Note:
@ -72,7 +75,100 @@ class Dem:
#
# [2] = 0 for north up DEM
# [4] = 0 for north up DEM
Xpixel = int(round((Xgeo[i] - gt[0]) / gt[1]))
Yline = int(round((Ygeo[i] - gt[3]) / gt[5]))
site_ele[i] = gdal_data.ReadAsArray(Xpixel, Yline, 1, 1).astype(np.float)
Xpixel = (Xgeo[i] - gt[0]) / gt[1]
Yline = (Ygeo[i] - gt[3]) / gt[5]
# 寻找左上左下右上右下4个点
lu_xy = np.array([math.floor(Xpixel), math.floor(Yline)]) # 左上
ld_xy = np.array([math.floor(Xpixel), math.ceil(Yline)]) # 左下
ru_xy = np.array([math.ceil(Xpixel), math.floor(Yline)]) # 右上
rd_xy = np.array([math.ceil(Xpixel), math.ceil(Yline)]) # 右下
lu_elevation = gdal_data.ReadAsArray(
int(lu_xy[0]), int(lu_xy[1]), 1, 1
).astype(float)
ld_elevation = gdal_data.ReadAsArray(
int(ld_xy[0]), int(ld_xy[1]), 1, 1
).astype(float)
ru_elevation = gdal_data.ReadAsArray(
int(ru_xy[0]), int(ru_xy[1]), 1, 1
).astype(float)
rd_elevation = gdal_data.ReadAsArray(
int(rd_xy[0]), int(rd_xy[1]), 1, 1
).astype(float)
# delta_x = (Xpixel - ld_xy[0]) / 1 # 距离是1
# delta_y = (ld_xy[1]-Yline) / 1
# site_ele[i] = (
# ld_elevation
# + (lu_elevation - ld_elevation) * delta_y
# + (rd_elevation - ld_elevation) * delta_x
# + (ld_elevation - lu_elevation + ru_elevation - rd_elevation)
# * delta_x
# * delta_y
# )
# 通过空间平面方程乃拟合Z值
equa_a = np.array(
[
[lu_xy[0], lu_xy[0] * lu_xy[1], lu_xy[1], 1],
[ld_xy[0], ld_xy[0] * ld_xy[1], ld_xy[1], 1],
[ru_xy[0], ru_xy[0] * ru_xy[1], ru_xy[1], 1],
[rd_xy[0], rd_xy[0] * rd_xy[1], rd_xy[1], 1],
]
)
equa_b = np.array(
[lu_elevation[0], ld_elevation[0], ru_elevation[0], rd_elevation[0]]
)
equa_c = np.linalg.solve(equa_a, equa_b)
point_z = (
Xpixel * equa_c[0]
+ equa_c[1] * Xpixel * Yline
+ equa_c[2] * Yline
+ equa_c[3]
)
site_ele[i] = point_z
print(f"row:{Yline} col:{Xpixel} elev:{site_ele[i]}")
return site_ele
def to_line_coordination(point_x_s, point_y_s, point_x_e, point_y_e):
path_length = ((point_x_s - point_x_e) ** 2 + (point_y_s - point_y_e) ** 2) ** 0.5
n = round(path_length / 1)
point_x = np.linspace(point_x_s, point_x_e, n, endpoint=True)
point_y = np.linspace(point_y_s, point_y_e, n, endpoint=True)
r = np.array([point_x, point_y]).transpose()
return r
def distance(a, b):
r = np.sum((a - b) ** 2) ** 0.5
return r
if __name__ == "__main__":
dem = Dem(r"e:\西北院\天都山750kV线路\天都山-妙岭卫片\ASC\tianmiaoN03-dem.tif")
dem.get_dem_info(if_print=True)
line_coordination = to_line_coordination(
573284.886, 4104898.933, 574543.845, 4105825.047
)
elevation = dem.get_elevation(line_coordination)
# fdsfd = np.array([
# [573284.886, 4104898.933],
# [573286.584, 4104900.182],
# [573288.281, 4104901.431],
# [573289.979 , 4104902.68],
# [573291.677 , 4104903.929],
# [573293.374 , 4104905.178],
#
# ])
# elevation = dem.get_elevation(elevation)
doc = ezdxf.new(dxfversion="R2010")
msp = doc.modelspace()
x_axis = [0]
cord_0 = line_coordination[0, :]
for cord in line_coordination[1:]:
x_axis.append(distance(cord, cord_0))
msp.add_polyline2d(
[(x_axis[i] / 5, elevation[i] * 2) for i in range(len(elevation))]
)
doc.saveas("d.dxf")
print("Finished.")