Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@jit(nb_types.NamedUniTuple(int64, 4, Box)(
float64,
float64,
float64,
float64,
float64,
float64,
int64,
int64,
int64,
int64,
float64,
float64,
float64,
float64,
float64,
float64,
def visible_tiles(self, visible_geom, stride=Point(1, 1), extra_tiles_box=Box(0, 0, 0, 0)) -> Box:
"""
given a visible world geometry and sampling, return (sampling-state, [Box-of-tiles-to-draw])
sampling state is WELLSAMPLED/OVERSAMPLED/UNDERSAMPLED
returned Box should be iterated per standard start:stop style
tiles are specified as (iy,ix) integer pairs
extra_box value says how many extra tiles to include around each edge
"""
v = visible_geom
e = extra_tiles_box
return visible_tiles(
float(self.pixel_rez[0]), float(self.pixel_rez[1]),
float(self.tile_size[0]), float(self.tile_size[1]),
float(self.image_center[0]), float(self.image_center[1]),
int(self.image_shape[0]), int(self.image_shape[1]),
int(self.tile_shape[0]), int(self.tile_shape[1]),
float(v[0]), float(v[1]),
"""
super(TileCalculator, self).__init__()
self.name = name
self.image_shape = Point(np.int64(image_shape[0]), np.int64(image_shape[1]))
self.ul_origin = Point(*ul_origin)
self.pixel_rez = Resolution(np.float64(pixel_rez[0]), np.float64(pixel_rez[1]))
self.tile_shape = Point(np.int64(tile_shape[0]), np.int64(tile_shape[1]))
# in units of tiles:
self.texture_shape = texture_shape
# in units of data elements (float32):
self.texture_size = (self.texture_shape[0] * self.tile_shape[0], self.texture_shape[1] * self.tile_shape[1])
self.image_tiles_avail = (self.image_shape[0] / self.tile_shape[0], self.image_shape[1] / self.tile_shape[1])
self.wrap_lon = wrap_lon
self.proj = Proj(projection)
self.image_extents_box = e = Box(
bottom=np.float64(self.ul_origin[0] - self.image_shape[0] * self.pixel_rez.dy),
top=np.float64(self.ul_origin[0]),
left=np.float64(self.ul_origin[1]),
right=np.float64(self.ul_origin[1] + self.image_shape[1] * self.pixel_rez.dx),
)
# Array of points across the image space to be used as an estimate of image coverage
# Used when checking if the image is viewable on the current canvas's projection
self.image_mesh = np.meshgrid(np.linspace(e.left, e.right, IMAGE_MESH_SIZE),
np.linspace(e.bottom, e.top, IMAGE_MESH_SIZE))
self.image_mesh = np.column_stack((self.image_mesh[0].ravel(), self.image_mesh[1].ravel(),))
self.image_center = Point(self.ul_origin.y - self.image_shape[0] / 2. * self.pixel_rez.dy,
self.ul_origin.x + self.image_shape[1] / 2. * self.pixel_rez.dx)
# size of tile in image projection
self.tile_size = Resolution(self.pixel_rez.dy * self.tile_shape[0], self.pixel_rez.dx * self.tile_shape[1])
self.overview_stride = self.calc_overview_stride()
hh = ath / 2.
# center tile is half pixel off because we want center of the center
# tile to be at the center of the image
if tix0 < -hw + 0.5:
ntw += hw - 0.5 + tix0
tix0 = -hw + 0.5
if tiy0 < -hh + 0.5:
nth += hh - 0.5 + tiy0
tiy0 = -hh + 0.5
# add 0.5 to include the "end of the tile" since the r and b are exclusive
if tix0 + ntw > hw + 0.5:
ntw = hw + 0.5 - tix0
if tiy0 + nth > hh + 0.5:
nth = hh + 0.5 - tiy0
tilebox = Box(
bottom=np.int64(np.ceil(tiy0 + nth)),
left=np.int64(np.floor(tix0)),
top=np.int64(np.floor(tiy0)),
right=np.int64(np.ceil(tix0 + ntw)),
)
return tilebox
def visible_tiles(self, visible_geom, stride=Point(1, 1), extra_tiles_box=Box(0, 0, 0, 0)) -> Box:
"""
given a visible world geometry and sampling, return (sampling-state, [Box-of-tiles-to-draw])
sampling state is WELLSAMPLED/OVERSAMPLED/UNDERSAMPLED
returned Box should be iterated per standard start:stop style
tiles are specified as (iy,ix) integer pairs
extra_box value says how many extra tiles to include around each edge
"""
v = visible_geom
e = extra_tiles_box
return visible_tiles(
float(self.pixel_rez[0]), float(self.pixel_rez[1]),
float(self.tile_size[0]), float(self.tile_size[1]),
float(self.image_center[0]), float(self.image_center[1]),
int(self.image_shape[0]), int(self.image_shape[1]),
int(self.tile_shape[0]), int(self.tile_shape[1]),
float(v[0]), float(v[1]),
def assess(self):
"""Determine if a retile is needed.
Tell workspace we will be needed
"""
try:
view_box = self.get_view_box()
preferred_stride = self._get_stride(view_box)
tile_box = self.calc.visible_tiles(view_box, stride=preferred_stride, extra_tiles_box=Box(1, 1, 1, 1))
except ValueError:
LOG.error("Could not determine viewable image area for '{}'".format(self.name))
return False, self._stride, self._latest_tile_box
num_tiles = (tile_box.bottom - tile_box.top) * (tile_box.right - tile_box.left)
LOG.debug("Assessment: Prefer '%s' have '%s', was looking at %r, now looking at %r",
preferred_stride, self._stride, self._latest_tile_box, tile_box)
# If we zoomed out or we panned
need_retile = (num_tiles > 0) and (preferred_stride != self._stride or self._latest_tile_box != tile_box)
return need_retile, preferred_stride, tile_box
nb_types.NamedUniTuple(float64, 4, Box),
nb_types.Array(float64, 1, 'C'),
nb_types.Array(float64, 1, 'C'),
nb_types.UniTuple(int64, 2),
float64,
float64
), nopython=True, cache=True, nogil=True)
def calc_view_extents(image_extents_box: Box, canvas_point, image_point, canvas_size, dx, dy) -> Box:
left, right = _calc_extent_component(canvas_point[0], image_point[0], canvas_size[0], dx)
left = clip(left, image_extents_box.left, image_extents_box.right)
right = clip(right, image_extents_box.left, image_extents_box.right)
bot, top = _calc_extent_component(canvas_point[1], image_point[1], canvas_size[1], dy)
bot = clip(bot, image_extents_box.bottom, image_extents_box.top)
top = clip(top, image_extents_box.bottom, image_extents_box.top)
if (right - left) < CANVAS_EXTENTS_EPSILON or (top - bot) < CANVAS_EXTENTS_EPSILON:
float64
), nopython=True, cache=True, nogil=True)
def calc_view_extents(image_extents_box: Box, canvas_point, image_point, canvas_size, dx, dy) -> Box:
left, right = _calc_extent_component(canvas_point[0], image_point[0], canvas_size[0], dx)
left = clip(left, image_extents_box.left, image_extents_box.right)
right = clip(right, image_extents_box.left, image_extents_box.right)
bot, top = _calc_extent_component(canvas_point[1], image_point[1], canvas_size[1], dy)
bot = clip(bot, image_extents_box.bottom, image_extents_box.top)
top = clip(top, image_extents_box.bottom, image_extents_box.top)
if (right - left) < CANVAS_EXTENTS_EPSILON or (top - bot) < CANVAS_EXTENTS_EPSILON:
# they are viewing essentially nothing or the image isn't in view
raise ValueError("Image can't be currently viewed")
return Box(left=left, right=right, bottom=bot, top=top)
def visible_tiles(z_dy, z_dx,
tile_size_dy, tile_size_dx,
image_center_y, image_center_x,
image_shape_y, image_shape_x,
tile_shape_y, tile_shape_x,
v_bottom, v_left, v_top, v_right, v_dy, v_dx,
stride_y, stride_x,
x_bottom, x_left, x_top, x_right):
tile_size = Resolution(tile_size_dy * stride_y, tile_size_dx * stride_x)
# should be the upper-left corner of the tile centered on the center of the image
to = Point(image_center_y + tile_size.dy / 2.,
image_center_x - tile_size.dx / 2.) # tile origin
# number of data pixels between view edge and originpoint
pv = Box(
bottom=(v_bottom - to.y) / -(z_dy * stride_y),
top=(v_top - to.y) / -(z_dy * stride_y),
left=(v_left - to.x) / (z_dx * stride_x),
right=(v_right - to.x) / (z_dx * stride_x)
)
th = tile_shape_y
tw = tile_shape_x
# first tile we'll need is (tiy0, tix0)
# floor to make sure we get the upper-left of the theoretical tile
tiy0 = np.floor(pv.top / th)
tix0 = np.floor(pv.left / tw)
# number of tiles wide and high we'll absolutely need
# add 0.5 and ceil to make sure we include all possible tiles
# NOTE: output r and b values are exclusive, l and t are inclusive
nth = np.ceil((pv.bottom - tiy0 * th) / th + 0.5)
hh = ath / 2.
# center tile is half pixel off because we want center of the center
# tile to be at the center of the image
if tix0 < -hw + 0.5:
ntw += hw - 0.5 + tix0
tix0 = -hw + 0.5
if tiy0 < -hh + 0.5:
nth += hh - 0.5 + tiy0
tiy0 = -hh + 0.5
# add 0.5 to include the "end of the tile" since the r and b are exclusive
if tix0 + ntw > hw + 0.5:
ntw = hw + 0.5 - tix0
if tiy0 + nth > hh + 0.5:
nth = hh + 0.5 - tiy0
tilebox = Box(
bottom=np.int64(np.ceil(tiy0 + nth)),
left=np.int64(np.floor(tix0)),
top=np.int64(np.floor(tiy0)),
right=np.int64(np.ceil(tix0 + ntw)),
)
return tilebox