Skip to content

Memoize coordinate reference system creation #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/MilkMachine/MMExport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsPoint, QgsVectorFileWriter
from qgis.gui import QgsMessageBar

def memoize(function):
"""
Function decorator to memoize expensive function calls
"""
from functools import wraps

memo = {}

@wraps(function)
def wrapper(*args):
if args in memo:
return memo[args]
else:
rv = function(*args)
memo[args] = rv
return rv
return wrapper

def wgs84LatLonToUTMZone(latitude, longitude):
"""
Finds the UTM zone which contains the given WGS84 point.
Expand Down Expand Up @@ -49,6 +67,7 @@ def wgs84LatLonToUTMZone(latitude, longitude):

return zone, latBand

@memoize
def makeCoordinateReferenceSystem(latitude, utmZone):
"""
Creates a coordinate reference system, for instance for converting to this system.
Expand All @@ -66,10 +85,14 @@ def makeCoordinateReferenceSystem(latitude, utmZone):
>>> makeCoordinateReferenceSystem(13.41250188, 21442) is None
True
"""
crs = QgsCoordinateReferenceSystem()
proj4String = "+proj=utm +ellps=WGS84 +datum=WGS84 +units=m +zone=%s" % utmZone
if latitude < 0:
proj4String += " +south"
return crsCreateFromProj4(proj4String)

@memoize
def crsCreateFromProj4(proj4String):
crs = QgsCoordinateReferenceSystem()
result = crs.createFromProj4(proj4String)
return crs if result and crs.isValid() else None

Expand Down