Browse Source

wxGUI/datacatalog: fix setting output vector/raster format (#1596)

Tomas Zigo 3 years ago
parent
commit
aed181097b
3 changed files with 71 additions and 25 deletions
  1. 7 7
      gui/wxpython/core/utils.py
  2. 50 15
      gui/wxpython/gui_core/gselect.py
  3. 14 3
      gui/wxpython/modules/import_export.py

+ 7 - 7
gui/wxpython/core/utils.py

@@ -19,6 +19,7 @@ import glob
 import shlex
 import re
 import inspect
+import operator
 import six
 
 from grass.script import core as grass
@@ -649,7 +650,7 @@ def _getOGRFormats():
 
 def _parseFormats(output, writableOnly=False):
     """Parse r.in.gdal/v.in.ogr -f output"""
-    formats = {"file": list(), "database": list(), "protocol": list()}
+    formats = {"file": {}, "database": {}, "protocol": {}}
 
     if not output:
         return formats
@@ -660,7 +661,6 @@ def _parseFormats(output, writableOnly=False):
 
     for line in output.splitlines():
         key, name = map(lambda x: x.strip(), line.strip().split(":", 1))
-
         if writableOnly and not patt.search(key):
             continue
 
@@ -678,7 +678,7 @@ def _parseFormats(output, writableOnly=False):
             "MSSQLSpatial",
             "FileGDB",
         ):
-            formats["database"].append(name)
+            formats["database"][key.split(" ")[0]] = name
         elif name in (
             "GeoJSON",
             "OGC Web Coverage Service",
@@ -687,12 +687,12 @@ def _parseFormats(output, writableOnly=False):
             "GeoRSS",
             "HTTP Fetching Wrapper",
         ):
-            formats["protocol"].append(name)
+            formats["protocol"][key.split(" ")[0]] = name
         else:
-            formats["file"].append(name)
+            formats["file"][key.split(" ")[0]] = name
 
-    for items in six.itervalues(formats):
-        items.sort()
+    for k, v in formats.items():
+        formats[k] = dict(sorted(v.items(), key=operator.itemgetter(1)))
 
     return formats
 

+ 50 - 15
gui/wxpython/gui_core/gselect.py

@@ -1412,7 +1412,7 @@ class FormatSelect(wx.Choice):
             ftype = "gdal"
 
         formats = list()
-        for f in GetFormats()[ftype][srcType].values():
+        for f in GetFormats()[ftype][srcType].items():
             formats += f
         self.SetItems(formats)
 
@@ -1594,8 +1594,8 @@ class GdalSelect(wx.Panel):
         self.dirWidgets["browse"] = browse
         formatSelect = wx.Choice(parent=self.dirPanel)
         self.dirWidgets["format"] = formatSelect
-        fileFormats = GetFormats(writableOnly=dest)[fType]["file"]
-        formatSelect.SetItems(sorted(list(fileFormats)))
+        self.fileFormats = GetFormats(writableOnly=dest)[fType]["file"]
+        formatSelect.SetItems(sorted(list(self.fileFormats.values())))
         formatSelect.Bind(
             wx.EVT_CHOICE,
             lambda evt: self.SetExtension(
@@ -1612,19 +1612,20 @@ class GdalSelect(wx.Panel):
         self.dirWidgets["options"] = TextCtrl(parent=self.dirPanel)
         if self.ogr:
             shapefile = "ESRI Shapefile"
-            if shapefile in fileFormats:
+            if shapefile in self.fileFormats:
                 formatSelect.SetStringSelection(shapefile)
                 self.SetExtension(shapefile)
         else:
             tiff = "GeoTIFF"
-            if tiff in fileFormats:
+            if tiff in self.fileFormats:
                 formatSelect.SetStringSelection(tiff)
                 self.SetExtension(tiff)
 
         # database
         self.dbPanel = wx.Panel(parent=self)
+
         self.dbFormats = GetFormats(writableOnly=dest)[fType]["database"]
-        dbChoice = wx.Choice(parent=self.dbPanel, choices=self.dbFormats)
+        dbChoice = wx.Choice(parent=self.dbPanel, choices=list(self.dbFormats.values()))
         dbChoice.Bind(
             wx.EVT_CHOICE,
             lambda evt: self.SetDatabase(db=dbChoice.GetStringSelection()),
@@ -1681,8 +1682,10 @@ class GdalSelect(wx.Panel):
 
         # protocol
         self.protocolPanel = wx.Panel(parent=self)
-        protocolFormats = GetFormats(writableOnly=self.dest)[fType]["protocol"]
-        protocolChoice = wx.Choice(parent=self.protocolPanel, choices=protocolFormats)
+        self.protocolFormats = GetFormats(writableOnly=self.dest)[fType]["protocol"]
+        protocolChoice = wx.Choice(
+            parent=self.protocolPanel, choices=list(self.protocolFormats.values())
+        )
         self.protocolWidgets["format"] = protocolChoice
 
         self.protocolWidgets["text"] = TextCtrl(parent=self.protocolPanel)
@@ -1705,7 +1708,7 @@ class GdalSelect(wx.Panel):
             )
             if current["format"] == "native":
                 sourceType = "native"
-            elif current["format"] in GetFormats()["ogr"]["database"]:
+            elif current["format"] in GetFormats()["ogr"]["database"].values():
                 sourceType = "db"
             else:
                 sourceType = "dir"
@@ -1971,6 +1974,18 @@ class GdalSelect(wx.Panel):
 
         return formatToExt.get(name, "")
 
+    def _getFormatAbbreviation(self, formats, formatName):
+        """Get format abbreviation
+
+        :param dict formats: {formatAbbreviation: formatLongName}
+        :param str formatName: long format name
+
+        return str: return format abbreviation
+        """
+        for key, value in formats.items():
+            if formatName == value:
+                return key
+
     def SetSourceType(self, sourceType):
         """Set source type (db, file, dir, ...).
         Does not switch radioboxes."""
@@ -1984,9 +1999,9 @@ class GdalSelect(wx.Panel):
         self.changingSizer.Layout()
 
         if sourceType == "db":
-            self.dbWidgets["format"].SetItems(self.dbFormats)
+            self.dbWidgets["format"].SetItems(list(self.dbFormats.values()))
             if self.dbFormats:
-                if "PostgreSQL" in self.dbFormats:
+                if "PostgreSQL" in self.dbFormats.values():
                     self.dbWidgets["format"].SetStringSelection("PostgreSQL")
                 else:
                     self.dbWidgets["format"].SetSelection(0)
@@ -2274,14 +2289,34 @@ class GdalSelect(wx.Panel):
         """Get source type"""
         return self._sourceType
 
-    def GetFormat(self):
+    def GetFormat(self, getFormatAbbreviation=False):
         """Get format as string"""
-        if self._sourceType == "dir":
+
+        def _getFormat(getFormatAbbreviation, format_group):
+            """Get format long name or format abbreviation
+
+            :param bool getFormatAbbreviation: get format abbreviation
+            :param dict format_group: formats dict {formatAbbreviation: formatLongName}
+            for 'file', 'protocol', 'database' group
+
+            return str: long format name or format abbreviation
+            """
             format = self.dirWidgets["format"].GetStringSelection()
+            if getFormatAbbreviation:
+                return self._getFormatAbbreviation(
+                    formats=self.fileFormats,
+                    formatName=format,
+                )
+            return format
+
+        if self._sourceType == "dir":
+            format = _getFormat(getFormatAbbreviation, format_group=self.fileFormats)
         elif self._sourceType == "pro":
-            format = self.protocolWidgets["format"].GetStringSelection()
+            format = _getFormat(
+                getFormatAbbreviation, format_group=self.protocolFormats
+            )
         elif self._sourceType == "db":
-            format = self.dbWidgets["format"].GetStringSelection()
+            format = _getFormat(getFormatAbbreviation, format_group=self.dbFormats)
         else:
             format = ""
 

+ 14 - 3
gui/wxpython/modules/import_export.py

@@ -776,14 +776,25 @@ class GdalOutputDialog(wx.Dialog):
             RunCommand("v.external.out", parent=self, flags="r")
         else:
             dsn = self.dsnInput.GetDsn()
-            frmt = self.dsnInput.GetFormat()
+            frmt = self.dsnInput.GetFormat(getFormatAbbreviation=True)
+            extension = self.dsnInput.GetFormatExt()
             options = self.dsnInput.GetOptions()
             if not dsn:
                 GMessage(_("No data source selected."), parent=self)
                 return
-
+            if self.ogr:
+                cmd, params = "v.external.out", {"output": dsn}
+            else:
+                cmd, params = (
+                    "r.external.out",
+                    {"directory": dsn, "extension": extension},
+                )
             RunCommand(
-                "v.external.out", parent=self, output=dsn, format=frmt, options=options
+                cmd,
+                parent=self,
+                format=frmt,
+                options=options,
+                **params,
             )
         self.Close()