"""
Companion manager for Tiles and Image Tiles menus and actions.
Extracted from PyDraw.WolfMapViewer.
"""
from __future__ import annotations
import glob
import logging
from os.path import join
from typing import TYPE_CHECKING
import wx
from .PyTranslate import _
from .PyVertexvectors import vector, wolfvertex
if TYPE_CHECKING:
from .PyDraw import WolfMapViewer
[docs]
class TilesManager:
"""Manages the &Tiles and &Image tiles menus and their associated actions."""
def __init__(self, viewer: 'WolfMapViewer') -> None:
# ------------------------------------------------------------------
# Menu build
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# Action handlers
# ------------------------------------------------------------------
[docs]
def on_pick_image_tile(self, event: wx.Event) -> None:
v = self._viewer
if v.active_imagestiles is None:
logging.warning(_('No active image tile -- Please load data first'))
return
v.start_action('select active image tile', _('Select active image tile'))
[docs]
def on_pick_tile(self, event: wx.Event) -> None:
v = self._viewer
if v.active_tile is None:
logging.warning(_('No active tile -- Please load data first'))
return
v.start_action('select active tile', _('Select active tile'))
[docs]
def on_create_data_from_tiles_activevec(self, event: wx.Event) -> None:
v = self._viewer
if v.active_tile is None:
logging.warning(_('No active tile -- Please load data first'))
return
if v.active_vector is None:
logging.warning(_('No active vector -- Please activate a vector first'))
return
self._create_data_from_tiles_common()
[docs]
def on_create_data_from_tiles_tmpvec(self, event: wx.Event) -> None:
v = self._viewer
if v.active_tile is None:
logging.warning(_('No active tile -- Please load data first'))
return
v.start_action('create polygon - tiles', _('Extract data from tiles inside polygon'))
v.active_vector = vector()
v.active_vector.myname = 'crop_tiles'
v.active_vector.add_vertex(wolfvertex(0., 0.))
[docs]
def _create_data_from_tiles_common(self) -> None:
from .wolf_vrt import create_vrt, crop_vrt
v = self._viewer
dirdata = v.active_tile.linked_data_dir
glob_vrt = glob.glob(join(dirdata, '*.vrt'))
if len(glob_vrt) == 0:
file_vrt = r'tmp.vrt'
create_vrt(dirdata, fout=file_vrt)
glob_vrt = file_vrt
else:
glob_vrt = glob_vrt[0]
dlg = wx.FileDialog(
None,
_('Choose filename'),
wildcard='tif (*.tif)|*.tif',
defaultDir=dirdata,
defaultFile='{}_crop.tif'.format(v.active_vector.myname),
style=wx.FD_SAVE
)
ret = dlg.ShowModal()
if ret == wx.ID_CANCEL:
dlg.Destroy()
return
fout = dlg.GetPath()
dlg.Destroy()
bbox = v.active_vector.get_bounds_xx_yy()
crop_vrt(glob_vrt, bbox, fout=fout)
logging.info(_('File {} created').format(fout))
dlg = wx.MessageDialog(
v,
_('Do you want to load the created file ?'),
_('Load file'),
wx.YES_NO | wx.ICON_QUESTION
)
ret = dlg.ShowModal()
if ret == wx.ID_CANCEL:
dlg.Destroy()
return
elif ret == wx.ID_YES:
v.add_object('array', filename=fout, id=v.active_vector.myname)