Skip to content

Commit

Permalink
isort
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha committed Jul 17, 2024
1 parent b0fe85b commit 254e4e9
Show file tree
Hide file tree
Showing 44 changed files with 671 additions and 1,546 deletions.
2 changes: 1 addition & 1 deletion los_tools/classes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .classes_los import LoSGlobal, LoSLocal, LoSWithoutTarget
from .list_raster import ListOfRasters
from .sampling_distance_matrix import SamplingDistanceMatrix
from .classes_los import LoSLocal, LoSGlobal, LoSWithoutTarget
121 changes: 28 additions & 93 deletions los_tools/classes/classes_los.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

import math
from typing import List, Optional, Union

from qgis.core import QgsPoint, QgsFeature, QgsGeometry
from qgis.core import QgsFeature, QgsGeometry, QgsPoint

from los_tools.processing.tools.util_functions import (
calculate_distance,
line_geometry_to_coords,
)
from los_tools.constants.field_names import FieldNames
from los_tools.processing.tools.util_functions import calculate_distance, line_geometry_to_coords


class LoS:
Expand Down Expand Up @@ -61,9 +59,7 @@ def __identify_horizons(self) -> None:
if i == len(self.points) - 1:
self.horizon.append(False)
else:
self.horizon.append(
(self.visible[i] is True) and (self.visible[i + 1] is False)
)
self.horizon.append((self.visible[i] is True) and (self.visible[i + 1] is False))

def __parse_points(self, points: List[List[float]]) -> None:
max_angle_temp = -180
Expand All @@ -75,62 +71,41 @@ def __parse_points(self, points: List[List[float]]) -> None:
sampling_distance: float = None

if self.is_global:
target_distance = calculate_distance(
first_point_x, first_point_y, self.target_x, self.target_y
)
sampling_distance = calculate_distance(
points[0][0], points[0][1], points[1][0], points[1][1]
)
target_distance = calculate_distance(first_point_x, first_point_y, self.target_x, self.target_y)
sampling_distance = calculate_distance(points[0][0], points[0][1], points[1][0], points[1][1])

for i in range(0, len(points)):
point_x = points[i][0]
point_y = points[i][1]
point_z = points[i][2]

distance = calculate_distance(
first_point_x, first_point_y, point_x, point_y
)
distance = calculate_distance(first_point_x, first_point_y, point_x, point_y)

if self.use_curvature_corrections:
point_z = self._curvature_corrections(
point_z, distance, self.refraction_coefficient
)
target_offset = self._curvature_corrections(
self.target_offset, distance, self.refraction_coefficient
)
point_z = self._curvature_corrections(point_z, distance, self.refraction_coefficient)
target_offset = self._curvature_corrections(self.target_offset, distance, self.refraction_coefficient)

if i == 0:
self.points[i] = [point_x, point_y, 0, first_point_z, -90]

elif (
self.is_global
and math.fabs(target_distance - distance) < sampling_distance / 2
):
elif self.is_global and math.fabs(target_distance - distance) < sampling_distance / 2:
self.points[i] = [
point_x,
point_y,
distance,
point_z + target_offset,
self._angle_vertical(
distance, point_z + target_offset - first_point_z
),
self._angle_vertical(distance, point_z + target_offset - first_point_z),
]

self.target_index = i

elif (
not self.is_global
and not self.is_without_target
and i == len(points) - 1
):
elif not self.is_global and not self.is_without_target and i == len(points) - 1:
self.points[i] = [
point_x,
point_y,
distance,
point_z + target_offset,
self._angle_vertical(
distance, point_z + target_offset - first_point_z
),
self._angle_vertical(distance, point_z + target_offset - first_point_z),
]

else:
Expand Down Expand Up @@ -158,9 +133,7 @@ def __parse_points(self, points: List[List[float]]) -> None:
self.visible.append(True)
else:
# [i] and [-1] actually points to the same point, no idea why I wrote this way
self.visible.append(
self.previous_max_angle[i] < self.points[i - 1][self.VERTICAL_ANGLE]
)
self.visible.append(self.previous_max_angle[i] < self.points[i - 1][self.VERTICAL_ANGLE])

def __str__(self):
string = ""
Expand All @@ -181,18 +154,10 @@ def _angle_vertical(distance: float, elev_diff: float) -> float:
return 90 if distance == 0 else math.degrees(math.atan(elev_diff / distance))

@staticmethod
def _curvature_corrections(
elev: float, dist: float, ref_coeff: float, earth_diameter: float = 12740000
) -> float:
return (
elev
- (math.pow(dist, 2) / earth_diameter)
+ ref_coeff * (math.pow(dist, 2) / earth_diameter)
)
def _curvature_corrections(elev: float, dist: float, ref_coeff: float, earth_diameter: float = 12740000) -> float:
return elev - (math.pow(dist, 2) / earth_diameter) + ref_coeff * (math.pow(dist, 2) / earth_diameter)

def is_visible_at_index(
self, index: int, return_integer: bool = False
) -> Union[bool, int]:
def is_visible_at_index(self, index: int, return_integer: bool = False) -> Union[bool, int]:
return int(self.visible[index]) if return_integer else self.visible[index]

def get_geom_at_index(self, index: int) -> QgsPoint:
Expand Down Expand Up @@ -248,21 +213,13 @@ def get_global_horizon_angle(self) -> float:
def get_angle_difference_global_horizon_at_point(self, index_point: int) -> float:
horizon_angle = -90
if self._get_global_horizon_index() != 0:
horizon_angle = self.points[self._get_global_horizon_index()][
self.VERTICAL_ANGLE
]
horizon_angle = self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]
return self.points[index_point][self.VERTICAL_ANGLE] - horizon_angle

def get_elevation_difference_global_horizon_at_point(
self, index_point: int
) -> float:
def get_elevation_difference_global_horizon_at_point(self, index_point: int) -> float:
elev_difference_horizon = self.points[index_point][self.Z] - (
self.points[0][self.Z]
+ math.tan(
math.radians(
self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]
)
)
+ math.tan(math.radians(self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]))
* self.points[index_point][self.DISTANCE]
)
return elev_difference_horizon
Expand All @@ -283,8 +240,7 @@ def get_angle_difference_horizon_at_point(self, index_point: int) -> float:

if horizon_index is not None:
horizon_angle = (
self.points[index_point][self.VERTICAL_ANGLE]
- self.points[horizon_index][self.VERTICAL_ANGLE]
self.points[index_point][self.VERTICAL_ANGLE] - self.points[horizon_index][self.VERTICAL_ANGLE]
)
else:
horizon_angle = None
Expand All @@ -300,9 +256,7 @@ def get_elevation_difference_horizon_at_point(self, index_point: int) -> float:
if horizon_index is not None:
elev_difference_horizon = self.points[index_point][self.Z] - (
self.points[0][self.Z]
+ math.tan(
math.radians(self.points[horizon_index][self.VERTICAL_ANGLE])
)
+ math.tan(math.radians(self.points[horizon_index][self.VERTICAL_ANGLE]))
* self.points[index_point][self.DISTANCE]
)
else:
Expand Down Expand Up @@ -362,22 +316,13 @@ def get_elevation_difference(self) -> float:
return self.points[0][self.Z] - self.points[-1][self.Z]

def get_angle_difference_local_horizon(self) -> float:
return (
self.target_angle
- self.points[self._get_max_local_horizon_index()][self.VERTICAL_ANGLE]
)
return self.target_angle - self.points[self._get_max_local_horizon_index()][self.VERTICAL_ANGLE]

def get_elevation_difference_local_horizon(self) -> float:
return (
self.points[-1][self.Z]
- self.points[0][self.Z]
- math.tan(
math.radians(
self.points[self._get_max_local_horizon_index()][
self.VERTICAL_ANGLE
]
)
)
- math.tan(math.radians(self.points[self._get_max_local_horizon_index()][self.VERTICAL_ANGLE]))
* self.points[-1][self.DISTANCE]
)

Expand Down Expand Up @@ -478,9 +423,7 @@ def from_feature(
)

def is_target_visible(self, return_integer: bool = False) -> Union[bool, int]:
return self.is_visible_at_index(
index=self.target_index, return_integer=return_integer
)
return self.is_visible_at_index(index=self.target_index, return_integer=return_integer)

def _get_global_horizon_index(self) -> int:
if self.global_horizon_index is not None:
Expand All @@ -496,19 +439,13 @@ def _get_global_horizon_index(self) -> int:
def get_angle_difference_global_horizon(self) -> float:
horizon_angle = -90
if self._get_global_horizon_index() != 0:
horizon_angle = self.points[self._get_global_horizon_index()][
self.VERTICAL_ANGLE
]
horizon_angle = self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]
return self.points[self.target_index][self.VERTICAL_ANGLE] - horizon_angle

def get_elevation_difference_global_horizon(self) -> float:
elev_difference_horizon = self.points[self.target_index][self.Z] - (
self.points[0][self.Z]
+ math.tan(
math.radians(
self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]
)
)
+ math.tan(math.radians(self.points[self._get_global_horizon_index()][self.VERTICAL_ANGLE]))
* self.points[self.target_index][self.DISTANCE]
)

Expand Down Expand Up @@ -641,9 +578,7 @@ def get_global_horizon_elevation_difference(self):
if global_horizon_index is not None and local_horizon_index is not None:
return (
self.points[global_horizon_index][self.Z]
- math.tan(
math.radians(self.points[local_horizon_index][self.VERTICAL_ANGLE])
)
- math.tan(math.radians(self.points[local_horizon_index][self.VERTICAL_ANGLE]))
* self.points[global_horizon_index][self.DISTANCE]
)

Expand Down
33 changes: 8 additions & 25 deletions los_tools/classes/list_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,20 @@ def validate_bands(rasters: List[QgsMapLayer]) -> Tuple[bool, str]:
return True, ""

@staticmethod
def validate_crs(
rasters: List[QgsMapLayer], crs: QgsCoordinateReferenceSystem = None
) -> Tuple[bool, str]:
def validate_crs(rasters: List[QgsMapLayer], crs: QgsCoordinateReferenceSystem = None) -> Tuple[bool, str]:
if crs is None:
crs = rasters[0].crs()

first_raster_crs = rasters[0].crs()

for raster in rasters:
if not first_raster_crs == raster.crs():
msg = (
"All CRS for all rasters must be equal. " "Right now they are not."
)
msg = "All CRS for all rasters must be equal. " "Right now they are not."

return False, msg

if not raster.crs() == crs:
msg = (
"Provided crs template and raster layers crs must be equal. "
"Right now they are not."
)
msg = "Provided crs template and raster layers crs must be equal. " "Right now they are not."

return False, msg

Expand Down Expand Up @@ -176,30 +169,20 @@ def extract_interpolated_value(self, point: QgsPoint) -> Optional[float]:

return None

def _convert_point_to_crs_of_raster(
self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem
) -> QgsPoint:
def _convert_point_to_crs_of_raster(self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem) -> QgsPoint:
if crs.toWkt() == self.rasters[0].crs().toWkt():
return QgsPoint(point.x(), point.y())

transformer = QgsCoordinateTransform(
crs, self.rasters[0].crs(), QgsCoordinateTransformContext()
)
transformer = QgsCoordinateTransform(crs, self.rasters[0].crs(), QgsCoordinateTransformContext())
geom = QgsGeometry.fromPointXY(point)
geom.transform(transformer)
transformed_point = geom.asPoint()
return QgsPoint(transformed_point.x(), transformed_point.y())

def extract_interpolated_value_at_point(
self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem
) -> float:
return self.extract_interpolated_value(
self._convert_point_to_crs_of_raster(point, crs)
)
def extract_interpolated_value_at_point(self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem) -> float:
return self.extract_interpolated_value(self._convert_point_to_crs_of_raster(point, crs))

def sampling_from_raster_at_point(
self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem
) -> str:
def sampling_from_raster_at_point(self, point: QgsPointXY, crs: QgsCoordinateReferenceSystem) -> str:
point = self._convert_point_to_crs_of_raster(point, crs)
for i, raster_dp in enumerate(self.rasters_dp):
value = bilinear_interpolated_value(raster_dp, point)
Expand Down
35 changes: 8 additions & 27 deletions los_tools/classes/sampling_distance_matrix.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List, Union

import numpy as np

from qgis.core import QgsVectorLayer, QgsFeature, QgsPoint, QgsGeometry, QgsLineString
from qgis.core import QgsFeature, QgsGeometry, QgsLineString, QgsPoint, QgsVectorLayer

from los_tools.constants.field_names import FieldNames

Expand Down Expand Up @@ -55,10 +54,7 @@ def replace_minus_one_with_value(self, value: float) -> None:
self.data[0][self.INDEX_SAMPLING_DISTANCE] = sampling_distance_limit

if 1 < len(self.data):
while (
self.data[0][self.INDEX_DISTANCE]
< self.data[-1][self.INDEX_DISTANCE]
):
while self.data[0][self.INDEX_DISTANCE] < self.data[-1][self.INDEX_DISTANCE]:
self.data.remove(self.data[-1])

self.sort_data()
Expand Down Expand Up @@ -113,17 +109,12 @@ def next_distance(self, current_distance: float) -> float:
row: List[float]

for row in self.data:
if (
row[self.INDEX_DISTANCE]
< current_distance + row[self.INDEX_SAMPLING_DISTANCE]
):
if row[self.INDEX_DISTANCE] < current_distance + row[self.INDEX_SAMPLING_DISTANCE]:
value_to_add = row[self.INDEX_SAMPLING_DISTANCE]

return current_distance + value_to_add

def build_line(
self, origin_point: QgsPoint, direction_point: QgsPoint
) -> QgsLineString:
def build_line(self, origin_point: QgsPoint, direction_point: QgsPoint) -> QgsLineString:
line: Union[QgsLineString, QgsGeometry]

lines = []
Expand All @@ -144,13 +135,9 @@ def build_line(
else:
if i + 1 < len(self):
this_line: QgsLineString = lines[-1].clone()
this_line.extend(
0, self.get_row_distance(i + 1) - self.get_row_distance(i)
)
this_line.extend(0, self.get_row_distance(i + 1) - self.get_row_distance(i))

line_res = self.densified_line(
lines[-1].endPoint(), this_line.endPoint(), i
)
line_res = self.densified_line(lines[-1].endPoint(), this_line.endPoint(), i)

lines.append(line_res)

Expand All @@ -161,15 +148,9 @@ def build_line(

return result_line

def densified_line(
self, start_point: QgsPoint, end_point: QgsPoint, sampling_row_index: int
) -> QgsLineString:
def densified_line(self, start_point: QgsPoint, end_point: QgsPoint, sampling_row_index: int) -> QgsLineString:
line = QgsGeometry.fromPolyline([start_point, end_point])

line = line.densifyByDistance(
distance=np.nextafter(
self.get_row_sampling_distance(sampling_row_index), np.Inf
)
)
line = line.densifyByDistance(distance=np.nextafter(self.get_row_sampling_distance(sampling_row_index), np.Inf))

return QgsLineString([x for x in line.vertices()])
Loading

0 comments on commit 254e4e9

Please sign in to comment.