File: dialogs.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (252 lines) | stat: -rw-r--r-- 8,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
@package datacatalog.dialogs

@brief Dialogs used in data catalog

Classes:
 - dialogs::CatalogReprojectionDialog

(C) 2017 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Anna Petrasova <kratochanna gmail.com>
"""

import wx
from gui_core.widgets import FloatValidator, IntegerValidator
from core.giface import Notification
from core.gcmd import RunCommand
from gui_core.wrap import Button, StaticText, TextCtrl

from grass.script import parse_key_val, region_env


class CatalogReprojectionDialog(wx.Dialog):
    def __init__(
        self,
        parent,
        giface,
        inputGisdbase,
        inputLocation,
        inputMapset,
        inputLayer,
        inputEnv,
        outputGisdbase,
        outputLocation,
        outputMapset,
        outputLayer,
        etype,
        outputEnv,
        callback,
        id=wx.ID_ANY,
        title=_("Reprojection"),
        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
    ):
        self.parent = parent
        self._giface = giface

        wx.Dialog.__init__(
            self, parent, id, title, style=style, name="ReprojectionDialog"
        )

        self.panel = wx.Panel(parent=self)
        self.iGisdbase = inputGisdbase
        self.iLocation = inputLocation
        self.iMapset = inputMapset
        self.iLayer = inputLayer
        self.iEnv = inputEnv
        self.oGisdbase = outputGisdbase
        self.oLocation = outputLocation
        self.oMapset = outputMapset
        self.oLayer = outputLayer
        self.etype = etype
        self.oEnv = outputEnv
        self.callback = callback

        self._widgets()
        self._doLayout()

        if self.etype == "raster":
            self._estimateResampling()
            self._estimateResolution()

    def _widgets(self):
        if self.etype == "raster":
            self.resolution = TextCtrl(self.panel, validator=FloatValidator())
            self.resampling = wx.Choice(
                self.panel,
                size=(200, -1),
                choices=[
                    "nearest",
                    "bilinear",
                    "bicubic",
                    "lanczos",
                    "bilinear_f",
                    "bicubic_f",
                    "lanczos_f",
                ],
            )
        else:
            self.vsplit = TextCtrl(self.panel, validator=IntegerValidator())
            self.vsplit.SetValue("10000")

        #
        # buttons
        #
        self.btn_close = Button(parent=self.panel, id=wx.ID_CLOSE)
        self.SetEscapeId(self.btn_close.GetId())

        # run
        self.btn_run = Button(parent=self.panel, id=wx.ID_OK, label=_("Reproject"))
        if self.etype == "raster":
            self.btn_run.SetToolTip(_("Reproject raster"))
        elif self.etype == "vector":
            self.btn_run.SetToolTip(_("Reproject vector"))
        self.btn_run.SetDefault()
        self.btn_run.Bind(wx.EVT_BUTTON, self.OnReproject)

    def _doLayout(self):
        """Do layout"""
        dialogSizer = wx.BoxSizer(wx.VERTICAL)
        optionsSizer = wx.GridBagSizer(5, 5)

        label = _(
            "Map layer <{ml}> needs to be reprojected.\n"
            "Please review and modify reprojection parameters:"
        ).format(ml=self.iLayer)
        dialogSizer.Add(
            StaticText(self.panel, label=label), flag=wx.ALL | wx.EXPAND, border=10
        )
        if self.etype == "raster":
            optionsSizer.Add(
                StaticText(self.panel, label=_("Estimated resolution:")),
                pos=(0, 0),
                flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL,
            )
            optionsSizer.Add(self.resolution, pos=(0, 1), flag=wx.EXPAND)
            optionsSizer.Add(
                StaticText(self.panel, label=_("Resampling method:")),
                pos=(1, 0),
                flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL,
            )
            optionsSizer.Add(self.resampling, pos=(1, 1), flag=wx.EXPAND)
        else:
            optionsSizer.Add(
                StaticText(self.panel, label=_("Maximum segment length:")),
                pos=(1, 0),
                flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL,
            )
            optionsSizer.Add(self.vsplit, pos=(1, 1), flag=wx.EXPAND)
        optionsSizer.AddGrowableCol(1)
        dialogSizer.Add(optionsSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=10)
        helptext = StaticText(
            self.panel,
            label="For more reprojection options,"
            " please see {module}".format(
                module="r.proj" if self.etype == "raster" else "v.proj"
            ),
        )
        dialogSizer.Add(helptext, proportion=0, flag=wx.ALL | wx.EXPAND, border=10)
        #
        # buttons
        #
        btnStdSizer = wx.StdDialogButtonSizer()
        btnStdSizer.AddButton(self.btn_run)
        btnStdSizer.AddButton(self.btn_close)
        btnStdSizer.Realize()
        dialogSizer.Add(btnStdSizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)

        self.panel.SetSizer(dialogSizer)
        dialogSizer.Fit(self.panel)

        self.Layout()
        self.SetSize(self.GetBestSize())

    def _estimateResolution(self):
        output = RunCommand(
            "r.proj",
            flags="g",
            quiet=False,
            read=True,
            input=self.iLayer,
            dbase=self.iGisdbase,
            project=self.iLocation,
            mapset=self.iMapset,
            env=self.oEnv,
        ).strip()
        params = parse_key_val(output, vsep=" ")
        output = RunCommand(
            "g.region",
            flags="ug",
            quiet=False,
            read=True,
            env=self.oEnv,
            parse=lambda x: parse_key_val(x, val_type=float),
            **params,
        )
        cell_ns = (output["n"] - output["s"]) / output["rows"]
        cell_ew = (output["e"] - output["w"]) / output["cols"]
        estimate = (cell_ew + cell_ns) / 2.0
        self.resolution.SetValue(str(estimate))
        self.params = params

    def _estimateResampling(self):
        output = RunCommand(
            "r.info",
            flags="g",
            quiet=False,
            read=True,
            map=self.iLayer,
            env=self.iEnv,
            parse=parse_key_val,
        )
        if output["datatype"] == "CELL":
            self.resampling.SetStringSelection("nearest")
        else:
            self.resampling.SetStringSelection("bilinear")

    def OnReproject(self, event):
        cmd = []
        if self.etype == "raster":
            cmd.append("r.proj")
            cmd.append("dbase=" + self.iGisdbase)
            cmd.append("project=" + self.iLocation)
            cmd.append("mapset=" + self.iMapset)
            cmd.append("input=" + self.iLayer)
            cmd.append("output=" + self.oLayer)
            cmd.append("method=" + self.resampling.GetStringSelection())

            self.oEnv["GRASS_REGION"] = region_env(
                n=self.params["n"],
                s=self.params["s"],
                e=self.params["e"],
                w=self.params["w"],
                flags="a",
                res=float(self.resolution.GetValue()),
                env=self.oEnv,
            )
        else:
            cmd.append("v.proj")
            cmd.append("dbase=" + self.iGisdbase)
            cmd.append("project=" + self.iLocation)
            cmd.append("mapset=" + self.iMapset)
            cmd.append("input=" + self.iLayer)
            cmd.append("output=" + self.oLayer)
            cmd.append("smax=" + self.vsplit.GetValue())

        self._giface.RunCmd(
            cmd,
            env=self.oEnv,
            compReg=False,
            addLayer=False,
            onDone=self._onDone,
            userData=None,
            notification=Notification.MAKE_VISIBLE,
        )

        event.Skip()

    def _onDone(self, event):
        self.callback()