|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + Heatmap.py |
| 6 | + --------------------- |
| 7 | + Date : December 2016 |
| 8 | + Copyright : (C) 2016 by Nyall Dawson |
| 9 | + Email : nyall dot dawson at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Nyall Dawson' |
| 21 | +__date__ = 'December 2016' |
| 22 | +__copyright__ = '(C) 2016, Nyall Dawson' |
| 23 | + |
| 24 | +from processing.gui.wrappers import WidgetWrapper, DIALOG_STANDARD |
| 25 | +from processing.tools import dataobjects |
| 26 | +from processing.core.parameters import _resolveLayers |
| 27 | +import os |
| 28 | +from qgis.PyQt import uic |
| 29 | +from qgis.gui import QgsDoubleSpinBox |
| 30 | +from qgis.core import QgsRectangle |
| 31 | + |
| 32 | +pluginPath = os.path.dirname(__file__) |
| 33 | +WIDGET, BASE = uic.loadUiType( |
| 34 | + os.path.join(pluginPath, 'RasterResolutionWidget.ui')) |
| 35 | + |
| 36 | + |
| 37 | +class HeatmapPixelSizeWidget(BASE, WIDGET): |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + super(HeatmapPixelSizeWidget, self).__init__(None) |
| 41 | + self.setupUi(self) |
| 42 | + |
| 43 | + self.layer_bounds = QgsRectangle() |
| 44 | + self.layer = None |
| 45 | + self.raster_bounds = QgsRectangle() |
| 46 | + self.radius = 100 |
| 47 | + self.radius_field = None |
| 48 | + |
| 49 | + self.mCellXSpinBox.setShowClearButton(False) |
| 50 | + self.mCellYSpinBox.setShowClearButton(False) |
| 51 | + self.mRowsSpinBox.setShowClearButton(False) |
| 52 | + self.mColumnsSpinBox.setShowClearButton(False) |
| 53 | + |
| 54 | + self.mCellYSpinBox.valueChanged.connect(self.mCellXSpinBox.setValue) |
| 55 | + self.mCellXSpinBox.valueChanged.connect(self.pixelSizeChanged) |
| 56 | + self.mRowsSpinBox.valueChanged.connect(self.rowsChanged) |
| 57 | + self.mColumnsSpinBox.valueChanged.connect(self.columnsChanged) |
| 58 | + |
| 59 | + def setRadius(self, radius): |
| 60 | + self.radius = radius |
| 61 | + self.recalculate_bounds() |
| 62 | + |
| 63 | + def setRadiusField(self, radius_field): |
| 64 | + self.radius_field = radius_field |
| 65 | + self.recalculate_bounds() |
| 66 | + |
| 67 | + def setLayer(self, layer): |
| 68 | + if not layer: |
| 69 | + return |
| 70 | + bounds = layer.extent() |
| 71 | + if bounds.isNull(): |
| 72 | + return |
| 73 | + |
| 74 | + self.layer = layer |
| 75 | + self.layer_bounds = bounds |
| 76 | + self.recalculate_bounds() |
| 77 | + |
| 78 | + def recalculate_bounds(self): |
| 79 | + self.raster_bounds = QgsRectangle(self.layer_bounds) |
| 80 | + |
| 81 | + if not self.layer: |
| 82 | + return |
| 83 | + |
| 84 | + max_radius = self.radius |
| 85 | + if self.radius_field: |
| 86 | + idx = self.layer.fields().lookupField(self.radius_field) |
| 87 | + try: |
| 88 | + max_radius = float(self.layer.maximumValue(idx)) |
| 89 | + except: |
| 90 | + pass |
| 91 | + |
| 92 | + self.raster_bounds.setXMinimum(self.raster_bounds.xMinimum() - max_radius) |
| 93 | + self.raster_bounds.setYMinimum(self.raster_bounds.yMinimum() - max_radius) |
| 94 | + self.raster_bounds.setXMaximum(self.raster_bounds.xMaximum() + max_radius) |
| 95 | + self.raster_bounds.setYMaximum(self.raster_bounds.yMaximum() + max_radius) |
| 96 | + |
| 97 | + self.pixelSizeChanged() |
| 98 | + |
| 99 | + def pixelSizeChanged(self): |
| 100 | + cell_size = self.mCellXSpinBox.value() |
| 101 | + if cell_size <= 0: |
| 102 | + return |
| 103 | + self.mCellYSpinBox.blockSignals(True) |
| 104 | + self.mCellYSpinBox.setValue(cell_size) |
| 105 | + self.mCellYSpinBox.blockSignals(False) |
| 106 | + rows = max(round(self.raster_bounds.height() / cell_size) + 1, 1) |
| 107 | + cols = max(round(self.raster_bounds.width() / cell_size) + 1, 1) |
| 108 | + self.mRowsSpinBox.blockSignals(True) |
| 109 | + self.mRowsSpinBox.setValue(rows) |
| 110 | + self.mRowsSpinBox.blockSignals(False) |
| 111 | + self.mColumnsSpinBox.blockSignals(True) |
| 112 | + self.mColumnsSpinBox.setValue(cols) |
| 113 | + self.mColumnsSpinBox.blockSignals(False) |
| 114 | + |
| 115 | + def rowsChanged(self): |
| 116 | + rows = self.mRowsSpinBox.value() |
| 117 | + if rows <= 0: |
| 118 | + return |
| 119 | + cell_size = self.raster_bounds.height() / rows |
| 120 | + cols = max(round(self.raster_bounds.width() / cell_size) + 1, 1) |
| 121 | + self.mColumnsSpinBox.blockSignals(True) |
| 122 | + self.mColumnsSpinBox.setValue(cols) |
| 123 | + self.mColumnsSpinBox.blockSignals(False) |
| 124 | + for w in [self.mCellXSpinBox, self.mCellYSpinBox]: |
| 125 | + w.blockSignals(True) |
| 126 | + w.setValue(cell_size) |
| 127 | + w.blockSignals(False) |
| 128 | + |
| 129 | + def columnsChanged(self): |
| 130 | + cols = self.mColumnsSpinBox.value() |
| 131 | + if cols < 2: |
| 132 | + return |
| 133 | + cell_size = self.raster_bounds.width() / (cols - 1) |
| 134 | + rows = max(round(self.raster_bounds.height() / cell_size), 1) |
| 135 | + self.mRowsSpinBox.blockSignals(True) |
| 136 | + self.mRowsSpinBox.setValue(rows) |
| 137 | + self.mRowsSpinBox.blockSignals(False) |
| 138 | + for w in [self.mCellXSpinBox, self.mCellYSpinBox]: |
| 139 | + w.blockSignals(True) |
| 140 | + w.setValue(cell_size) |
| 141 | + w.blockSignals(False) |
| 142 | + |
| 143 | + def setValue(self, value): |
| 144 | + try: |
| 145 | + numeric_value = float(value) |
| 146 | + except: |
| 147 | + return False |
| 148 | + |
| 149 | + self.mCellXSpinBox.setValue(numeric_value) |
| 150 | + self.mCellYSpinBox.setValue(numeric_value) |
| 151 | + return True |
| 152 | + |
| 153 | + def value(self): |
| 154 | + return self.mCellXSpinBox.value() |
| 155 | + |
| 156 | + |
| 157 | +class HeatmapPixelSizeWidgetWrapper(WidgetWrapper): |
| 158 | + |
| 159 | + def _panel(self): |
| 160 | + return HeatmapPixelSizeWidget() |
| 161 | + |
| 162 | + def createWidget(self): |
| 163 | + if self.dialogType == DIALOG_STANDARD: |
| 164 | + return self._panel() |
| 165 | + else: |
| 166 | + w = QgsDoubleSpinBox() |
| 167 | + w.setShowClearButton(False) |
| 168 | + w.setMinimum(0) |
| 169 | + w.setMaximum(99999999999) |
| 170 | + w.setDecimals(6) |
| 171 | + w.setTooltip(self.tr('Resolution of each pixel in output raster, in layer units')) |
| 172 | + |
| 173 | + def postInitialize(self, wrappers): |
| 174 | + if self.dialogType != DIALOG_STANDARD: |
| 175 | + return |
| 176 | + |
| 177 | + for wrapper in wrappers: |
| 178 | + if wrapper.param.name == self.param.parent_layer: |
| 179 | + self.setLayer(wrapper.value()) |
| 180 | + wrapper.widgetValueHasChanged.connect(self.parentLayerChanged) |
| 181 | + elif wrapper.param.name == self.param.radius_param: |
| 182 | + self.setRadius(wrapper.value()) |
| 183 | + wrapper.widgetValueHasChanged.connect(self.radiusChanged) |
| 184 | + elif wrapper.param.name == self.param.radius_field_param: |
| 185 | + self.setLayer(wrapper.value()) |
| 186 | + wrapper.widgetValueHasChanged.connect(self.radiusFieldChanged) |
| 187 | + |
| 188 | + def parentLayerChanged(self, wrapper): |
| 189 | + self.setLayer(wrapper.value()) |
| 190 | + |
| 191 | + def setLayer(self, layer): |
| 192 | + if isinstance(layer, str): |
| 193 | + layer = dataobjects.getObjectFromUri(_resolveLayers(layer)) |
| 194 | + self.widget.setLayer(layer) |
| 195 | + |
| 196 | + def radiusChanged(self, wrapper): |
| 197 | + self.setRadius(wrapper.value()) |
| 198 | + |
| 199 | + def setRadius(self, radius): |
| 200 | + self.widget.setRadius(radius) |
| 201 | + |
| 202 | + def radiusFieldChanged(self, wrapper): |
| 203 | + self.setRadiusField(wrapper.value()) |
| 204 | + |
| 205 | + def setRadiusField(self, radius_field): |
| 206 | + self.widget.setRadiusField(radius_field) |
| 207 | + |
| 208 | + def setValue(self, value): |
| 209 | + return self.widget.setValue(value) |
| 210 | + |
| 211 | + def value(self): |
| 212 | + return self.widget.value() |
0 commit comments