From 67d9ee29ac91faa62dcf071403f6baa01de872f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=86=AC=E5=8D=AB?= Date: Sat, 8 Apr 2023 22:13:49 +0800 Subject: [PATCH] modify3-04-08 --- .idea/.name | 1 + QT/main_2.ui | 220 ++++++++++++++++++++++++++++++++++++++++ data.pos | 113 +++++++++++++++++++++ main_2.py | 276 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 610 insertions(+) create mode 100644 .idea/.name create mode 100644 QT/main_2.ui create mode 100644 data.pos create mode 100644 main_2.py diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..c659c60 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +main_2.py \ No newline at end of file diff --git a/QT/main_2.ui b/QT/main_2.ui new file mode 100644 index 0000000..82b0213 --- /dev/null +++ b/QT/main_2.ui @@ -0,0 +1,220 @@ + + + Form + + + + 0 + 0 + 790 + 757 + + + + Form + + + + + + + + + + 请导入数据文件 + + + + + + + 选择文件 + + + + + + + + + + + levels: + + + + + + + 14 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + gWeight: + + + + + + + 15 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 图例颜色选择 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + colorbar选择 + + + + + + + + + + + 请导入井文件 + + + + + + + 选择文件 + + + + + + + + + + + 油井颜色选择 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 水井颜色选择 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 清除数据 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 切换比例 + + + + + + + + + + + + + + + diff --git a/data.pos b/data.pos new file mode 100644 index 0000000..fffa8db --- /dev/null +++ b/data.pos @@ -0,0 +1,113 @@ +112 +-45.9 -71.2 1009.9 10.384 +-74.3 135.3 1006.9 65.182 +4.1 -38.5 1007.9 96.275 +-6 148.3 1006.9 254.818 +26.9 -5.9 1005.9 353.900 +-80.9 -10.8 1006.9 400.117 +-12.7 -127.7 1006.9 554.83 +66.9 -31.1 1009.9 837.881 +-28.1 -39.8 1006.9 1031.90 +-32.8 20.5 1006.9 1281.306 +36.5 -25.1 1006.9 1411.553 +-92.1 13.7 1006.9 1439.940 +-79.2 105.5 1011.9 1500.433 +99.7 63.1 1006.9 1542.607 +-26.3 3.3 1006.9 1549.297 +-63.1 -0.1 1010.9 1685.309 +-11.3 4.1 1004.9 1923.693 +34.6 -51.2 1006.9 2083.725 +36 69.5 1001.9 2136.755 +177.9 0.5 1012.9 2311.923 +-3.7 -5.8 1003.9 2345.193 +-8.1 -9 1004.9 2497.272 +-18.1 -5.2 1001.9 2673.628 +109.9 1.4 1006.9 2687.248 +137.8 -41.5 1006.9 2729.813 +44 -0.5 1008.9 2908.834 +-5.2 -32.3 1006.9 2913.675 +198.3 60.8 1006.9 3084.214 +11.1 -91.6 1006.9 3298.919 +-4.9 4.9 1013.9 3523.955 +19.6 -35.7 1008.9 3695.617 +-37.4 -26.9 1008.9 3916.784 +137.6 -42.9 1008.9 3938.952 +7.1 -10.1 1006.9 4063.108 +80.6 -103.8 1006.9 4107.944 +8.9 -20.9 1006.9 4183.385 +-44 -6 1007.9 4310.811 +-50.1 28.4 1009.9 4491.107 +104 97.2 1001.9 4715.866 +-60.3 -41.4 1006.9 4772.88 +61.3 58.9 1006.9 5042.962 +-121.1 -82.8 1006.9 5181.381 +31.5 -22.7 1002.9 5373.757 +-41.9 170 1006.9 5472.766 +-7.3 5.2 1006.9 5521.58 +82.8 102.9 1011.9 5636.275 +154.7 106.1 1011.9 5756.394 +-37.8 18.5 998.9 5880.473 +-151.5 -1.2 1005.9 5907.330 +10.2 4.8 1007.9 6024.836 +80.3 26.1 1002.9 6050.715 +-144.7 -101.9 1004.9 6163.211 +9.1 -57.8 1006.9 6404.898 +-122.7 -22.3 1011.9 6442.493 +8 2.4 1007.9 6652.171 +-182.8 -0.4 1004.9 6935.490 +25 48.1 1006.9 7113.943 +-93.6 116.2 1006.9 7237.796 +-110.2 -14 1006.9 7414.765 +-101.6 -19.4 1014.9 7642.493 +-76.8 -12.3 1007.9 7755.607 +1.6 -26.1 1008.9 7814.322 +-201.5 1.2 1006.9 7915.129 +37.5 63.5 1004.9 8003.43 +21.2 -173.7 1006.9 8089.478 +-5.5 143.1 1004.9 8316.296 +-67.6 -52.5 1007.9 8394.665 +-156.4 15.6 1010.9 8604.577 +-89.1 31.6 1007.9 8730.251 +-34.6 -29.8 1006.9 8874.838 +11.5 -131.3 1008.9 8938.423 +10.2 4.9 1004.9 8958.814 +9.2 -46.1 1006.9 9135.222 +95.2 171 1010.9 9267.394 +19.3 50.3 1010.9 9344.557 +127.7 -61.9 1010.9 9574.948 +7.5 -96.9 1005.9 9668.554 +6.1 -74.5 1008.9 9855.509 +-148.6 22.3 1009.9 9903.75 +39.2 103.1 1010.9 10066.866 +-80.4 48 1006.9 10208.58 +157.1 101.1 1006.9 10490.953 +-92 30.2 1007.9 10499.380 +111.1 73.2 1009.9 10643.279 +1.4 -29.7 1006.9 10690.866 +9.7 -141.4 1007.9 10804.785 +-0.4 2.5 1003.9 11081.846 +-8.9 -144.1 1000.9 11233.580 +-92.8 -26.4 1009.9 11419.149 +-35.8 38.4 1006.9 11657.103 +62.8 149.6 1006.9 11853.628 +40.7 73.8 1009.9 11895.399 +-38.8 -56.2 1004.9 12158.102 +69.3 -101.4 1004.9 12196.795 +-7.4 -4.4 1000.9 12254.790 +-176.5 -81.9 1001.9 12306.909 +-34.5 -42.2 1008.9 12375.591 +-2.5 3.9 1010.9 12391.64 +-2.5 -1 1008.9 12561.584 +-128.9 -34.1 1006.9 12669.326 +83.6 52.6 1001.9 12799.414 +-48.4 28.4 1005.9 13009.623 +-55.2 20.7 1002.9 13277.377 +2 -8.8 1010.9 13432.271 +-63.7 62.3 1006.9 13496.217 +29.3 29.6 1007.9 13550.730 +26 -0.2 1009.9 13572.226 +108.2 104.8 1006.9 13675.451 +-34.5 129 1006.9 14009.216 +113.9 133.5 1006.9 14096.519 +-32.7 -58.4 1006.9 14356.918 +1 45.2 1003.9 14454.743 diff --git a/main_2.py b/main_2.py new file mode 100644 index 0000000..2b93e9e --- /dev/null +++ b/main_2.py @@ -0,0 +1,276 @@ +# -*- coding: utf-8 -*- +import sys +from PySide6.QtWidgets import QApplication, QMainWindow, QGraphicsView, QFileDialog, QColorDialog +from PySide6.QtUiTools import QUiLoader +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas +from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar +from matplotlib.figure import Figure +import matplotlib.pyplot as plt +from PySide6.QtWidgets import QVBoxLayout +import CREAT +import numpy as np +import matplotlib as mpl +from matplotlib.path import Path +from matplotlib.patches import Patch +from PySide6.QtGui import QPalette +import random +# plt.legend(prop={'family': 'SimHei', 'size': 15}) + + +class MainWindow(QMainWindow): + def __init__(self): + super().__init__() + self.toolbar = None + self.canvas = None + self.file_paths = [] + self.get_color = lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF),range(n))) + self.color = self.get_color(100) # 图例颜色选择值 + self.colorbar = 'jet' + self.num = len(self.color) # 记录选择颜色的次数 + self.oilwell_color = 'k' + self.waterwell_color = 'k' + self.chose = 0 + self.num = 0 + # 加载UI文件 + self.ui = QUiLoader().load('QT/main_2.ui') + self.color_bar() + self.ui.legend_color.clicked.connect(self.legend_color_chose) # 选择图例颜色 + self.ui.colorbar_chose.currentTextChanged.connect(self.update_colorbar) # 更新colorbar + self.ui.chose_oilwell_color.clicked.connect(lambda: self.oil_well_color()) # 选择油井颜色 + self.ui.chose_oilwell_color.clicked.connect(lambda: self.plot_density()) # 更新油井颜色 + self.ui.chose_waterwell_color.clicked.connect(lambda: self.water_well_color()) # 选择水井颜色 + self.ui.chose_waterwell_color.clicked.connect(lambda: self.plot_density()) # 更新水井颜色 + # 更新图窗 + self.ui.graphicsView.repaint() + self.ui.graphicsView.update() + self.graphics_layout = QVBoxLayout(self.ui.graphicsView) + + self.ui.chose_datafile_button.clicked.connect(self.choose_datafile) + + self.ui.chose_wellfile_button.clicked.connect(self.choose_wellfile) + + # 绘制或清除数据和画布 + self.ui.clear_data.clicked.connect(lambda: self.clear_data()) + + # 绘图设置中心比例 + self.ui.center_scale.clicked.connect(lambda: self.center_scale()) + self.ui.center_scale.clicked.connect(lambda: self.plot_density()) + self.ui.level_chose.valueChanged.connect(lambda: self.plot_density()) + self.ui.gWeight_chose.valueChanged.connect(lambda: self.plot_density()) + + def self_levels(self): + self.levels = self.ui.level_chose.value() # 选择层参数 + self.plot_density() + + def self_gWeight(self): + self.gWeight = self.self.ui.gWeight_chose.value() + self.plot_density() + + def scene_fig(self): + fig = Figure() + # 在Figure对象中添加子图 + ax = fig.add_subplot(111) + return fig, ax + + def canvas_adjust(self, fig): + if self.canvas is not None: + # 从布局中删除旧的 canvas 和 toolbar + item = self.graphics_layout.takeAt(0) + while item: + widget = item.widget() + if widget: + widget.setParent(None) + item = self.graphics_layout.takeAt(0) + self.graphics_layout.removeWidget(self.canvas) + self.canvas = FigureCanvas(fig) + self.toolbar = NavigationToolbar(self.canvas, self.ui.graphicsView) + self.toolbar.setParent(self.canvas) + self.graphics_layout.addWidget(self.canvas) + self.adjustSize() + + def choose_datafile(self): + file_dialog = QFileDialog(self) + file_dialog.setFileMode(QFileDialog.ExistingFiles) + if file_dialog.exec(): + self.file_paths += file_dialog.selectedFiles() + self.ui.data_file_lineEdit.setText(','.join(self.file_paths)) + self.plot_density() + + def choose_wellfile(self): + file_dialog = QFileDialog(self) + file_dialog.setFileMode(QFileDialog.ExistingFiles) + if file_dialog.exec(): + file_paths = file_dialog.selectedFiles() + self.ui.well_file_lineEdit.setText(','.join(file_paths)) + self.plot_density() + + def legend_color_chose(self): + col = QColorDialog.getColor() + self.color[self.num] = col.name() + self.num += 1 + self.plot_density() + + def oil_well_color(self): + col = QColorDialog.getColor() + self.oilwell_color = col.name() + + def water_well_color(self): + col = QColorDialog.getColor() + self.waterwell_color = col.name() + + def color_bar(self): + self.ui.colorbar_chose.addItem("jet") + self.ui.colorbar_chose.addItem("viridis") + self.ui.colorbar_chose.addItem("coolwarm") + self.ui.colorbar_chose.addItem('plasma') + self.ui.colorbar_chose.addItem("magma") + self.ui.colorbar_chose.addItem("inferno") + + def update_colorbar(self): + colorbar_name = self.ui.colorbar_chose.currentText() + self.colorbar = plt.get_cmap(colorbar_name) + self.plot_density() + + def clear_data(self): + self.file_paths = [] + self.color = self.get_color(100) + self.num = 0 + self.ui.data_file_lineEdit.clear() + self.ui.well_file_lineEdit.clear() + self.canvas.figure.clf() # 清除画布上的内容 + self.canvas.draw() + + def center_scale(self): + if self.chose == 0: + self.chose = 1 + else: + self.chose = 0 + self.num = 0 + + def plot_density(self): + fig, ax = self.scene_fig() + data_name = self.ui.data_file_lineEdit.text().split(',') # 文件输入 =================1 + level_nums = self.ui.level_chose.value() # 层参数 =================2 + gWeight = self.ui.gWeight_chose.value() # 权重输入 =================3 + data = CREAT.create() + legend_elements = [] + if self.num == len(data_name): + self.num = 0 + for i in range(len(data_name)): + data.data_pre(data_name[i]) + data.contours_pre(level_nums, gWeight) + + """画出密度等高线""" + f = data.f + x_min, x_max = data.x_range[0], data.x_range[1] + y_min, y_max = data.y_range[0], data.y_range[1] + levels = data.levels + + xx, yy = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] + # 填充等高线图中间的区域 + + if len(data_name) > 1: + color_i = self.color[i] # """ 自动选择图例的颜色""" =================4 + level = [levels[0], levels[-1]] + ff = ax.contourf(xx, yy, f, levels=level, colors=color_i, alpha=1, zorder=len(data_name) - i) + # 生成图例 + legend_elements.append(Patch(facecolor=color_i, label='第'+f'{i+1}'+'次')) + if i == len(data_name)-1: + ax.legend(handles=legend_elements, loc='upper right', prop={'family': 'SimHei', 'size': 10}) + # ax.legend(prop={'family': 'SimHei', 'size': 15}) + else: + # colorbars = 'jet' # 输入选择的colorbar =================5 + cmap = mpl.colormaps.get_cmap(self.colorbar) + colors = cmap(np.linspace(0, 1, level_nums)) + ff = ax.contourf(xx, yy, f, levels=levels, colors=colors[0:level_nums], alpha=0.5, + zorder=len(data_name) - i) + # 生成colorbar + sm = mpl.cm.ScalarMappable(cmap=cmap, norm=mpl.colors.Normalize(vmin=ff.zmin, vmax=ff.zmax)) + sm.set_array([]) + fig.colorbar(sm, ax=ax) + + ax.set_xlabel('X') + ax.set_ylabel('Y') + ax.set_title('Density Distribution') + + wells_name = self.ui.well_file_lineEdit.text() # 井文件输入 =================6 + if wells_name == '': + self.canvas_adjust(fig) + else: + wells = CREAT.well_to_edge() + wells.wells_name_and_position(wells_name) + wells.welltoedge_distance(data.contours) + + """画井位信息""" + typee = wells.type + points = wells.position + namee = wells.name + min_distance = wells.min_distance + contours_p = wells.welltoedge_points + x_len = ((np.array(points)[:, 0].max()-np.array(points)[:,0].min())//100+2)*100 + y_len = ((np.array(points)[:, 1].max()-np.array(points)[:,1].min())//100+2)*100 + if self.chose == 1: + max_x = max(np.array([(np.array(points)[:, 0].max()//100+1)*100, (np.array(points)[:, 1].max()//100+1)*100, data.x_range[1], data.y_range[1]])) + min_x = max(np.array([(np.array(points)[:, 0].min()//100-1)*100, (np.array(points)[:, 1].min()//100-1)*100, data.x_range[0], data.y_range[0]])) + max_x = max(max_x, -min_x) + ax.set_xlim([-max_x, max_x]) + ax.set_ylim([-max_x, max_x]) + x_len = max(x_len, y_len) + y_len = x_len + + for i in range(len(points)): + is_inside = False # 标记点是否在等高线内部 + # path = Path(contours_p) + # if path.contains_point(points[i]): + if np.linalg.norm(contours_p[i]) >= np.linalg.norm(points[i]): + is_inside = True + if typee[i][0] == 0: + # oil_color = 'black' # 输入油井颜色选择 =================8 + ax.scatter(points[i][0], points[i][1], marker='o', edgecolors=self.oilwell_color, facecolors='none', s=70) + ax.scatter(points[i][0], points[i][1], marker='o', edgecolors=self.oilwell_color, facecolors='none', s=40, + linewidths=0.5) + ax.scatter(points[i][0], points[i][1], marker='o', edgecolors=self.oilwell_color, facecolors='none', s=20) + if wells.angle[i] <= 90: + ax.text(points[i][0] - 1/35*x_len, points[i][1] - 1/30*y_len, f'{namee[i][0]}', + fontdict={'size': '8', 'color': 'b'}) # 井名信息 + elif wells.angle[i] > 270: + ax.text(points[i][0] + 3/175*x_len, points[i][1] - 1/40*y_len, f'{namee[i][0]}', + fontdict={'size': '8', 'color': 'b'}) # 井名信息 + else: + ax.text(points[i][0] - 3/80*x_len, points[i][1] + 3/175*y_len, f'{namee[i][0]}', + fontdict={'size': '8', 'color': 'b'}) # 井名信息 + if not is_inside: + # 绘制油井边缘最短距离点的箭头并标出距离 + ax.quiver(points[i][0], points[i][1], contours_p[i][0] - points[i][0], + contours_p[i][1] - points[i][1], + angles='xy', scale=1.03, + scale_units='xy', width=0.002, zorder=len(data_name) + 1) # 绘制箭头 + if np.linalg.norm(np.array(points[i])-np.array(contours_p[i])) > 50: + ax.text(points[i][0] * 1 / 2 + contours_p[i][0] * 1 / 2 + min_distance[i] / 18, + points[i][1] * 1 / 2 + contours_p[i][1] * 1 / 2 - min_distance[i] / 18, + f'{round(min_distance[i], 2)}m', + fontdict={'size': '8', 'color': 'm'}) # 标出距离 + else: + if wells.angle[i] <= 90: + ax.text(points[i][0]-3/70*x_len, points[i][1]+1/40*y_len, f'{round(min_distance[i], 2)}m', + fontdict={'size': '8', 'color': 'm'}) # 标出距离 + elif wells.angle[i] > 270: + ax.text(points[i][0] - 1/35*x_len, points[i][1] + 1/30*y_len, f'{round(min_distance[i], 2)}m', + fontdict={'size': '8', 'color': 'm'}) # 标出距离 + else: + ax.text(points[i][0] + 3/140*x_len, points[i][1] - 1/40*y_len, f'{round(min_distance[i], 2)}m', + fontdict={'size': '8', 'color': 'm'}) # 标出距离 + else: + # water_color = 'black' # 水井颜色 =================9 + ax.scatter(points[i][0], points[i][1], marker='o', edgecolors=self.waterwell_color, facecolors=self.waterwell_color, s=70, zorder=len(data_name)+1) + ax.text(points[i][0] + 15, points[i][1] - 15, f'{namee[i][0]}', + fontdict={'size': '8', 'color': 'b'}, zorder=len(data_name) + 2) # 井名信息 + continue + self.canvas_adjust(fig) + + +if __name__ == '__main__': + app = QApplication([]) + stats = MainWindow() + stats.ui.show() + app.exec()