Skip to content

Commit

Permalink
Merge pull request #195 from PUTvision/map_units_investigation_attempt_2
Browse files Browse the repository at this point in the history
Map units investigation attempt 2
  • Loading branch information
przemyslaw-aszkowski authored Dec 10, 2024
2 parents e4ff798 + 1830c1e commit f450c63
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/deepness/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name=Deepness: Deep Neural Remote Sensing
qgisMinimumVersion=3.22
description=Inference of deep neural network models (ONNX) for segmentation, detection and regression
version=0.6.4
version=0.6.5
author=PUT Vision
[email protected]

Expand Down
17 changes: 11 additions & 6 deletions src/deepness/processing/extent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
This file contains utilities related to Extent processing
"""

from qgis.core import QgsCoordinateTransform
from qgis.core import QgsRasterLayer
from qgis.core import QgsRectangle
from qgis.core import QgsVectorLayer
import logging

from qgis.core import QgsCoordinateTransform, QgsRasterLayer, QgsRectangle, QgsVectorLayer
from qgis.gui import QgsMapCanvas

from deepness.common.errors import OperationFailedException
from deepness.common.processing_parameters.map_processing_parameters import ProcessedAreaType, \
MapProcessingParameters
from deepness.common.processing_parameters.map_processing_parameters import MapProcessingParameters, ProcessedAreaType
from deepness.processing.processing_utils import BoundingBox, convert_meters_to_rlayer_units


Expand All @@ -23,7 +21,14 @@ def round_extent_to_rlayer_grid(extent: QgsRectangle, rlayer: QgsRasterLayer) ->
:param extent: Extent to round, needs to be in rlayer CRS units
:param rlayer: layer detemining the grid
"""
# For some ortophotos grid spacing is close to (1.0, 1.0), while it shouldn't be.
# Seems like it is some bug or special "feature" that I do not understand.
# In that case, just return the extent as it is
grid_spacing = rlayer.rasterUnitsPerPixelX(), rlayer.rasterUnitsPerPixelY()
if abs(grid_spacing[0] - 1.0) < 0.0001 and abs(grid_spacing[1] - 1.0) < 0.0001:
logging.warning('Grid spacing is close to 1.0, which is suspicious, returning extent as it is. It shouldn not be a problem for most cases.')
return extent

grid_start = rlayer.extent().xMinimum(), rlayer.extent().yMinimum()

x_min = grid_start[0] + int((extent.xMinimum() - grid_start[0]) / grid_spacing[0]) * grid_spacing[0]
Expand Down
4 changes: 2 additions & 2 deletions src/deepness/processing/map_processor/map_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def limit_extended_extent_image_to_base_extent_with_mask(self, full_img):
"""
# TODO look for some inplace operation to save memory
# cv2.copyTo(src=full_img, mask=area_mask_img, dst=full_img) # this doesn't work due to implementation details

for i in range(full_img.shape[0]):
full_img[i] = cv2.copyTo(src=full_img[i], mask=self.area_mask_img)

Expand Down Expand Up @@ -211,7 +211,7 @@ def tiles_generator(self) -> Tuple[np.ndarray, TileParams]:

if not tile_params.is_tile_within_mask(self.area_mask_img):
continue # tile outside of mask - to be skipped

tile_img = processing_utils.get_tile_image(
rlayer=self.rlayer, extent=tile_params.extent, params=self.params)

Expand Down
28 changes: 17 additions & 11 deletions src/deepness/processing/processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
cv2 = LazyPackageLoader('cv2')


def convert_meters_to_rlayer_units(rlayer, distance_m) -> float:
""" How many map units are there in one meter """
# TODO - potentially implement conversions from other units
def convert_meters_to_rlayer_units(rlayer: QgsRasterLayer, distance_m: float) -> float:
""" How many map units are there in one meter.
:param rlayer: raster layer for which we want to convert meters to its units
:param distance_m: distance in meters
"""
if rlayer.crs().mapUnits() != QgsUnitTypes.DistanceUnit.DistanceMeters:
# TODO - add support for more unit types
raise Exception("Unsupported layer units")
assert distance_m != 0
return distance_m
logging.warning(f"Map units are not meters but '{rlayer.crs().mapUnits()}'. It should be fine for most cases, but be aware.")

# Now we support all units, but we need to convert them to meters
# to have a consistent unit for the distance
scaling_factor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceMeters, rlayer.crs().mapUnits())
distance = distance_m * scaling_factor
assert distance != 0
return distance


def get_numpy_data_type_for_qgis_type(data_type_qgis: Qgis.DataType):
Expand Down Expand Up @@ -290,7 +296,7 @@ def get_xyxy(self) -> Tuple[int, int, int, int]:
self.x_max,
self.y_max
]

def get_xyxy_rot(self) -> Tuple[int, int, int, int, float]:
""" Returns the bounding box as a tuple (x_min, y_min, x_max, y_max, rotation)
Expand Down Expand Up @@ -321,7 +327,7 @@ def get_xywh(self) -> Tuple[int, int, int, int]:
self.x_max - self.x_min,
self.y_max - self.y_min
]

def get_center(self) -> Tuple[int, int]:
""" Returns the center of the bounding box as a tuple (x, y)
Expand Down Expand Up @@ -434,7 +440,7 @@ def get_4_corners(self) -> List[Tuple]:
else:
x_center = (self.x_min + self.x_max) / 2
y_center = (self.y_min + self.y_max) / 2

corners = np.array([
[self.x_min, self.y_min],
[self.x_min, self.y_max],
Expand Down Expand Up @@ -497,7 +503,7 @@ def create_area_mask_image(vlayer_mask,

if vlayer_mask is None:
return None

if files_handler is None:
img = np.zeros(shape=image_shape_yx, dtype=np.uint8)
else:
Expand Down

0 comments on commit f450c63

Please sign in to comment.