I'm sorry for the poor description, the root of the problem is that I'm not sure of the terminology.
This is not a GIS problem, but an example of a general problem: In python 2.7, how do I "freeze" a variable that takes too much time to calculate, like I might do with frozen_list_copy = original_list[:]
?
Here's some code, simplified to focus on my problem. maketransformer()
takes too long to run, and it outputs transformer
, a GDAL (GIS stuff) coordinate system transformation object that works the same for every loop iteration. I only need to make the transformer
object once and then repeatedly call a method from it. Every time the loop references the transformer
variable assignment, it runs maketransformer()
to make a new one.
def maketransformer(file): #contains arbitrary stuff that takes too long
data = gdal.Open(file)
output_wkt = data.GetProjection()
srs_out = osr.SpatialReference()
srs_out.ImportFromWkt(output_wkt)
transformer = osr.CoordinateTransformation(srs_out)
return transformer
transformer = maketransformer('data/w001001.adf')
for tuple in shapefilepoints:
getelevation(transformer.Transform(tuple))
How might I run maketransformer()
just once, and then "freeze" the object it produces, "disconnecting" it from its assignment so I can just use that object without running through the whole 0.1 second creation process 15,000 times?
What I'm trying to do is similar to:
>>> x = ['one', 'two', 'three']
>>> y = x
>>> z = x[:] # I want to do this, but with a non-list object
>>> x[2] = 'purple'
>>> x
['one', 'two', 'purple']
>>> y
['one', 'two', 'purple']
>>> z # So that the following happens when I use z
['one', 'two', 'three']
In short, what is the word that I google to describe this! Many thanks, apologies for the uselessly specific beginner question.
Edit: I found a helpful tool along the way in discovering what the holdup was:
>>>import profile
profile.run("maketransformer('data/w001001.adf')")
Profile broke down the function into its parts, returning the number and time of calls for everything it did, revealing that it's running in under 0.001 seconds. The problem lies elsewhere. Thanks, everyone, for teaching me all this helpful stuff.