Skip to content

Instantly share code, notes, and snippets.

@snorfalorpagus
Created November 10, 2015 14:23
Show Gist options
  • Save snorfalorpagus/974c09e1ec1309b00987 to your computer and use it in GitHub Desktop.
Save snorfalorpagus/974c09e1ec1309b00987 to your computer and use it in GitHub Desktop.
Speedups geoprocessing with Fiona and LRU caching
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