Changes between Initial Version and Version 3 of Ticket #36058


Ignore:
Timestamp:
Jan 7, 2025, 3:41:16 AM (4 weeks ago)
Author:
Arnaldo Govene
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #36058

    • Property Triage Stage UnreviewedAccepted
    • Property Summary Refactor SpatialRefSysMixin.srs Property for Improved Efficiency and ClarityRefactoring SpatialRefSysMixin.srs for efficiency and better error handling
    • Property Owner set to Arnaldo Govene
  • Ticket #36058 – Description

    initial v3  
    22
    33**1. Recursive Calls:**
    4 The property uses return self.srs within the try blocks. This introduces recursion, which could lead to stack overflow in edge cases or unexpected behavior. The recursive call to self.srs results in the function calling itself indefinitely, leading to a potential infinite loop.
     4The property uses `return self.srs` within the try blocks. This introduces recursion, which could lead to stack overflow in edge cases or unexpected behavior.
    55
    66Current Code:
     
    2020Current Code:
    2121
    22 
    2322{{{
    2423try:
     
    3736**Proposal:**
    3837
    39 **1. Remove Recursive Calls:**
    40 Replace return self.srs with return `self._srs.clone()` fo avoid recursion and improve clarity.
     38**1. Improve Error Reporting:**
     39Capture and differentiate between errors raised during the initialization from WKT and PROJ.4. Ensure that both error messages are included in the final exception for better clarity and debugging.
     40
     41**2. Raplace Manual Caching with `@cached_propert` and Remove Recursive Calls:**
     42To further optimize the `srs` property, replace the manual caching mechanism with `@cached_property`. This simplifies the code while retaining the benefits of caching, which is particularly important given the computational cost of creating a gdal.SpatialReference object.
    4143
    4244**Refactored Code:**
    4345
    4446{{{
     47from django.utils.functional import cached_property
    4548
    46 if hasattr(self, "_srs"):
    47     return self._srs.clone()  # Return the clone of the cached object
     49class SpatialRefSysMixin:
     50
     51    @cached_property
     52    def srs(self):
     53        """
     54        Return a GDAL SpatialReference object.
     55        """
     56        try:
     57            return gdal.SpatialReference(self.wkt)
     58        except Exception as e:
     59            wkt_error = f"Error initializing from WKT: {str(e)}"
     60
     61        try:
     62            return gdal.SpatialReference(self.proj4text)
     63        except Exception as e:
     64            proj4_error = f"Error initializing from PROJ.4: {str(e)}"
     65
     66        raise Exception(f"Could not get OSR SpatialReference. WKT Error: {wkt_error} | PROJ.4 Error: {proj4_error}")
    4867}}}
    4968
    50 **2. Improve Error Reporting:**
    51 Capture and differentiate between errors raised during the initialization from WKT and PROJ.4. Ensure that both error messages are included in the final exception for better clarity and debugging.
    52 
    53 Refactored Code:
    54 
    55 {{{
    56 
    57 try:
    58     self._srs = gdal.SpatialReference(self.wkt)
    59     return self._srs.clone()
    60 except Exception as e:
    61     wkt_error = f"Error initializing from WKT: {str(e)}"
    62 
    63 try:
    64     self._srs = gdal.SpatialReference(self.proj4text)
    65     return self._srs.clone()
    66 except Exception as e:
    67     proj4_error = f"Error initializing from PROJ.4: {str(e)}"
    68 
    69 raise Exception(f"Could not get OSR SpatialReference. WKT Error: {wkt_error} | PROJ.4 Error: {proj4_error}")
    70 }}}
    71 
    72 **Expected Outcome:**
    73 - By removing recursion and directly returning a cloned object, we avoid unnecessary function calls and improve performance.
    74 - With differentiated error messages, it becomes easier to diagnose issues with the WKT or PROJ.4 initialization.
     69**Rationale for Using cached_property:**
     70- Caches the computationally expensive creation of `gdal.SpatialReference`, improving efficiency for repeated access.
     71- Removes manual caching logic, making the code cleaner and easier to maintain.
     72- Ideal for mostly static spatial reference data, avoiding unnecessary recomputation.
Back to Top