Created
November 10, 2015 14:23
-
-
Save snorfalorpagus/974c09e1ec1309b00987 to your computer and use it in GitHub Desktop.
Speedups geoprocessing with Fiona and LRU caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CollectionLRU(fiona.Collection): | |
"""Wrapper for Fiona Collection that provides LRU read caching | |
""" | |
def __init__(self, *args, **kwargs): | |
fiona.Collection.__init__(self, *args, **kwargs) | |
self.cache = LRU(200) | |
def __getitem__(self, key): | |
try: | |
feature = self.cache[key] | |
except KeyError: | |
feature = fiona.Collection.__getitem__(self, key) | |
self.cache[key] = feature | |
return feature | |
def fiona_cache_open(path, mode='r', driver=None, schema=None, crs=None, | |
encoding=None, layer=None, vfs=None, enabled_drivers=None, | |
crs_wkt=None): | |
"""Open a new Fiona collection with LRU read caching | |
""" | |
assert(mode == 'r') | |
path, vsi, archive = fiona.parse_paths(path, vfs) | |
collection = CollectionLRU(path, mode, driver=driver, | |
encoding=encoding, layer=layer, vsi=vsi, archive=archive, | |
enabled_drivers=enabled_drivers) | |
return collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment