import math import struct import numpy as np try: from asgiref.local import Local as LocalContext except ImportError: from threading import local as LocalContext class GeometryIndexed: # binary format (everything little-endian): # 1 byte (uint8): variant id # 1 byte (uint8): resolution # 2 bytes (int16): origin x # 2 bytes (int16): origin y # 2 bytes (uint16): origin width # 2 bytes (uint16): origin height # (optional meta data, depending on subclass) # x bytes data, line after line. (cell size depends on subclass) dtype = np.uint16 variant_id = 0 def __init__(self, resolution=None, x=0, y=0, data=None, filename=None): if resolution is None: from django.conf import settings resolution = settings.CACHE_RESOLUTION self.resolution = resolution self.x = x self.y = y self.data = data if data is not None else self._get_empty_array() self.filename = filename @classmethod def _get_empty_array(cls): return np.empty((0, 0), dtype=cls.dtype) @classmethod def open(cls, filename): with open(filename, 'rb') as f: instance = cls.read(f) instance.filename = filename return instance @classmethod def read(cls, f): variant_id, resolution, x, y, width, height = struct.unpack('