Skip to content

Commit

Permalink
handle typing of QgsFields
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha committed Jul 18, 2024
1 parent 33200f0 commit c35cd74
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 130 deletions.
34 changes: 14 additions & 20 deletions los_tools/constants/fields.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
from qgis.core import Qgis, QgsField, QgsFields
from qgis.PyQt.QtCore import QMetaType, QVariant
from qgis.core import QgsField, QgsFields

from los_tools.constants.field_names import FieldNames
from los_tools.utils import COLUMN_TYPE, COLUMN_TYPE_STRING


class Fields:
if Qgis.versionInt() >= 33800:
source_type = QMetaType.Type
source_type_string = source_type.QString
else:
source_type = QVariant.Type
source_type_string = source_type.String

_field_azimuth = QgsField(FieldNames.AZIMUTH, source_type.Double)
_field_angle_step = QgsField(FieldNames.ANGLE_STEP, source_type.Double)
_field_observer_x = QgsField(FieldNames.OBSERVER_X, source_type.Double)
_field_observer_y = QgsField(FieldNames.OBSERVER_Y, source_type.Double)

_field_azimuth = QgsField(FieldNames.AZIMUTH, COLUMN_TYPE.Double)
_field_angle_step = QgsField(FieldNames.ANGLE_STEP, COLUMN_TYPE.Double)
_field_observer_x = QgsField(FieldNames.OBSERVER_X, COLUMN_TYPE.Double)
_field_observer_y = QgsField(FieldNames.OBSERVER_Y, COLUMN_TYPE.Double)

_base_fields = QgsFields()
_base_fields.append(QgsField(FieldNames.LOS_TYPE, source_type_string))
_base_fields.append(QgsField(FieldNames.ID_OBSERVER, source_type.Int))
_base_fields.append(QgsField(FieldNames.ID_TARGET, source_type.Int))
_base_fields.append(QgsField(FieldNames.OBSERVER_OFFSET, source_type.Double))
_base_fields.append(QgsField(FieldNames.LOS_TYPE, COLUMN_TYPE_STRING))
_base_fields.append(QgsField(FieldNames.ID_OBSERVER, COLUMN_TYPE.Int))
_base_fields.append(QgsField(FieldNames.ID_TARGET, COLUMN_TYPE.Int))
_base_fields.append(QgsField(FieldNames.OBSERVER_OFFSET, COLUMN_TYPE.Double))

los_notarget_fields = QgsFields(_base_fields)
los_notarget_fields.append(_field_azimuth)
Expand All @@ -31,11 +25,11 @@ class Fields:

los_local_fields = QgsFields(_base_fields)

los_local_fields.append(QgsField(FieldNames.TARGET_OFFSET, source_type.Double))
los_local_fields.append(QgsField(FieldNames.TARGET_OFFSET, COLUMN_TYPE.Double))

los_global_fields = QgsFields(los_local_fields)
los_global_fields.append(QgsField(FieldNames.TARGET_X, source_type.Double))
los_global_fields.append(QgsField(FieldNames.TARGET_Y, source_type.Double))
los_global_fields.append(QgsField(FieldNames.TARGET_X, COLUMN_TYPE.Double))
los_global_fields.append(QgsField(FieldNames.TARGET_Y, COLUMN_TYPE.Double))

los_plugin_layer_fields = QgsFields(los_global_fields)
los_plugin_layer_fields.append(_field_azimuth)
Expand Down
20 changes: 7 additions & 13 deletions los_tools/gui/dialog_los_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
QgsProject,
QgsUnitTypes,
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import QMetaType, Qt, QVariant
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import (
QCheckBox,
QComboBox,
Expand All @@ -30,16 +29,11 @@
QWidget,
)

from los_tools.utils import _column_type_class

from ..constants.field_names import FieldNames
from .custom_classes import Distance, DistanceWidget

if Qgis.versionInt() >= 33800:
source_type = QMetaType.Type
source_type_string = source_type.QString
else:
source_type = QVariant.Type
source_type_string = source_type.String


class LoSSettings(QDialog):
def __init__(
Expand Down Expand Up @@ -270,12 +264,12 @@ def create_data_layer(self) -> QgsVectorLayer:
size_field_name = FieldNames.TEMPLATE_SIZE.replace("?", unit_name)

fields = QgsFields()
fields.append(QgsField(FieldNames.SIZE_ANGLE, QVariant.Double))
fields.append(QgsField(distance_field_name, QVariant.Double))
fields.append(QgsField(size_field_name, QVariant.Double))
fields.append(QgsField(FieldNames.SIZE_ANGLE, _column_type_class().Double))
fields.append(QgsField(distance_field_name, _column_type_class().Double))
fields.append(QgsField(size_field_name, _column_type_class().Double))

layer = QgsMemoryProviderUtils.createMemoryLayer(
"Sampling Table", fields=fields, geometryType=QgsWkbTypes.NoGeometry
"Sampling Table", fields=fields, geometryType=Qgis.WkbType.NoGeometry
)

angle = self.object_angle_size.value()
Expand Down
39 changes: 19 additions & 20 deletions los_tools/processing/analyse_los/tool_analyse_los.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
QgsProcessingUtils,
QgsVectorLayer,
)
from qgis.PyQt.QtCore import QVariant

from los_tools.classes.classes_los import LoSGlobal, LoSLocal, LoSWithoutTarget
from los_tools.constants.field_names import FieldNames
from los_tools.constants.names_constants import NamesConstants
from los_tools.processing.tools.util_functions import get_los_type
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class AnalyseLosAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -98,28 +97,28 @@ def processAlgorithm(self, parameters, context: QgsProcessingContext, feedback:
fields.append(QgsField(attribute.name(), attribute.type()))

if los_type == NamesConstants.LOS_LOCAL:
fields.append(QgsField(FieldNames.VISIBLE, QVariant.Bool))
fields.append(QgsField(FieldNames.VIEWING_ANGLE, QVariant.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF, QVariant.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_LH, QVariant.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_LH, QVariant.Double))
fields.append(QgsField(FieldNames.SLOPE_DIFFERENCE_LH, QVariant.Double))
fields.append(QgsField(FieldNames.HORIZON_COUNT, QVariant.Int))
fields.append(QgsField(FieldNames.DISTANCE_LH, QVariant.Double))
# los_layer.addAttribute(QgsField(FieldNames.FUZZY_VISIBILITY, QVariant.Double))
fields.append(QgsField(FieldNames.VISIBLE, COLUMN_TYPE.Bool))
fields.append(QgsField(FieldNames.VIEWING_ANGLE, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_LH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_LH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.SLOPE_DIFFERENCE_LH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.HORIZON_COUNT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.DISTANCE_LH, COLUMN_TYPE.Double))
# los_layer.addAttribute(QgsField(FieldNames.FUZZY_VISIBILITY, COLUMN_TYPE.Double))

elif los_type == NamesConstants.LOS_GLOBAL:
fields.append(QgsField(FieldNames.VISIBLE, QVariant.Bool))
fields.append(QgsField(FieldNames.ANGLE_DIFF_GH, QVariant.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_GH, QVariant.Double))
fields.append(QgsField(FieldNames.HORIZON_COUNT_BEHIND, QVariant.Int))
fields.append(QgsField(FieldNames.DISTANCE_GH, QVariant.Double))
fields.append(QgsField(FieldNames.VISIBLE, COLUMN_TYPE.Bool))
fields.append(QgsField(FieldNames.ANGLE_DIFF_GH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_GH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.HORIZON_COUNT_BEHIND, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.DISTANCE_GH, COLUMN_TYPE.Double))

elif los_type == NamesConstants.LOS_NO_TARGET:
fields.append(QgsField(FieldNames.MAXIMAL_VERTICAL_ANGLE, QVariant.Double))
fields.append(QgsField(FieldNames.DISTANCE_GH, QVariant.Double))
fields.append(QgsField(FieldNames.DISTANCE_LH, QVariant.Double))
fields.append(QgsField(FieldNames.VERTICAL_ANGLE_LH, QVariant.Double))
fields.append(QgsField(FieldNames.MAXIMAL_VERTICAL_ANGLE, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.DISTANCE_GH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.DISTANCE_LH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.VERTICAL_ANGLE_LH, COLUMN_TYPE.Double))

sink, dest_id = self.parameterAsSink(
parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import Qt, QVariant
from qgis.PyQt.QtCore import Qt

from los_tools.classes.classes_los import LoSGlobal, LoSLocal, LoSWithoutTarget
from los_tools.constants.field_names import FieldNames
from los_tools.constants.names_constants import NamesConstants
from los_tools.constants.textlabels import TextLabels
from los_tools.processing.tools.util_functions import get_los_type
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class ExtractLoSVisibilityPartsAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -108,9 +108,9 @@ def processAlgorithm(self, parameters, context, feedback):
los_type = get_los_type(los_layer, field_names)

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_OBSERVER, QVariant.Int))
fields.append(QgsField(FieldNames.ID_TARGET, QVariant.Int))
fields.append(QgsField(FieldNames.VISIBLE, QVariant.Bool))
fields.append(QgsField(FieldNames.ID_OBSERVER, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_TARGET, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.VISIBLE, COLUMN_TYPE.Bool))

sink, self.dest_id = self.parameterAsSink(
parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import Qt, QVariant
from qgis.PyQt.QtCore import Qt

from los_tools.classes.classes_los import LoSWithoutTarget
from los_tools.constants.field_names import FieldNames
from los_tools.constants.names_constants import NamesConstants
from los_tools.constants.textlabels import TextLabels
from los_tools.processing.tools.util_functions import get_los_type, line_to_polygon
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class ExtractLoSVisibilityPolygonsAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -114,9 +114,9 @@ def processAlgorithm(self, parameters, context, feedback: QgsProcessingFeedback)
los_type = get_los_type(los_layer, field_names)

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_OBSERVER, QVariant.Int))
fields.append(QgsField(FieldNames.ID_TARGET, QVariant.Int))
fields.append(QgsField(FieldNames.VISIBLE, QVariant.Bool))
fields.append(QgsField(FieldNames.ID_OBSERVER, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_TARGET, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.VISIBLE, COLUMN_TYPE.Bool))

sink, self.dest_id = self.parameterAsSink(
parameters,
Expand Down
18 changes: 9 additions & 9 deletions los_tools/processing/analyse_los/tool_extract_points_los.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import Qt, QVariant
from qgis.PyQt.QtCore import Qt

from los_tools.classes.classes_los import LoSGlobal, LoSLocal, LoSWithoutTarget
from los_tools.constants.field_names import FieldNames
from los_tools.constants.names_constants import NamesConstants
from los_tools.constants.textlabels import TextLabels
from los_tools.processing.tools.util_functions import get_los_type
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class ExtractPointsLoSAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -123,17 +123,17 @@ def processAlgorithm(self, parameters, context, feedback):
los_type = get_los_type(los_layer, field_names)

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_OBSERVER, QVariant.Int))
fields.append(QgsField(FieldNames.ID_TARGET, QVariant.Int))
fields.append(QgsField(FieldNames.VISIBLE, QVariant.Bool))
fields.append(QgsField(FieldNames.ID_OBSERVER, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_TARGET, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.VISIBLE, COLUMN_TYPE.Bool))

if extended_attributes:
fields.append(QgsField(FieldNames.ELEVATION_DIFF_LH, QVariant.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_LH, QVariant.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_LH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_LH, COLUMN_TYPE.Double))

if los_type == NamesConstants.LOS_GLOBAL or los_type == NamesConstants.LOS_NO_TARGET:
fields.append(QgsField(FieldNames.ELEVATION_DIFF_GH, QVariant.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_GH, QVariant.Double))
fields.append(QgsField(FieldNames.ELEVATION_DIFF_GH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ANGLE_DIFF_GH, COLUMN_TYPE.Double))

sink, self.dest_id = self.parameterAsSink(
parameters,
Expand Down
9 changes: 4 additions & 5 deletions los_tools/processing/azimuths/tool_azimuth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
QgsProcessingUtils,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import QVariant

from los_tools.constants.field_names import FieldNames
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class AzimuthPointPolygonAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -78,9 +77,9 @@ def processAlgorithm(self, parameters, context, feedback):
object_field_id = self.parameterAsString(parameters, self.OBJECT_LAYER_FIELD_ID, context)

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_POINT, QVariant.Int))
fields.append(QgsField(FieldNames.ID_OBJECT, QVariant.Int))
fields.append(QgsField(FieldNames.AZIMUTH, QVariant.Double))
fields.append(QgsField(FieldNames.ID_POINT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_OBJECT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.AZIMUTH, COLUMN_TYPE.Double))

sink, dest_id = self.parameterAsSink(
parameters,
Expand Down
11 changes: 5 additions & 6 deletions los_tools/processing/azimuths/tool_limit_angles_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
QgsProject,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import QVariant

from los_tools.constants.field_names import FieldNames
from los_tools.constants.names_constants import NamesConstants
from los_tools.processing.tools.util_functions import get_los_type
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class LimitAnglesAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -97,10 +96,10 @@ def processAlgorithm(self, parameters, context, feedback):
coord_transform = None

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_OBSERVER, QVariant.Int))
fields.append(QgsField(FieldNames.ID_OBJECT, QVariant.Int))
fields.append(QgsField(FieldNames.AZIMUTH_MIN, QVariant.Double))
fields.append(QgsField(FieldNames.AZIMUTH_MAX, QVariant.Double))
fields.append(QgsField(FieldNames.ID_OBSERVER, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_OBJECT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.AZIMUTH_MIN, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.AZIMUTH_MAX, COLUMN_TYPE.Double))

sink, dest_id = self.parameterAsSink(
parameters,
Expand Down
9 changes: 4 additions & 5 deletions los_tools/processing/create_points/tool_points_around.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
QgsProcessingUtils,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import QVariant

from los_tools.constants.field_names import FieldNames
from los_tools.processing.tools.util_functions import get_max_decimal_numbers, round_all_values
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class CreatePointsAroundAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -116,9 +115,9 @@ def processAlgorithm(self, parameters, context, feedback):
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_ORIGINAL_POINT, QVariant.Int))
fields.append(QgsField(FieldNames.AZIMUTH, QVariant.Double))
fields.append(QgsField(FieldNames.ANGLE_STEP_POINTS, QVariant.Double))
fields.append(QgsField(FieldNames.ID_ORIGINAL_POINT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.AZIMUTH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ANGLE_STEP_POINTS, COLUMN_TYPE.Double))

sink, dest_id = self.parameterAsSink(
parameters,
Expand Down
11 changes: 5 additions & 6 deletions los_tools/processing/create_points/tool_points_by_azimuths.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
QgsProcessingUtils,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import QVariant

from los_tools.constants.field_names import FieldNames
from los_tools.processing.tools.util_functions import get_max_decimal_numbers, round_all_values
from los_tools.utils import get_doc_file
from los_tools.utils import COLUMN_TYPE, get_doc_file


class CreatePointsInAzimuthsAlgorithm(QgsProcessingAlgorithm):
Expand Down Expand Up @@ -136,10 +135,10 @@ def processAlgorithm(self, parameters, context, feedback):
round_digits = get_max_decimal_numbers([angle_min, angle_max, angle_step])

fields = QgsFields()
fields.append(QgsField(FieldNames.ID_ORIGINAL_POINT, QVariant.Int))
fields.append(QgsField(FieldNames.ID_POINT, QVariant.Int))
fields.append(QgsField(FieldNames.AZIMUTH, QVariant.Double))
fields.append(QgsField(FieldNames.ANGLE_STEP_POINTS, QVariant.Double))
fields.append(QgsField(FieldNames.ID_ORIGINAL_POINT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.ID_POINT, COLUMN_TYPE.Int))
fields.append(QgsField(FieldNames.AZIMUTH, COLUMN_TYPE.Double))
fields.append(QgsField(FieldNames.ANGLE_STEP_POINTS, COLUMN_TYPE.Double))

sink, dest_id = self.parameterAsSink(
parameters,
Expand Down
Loading

0 comments on commit c35cd74

Please sign in to comment.