From a32483ba84afcd3b2af92d75556a980484fdd076 Mon Sep 17 00:00:00 2001 From: Peter Robinett Date: Mon, 1 May 2017 00:23:44 -0700 Subject: [PATCH] Memoize coordinate reference system creation --- src/MilkMachine/MMExport.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/MilkMachine/MMExport.py b/src/MilkMachine/MMExport.py index 369a9b1..ac6bab1 100644 --- a/src/MilkMachine/MMExport.py +++ b/src/MilkMachine/MMExport.py @@ -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. @@ -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. @@ -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