Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:return:
"""
if dsi_or_uuid is None:
return None
elif isinstance(dsi_or_uuid, UUID):
uuid = dsi_or_uuid
elif isinstance(dsi_or_uuid, str):
uuid = UUID(dsi_or_uuid)
else:
uuid = dsi_or_uuid[Info.UUID]
# prod = self._product_with_uuid(dsi_or_uuid)
# TODO: this causes a locking exception when run in a secondary thread.
# Keeping background operations lightweight makes sense however, so just review this
# prod.touch()
with self._inventory as s:
content = s.query(Content).filter(
(Product.uuid_str == str(uuid)) & (Content.product_id == Product.id)).order_by(Content.lod.desc()).all()
content = [x for x in content if x.info.get(Info.KIND, Kind.IMAGE) == kind]
if len(content) != 1:
LOG.warning("More than one matching Content object for '{}'".format(dsi_or_uuid))
if not len(content) or not content[0]:
raise AssertionError('no content in workspace for {}, must re-import'.format(uuid))
content = content[0]
# content.touch()
# self._S.commit() # flush any pending updates to workspace db file
# FIXME: find the content for the requested LOD, then return its ActiveContent - or attach one
# for now, just work with assumption of one product one content
active_content = self._cached_arrays_for_content(content)
return active_content.data
"""
if dsi_or_uuid is None:
return None
elif isinstance(dsi_or_uuid, UUID):
uuid = dsi_or_uuid
elif isinstance(dsi_or_uuid, str):
uuid = UUID(dsi_or_uuid)
else:
uuid = dsi_or_uuid[Info.UUID]
# prod = self._product_with_uuid(dsi_or_uuid)
# TODO: this causes a locking exception when run in a secondary thread.
# Keeping background operations lightweight makes sense however, so just review this
# prod.touch()
with self._inventory as s:
content = s.query(Content).filter(
(Product.uuid_str == str(uuid)) & (Content.product_id == Product.id)).order_by(Content.lod.desc()).all()
content = [x for x in content if x.info.get(Info.KIND, Kind.IMAGE) == kind]
if len(content) != 1:
LOG.warning("More than one matching Content object for '{}'".format(dsi_or_uuid))
if not len(content) or not content[0]:
raise AssertionError('no content in workspace for {}, must re-import'.format(uuid))
content = content[0]
# content.touch()
# self._S.commit() # flush any pending updates to workspace db file
# FIXME: find the content for the requested LOD, then return its ActiveContent - or attach one
# for now, just work with assumption of one product one content
active_content = self._cached_arrays_for_content(content)
return active_content.data
def _product_overview_content(self, session, prod: Product = None, uuid: UUID = None,
kind: Kind = Kind.IMAGE) -> Content:
if prod is None and uuid is not None:
# Get Product object
try:
prod = session.query(Product).filter(Product.uuid_str == str(uuid)).one()
except NoResultFound:
LOG.error("No product with UUID {} found".format(uuid))
return None
contents = session.query(Content).filter(Content.product_id == prod.id).order_by(Content.lod).all()
contents = [c for c in contents if c.info.get(Info.KIND, Kind.IMAGE) == kind]
return None if 0 == len(contents) else contents[0]
def product_state(self, uuid: UUID) -> Flags:
state = Flags(self._state[uuid])
# add any derived information
if uuid in self._available:
state.add(State.ATTACHED)
with self._inventory as s:
ncontent = s.query(Content).filter_by(uuid=uuid).count()
if ncontent > 0:
state.add(State.CACHED)
return state
with open(ws_path, 'wb+') as fp:
mm = np.memmap(fp, dtype=data.dtype, shape=data.shape, mode='w+')
mm[:] = data[:]
parms.update(dict(
lod=Content.LOD_OVERVIEW,
path=ws_filename,
dtype=str(data.dtype),
proj4=info[Info.PROJ],
resolution=min(info[Info.CELL_WIDTH], info[Info.CELL_HEIGHT])
))
rcls = dict(zip(('rows', 'cols', 'levels'), data.shape))
parms.update(rcls)
LOG.debug("about to create Content with this: {}".format(repr(parms)))
C = Content.from_info(parms, only_fields=True)
P.content.append(C)
# FUTURE: do we identify a Resource to go with this? Probably not
# transaction with the metadatabase to add the product and content
with self._inventory as S:
S.add(P)
S.add(C)
# FIXME: Do I have to flush the session so the Product gets added for sure?
# activate the content we just loaded into the workspace
overview_data = self._overview_content_for_uuid(uuid)
# prod = self._product_with_uuid(S, uuid)
return uuid, self.get_info(uuid), overview_data
data_path = os.path.join(self._cwd, data_filename)
try:
contour_data = self._compute_contours(
img_data, prod.info[Info.VALID_RANGE][0],
prod.info[Info.VALID_RANGE][1], levels)
except ValueError:
LOG.warning("Could not compute contour levels for '{}'".format(prod.uuid))
LOG.debug("Contour error: ", exc_info=True)
continue
contour_data[:, 0] *= cell_width
contour_data[:, 0] += origin_x
contour_data[:, 1] *= cell_height
contour_data[:, 1] += origin_y
contour_data.tofile(data_path)
c = Content(
lod=0,
resolution=int(min(abs(cell_width), abs(cell_height))),
atime=now,
mtime=now,
# info about the data array memmap
path=data_filename,
rows=contour_data.shape[0], # number of vertices
cols=contour_data.shape[1], # col (x), row (y), "connect", num_points_for_level
proj4=proj4,
# levels = 0,
dtype='float32',
cell_width=cell_width,
cell_height=cell_height,
origin_x=origin_x,
# load at an increment that matches the file's tile size if possible
IDEAL_INCREMENT = 512.0
increment = min(blockh * int(np.ceil(IDEAL_INCREMENT / blockh)), 2048)
# how many coverage states are we traversing during the load?
# for now let's go simple and have it be just image rows
# coverage_rows = int((rows + increment - 1) / increment) if we had an even increment but it's not guaranteed
cov_data = np.memmap(coverage_path, dtype=np.int8, shape=(rows,), mode='w+')
cov_data[:] = 0 # should not be needed except maybe in Windows?
# LOG.debug("keys in geotiff product: {}".format(repr(list(prod.info.keys()))))
LOG.debug(
"cell size in geotiff product: {} x {}".format(prod.info[Info.CELL_HEIGHT], prod.info[Info.CELL_WIDTH]))
# create and commit a Content entry pointing to where the content is in the workspace, even if coverage is empty
c = Content(
lod=0,
resolution=int(min(abs(info[Info.CELL_WIDTH]), abs(info[Info.CELL_HEIGHT]))),
atime=now,
mtime=now,
# info about the data array memmap
path=data_filename,
rows=rows,
cols=cols,
levels=0,
dtype='float32',
cell_width=info[Info.CELL_WIDTH],
cell_height=info[Info.CELL_HEIGHT],
origin_x=info[Info.ORIGIN_X],
origin_y=info[Info.ORIGIN_Y],
def _all_tables_present(self):
from sqlalchemy.engine.reflection import Inspector
inspector = Inspector.from_engine(self.engine)
all_tables = set(inspector.get_table_names())
zult = True
for table_name in (Resource.__tablename__, Product.__tablename__,
ProductKeyValue.__tablename__, SymbolKeyValue.__tablename__,
Content.__tablename__, ContentKeyValue.__tablename__, PRODUCTS_FROM_RESOURCES_TABLE_NAME):
present = table_name in all_tables
LOG.debug("table {} {} present in database".format(table_name, "is" if present else "is not"))
zult = False if not present else zult
return zult
content = relationship("Content", backref=backref("product"), cascade="all", order_by=lambda: Content.lod)
def _product_native_content(self, session, prod: Product = None, uuid: UUID = None,
kind: Kind = Kind.IMAGE) -> Content:
# NOTE: This assumes the last Content object is the best resolution
# but it is untested
if prod is None and uuid is not None:
# Get Product object
try:
prod = session.query(Product).filter(Product.uuid_str == str(uuid)).one()
except NoResultFound:
LOG.error("No product with UUID {} found".format(uuid))
return None
contents = session.query(Content).filter(Content.product_id == prod.id).order_by(Content.lod.desc()).all()
contents = [c for c in contents if c.info.get(Info.KIND, Kind.IMAGE) == kind]
return None if 0 == len(contents) else contents[-1]