浏览代码

wxGUI/wxpsmap: simplified method for coordinates conversion, changed parameter name (map is python built-in function)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@50836 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 13 年之前
父节点
当前提交
a17606be34
共有 4 个文件被更改,包括 58 次插入64 次删除
  1. 10 10
      gui/wxpython/psmap/dialogs.py
  2. 5 5
      gui/wxpython/psmap/frame.py
  3. 10 10
      gui/wxpython/psmap/instructions.py
  4. 33 39
      gui/wxpython/psmap/utils.py

+ 10 - 10
gui/wxpython/psmap/dialogs.py

@@ -91,7 +91,7 @@ class TCValidator(wx.PyValidator):
         if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
             event.Skip()
             return
-        if self.flag == 'DIGIT_ONLY' and chr(key) in string.digits + '.':
+        if self.flag == 'DIGIT_ONLY' and chr(key) in string.digits + '.-':
             event.Skip()
             return
 ##        if self.flag == 'SCALE' and chr(key) in string.digits + ':':
@@ -259,7 +259,7 @@ class PsmapDialog(wx.Dialog):
         northingLabel  = wx.StaticText(panel, id = wx.ID_ANY, label = "N:")
         panel.position['eCtrl'] = wx.TextCtrl(panel, id = wx.ID_ANY, value = "")
         panel.position['nCtrl'] = wx.TextCtrl(panel, id = wx.ID_ANY, value = "")
-        east, north = PaperMapCoordinates(map = self.instruction[self.mapId], x = dialogDict['where'][0], y = dialogDict['where'][1], paperToMap = True)
+        east, north = PaperMapCoordinates(mapInstr = self.instruction[self.mapId], x = dialogDict['where'][0], y = dialogDict['where'][1], paperToMap = True)
         panel.position['eCtrl'].SetValue(str(east))
         panel.position['nCtrl'].SetValue(str(north))
         
@@ -3687,7 +3687,7 @@ class TextDialog(PsmapDialog):
             map = self.instruction.FindInstructionByType('initMap')
         self.mapId = map.id
 
-        self.textDict['east'], self.textDict['north'] = PaperMapCoordinates(map = map, x = self.textDict['where'][0], y = self.textDict['where'][1], paperToMap = True)
+        self.textDict['east'], self.textDict['north'] = PaperMapCoordinates(mapInstr = map, x = self.textDict['where'][0], y = self.textDict['where'][1], paperToMap = True)
         
         notebook = wx.Notebook(parent = self, id = wx.ID_ANY, style = wx.BK_DEFAULT)     
         self.textPanel = self._textPanel(notebook)
@@ -4015,7 +4015,7 @@ class TextDialog(PsmapDialog):
             else:
                 self.textDict['north'] = self.textDict['north']
 
-            self.textDict['where'] = PaperMapCoordinates(map = self.instruction[self.mapId], x = float(self.textDict['east']),
+            self.textDict['where'] = PaperMapCoordinates(mapInstr = self.instruction[self.mapId], x = float(self.textDict['east']),
                                                             y = float(self.textDict['north']), paperToMap = False)
         #rotation
         if self.rotCtrl.GetValue():
@@ -4078,7 +4078,7 @@ class ImageDialog(PsmapDialog):
             map = self.instruction.FindInstructionByType('initMap')
         self.mapId = map.id
 
-        self.imageDict['east'], self.imageDict['north'] = PaperMapCoordinates(map = map, x = self.imageDict['where'][0], y = self.imageDict['where'][1], paperToMap = True)
+        self.imageDict['east'], self.imageDict['north'] = PaperMapCoordinates(mapInstr = map, x = self.imageDict['where'][0], y = self.imageDict['where'][1], paperToMap = True)
         
         notebook = wx.Notebook(parent = self, id = wx.ID_ANY, style = wx.BK_DEFAULT)
         self.imagePanelName = imagePanelName
@@ -4429,7 +4429,7 @@ class ImageDialog(PsmapDialog):
             else:
                 self.imageDict['north'] = self.imageDict['north']
 
-            x, y = PaperMapCoordinates(map = self.instruction[self.mapId], x = float(self.imageDict['east']),
+            x, y = PaperMapCoordinates(mapInstr = self.instruction[self.mapId], x = float(self.imageDict['east']),
                                        y = float(self.imageDict['north']), paperToMap = False)
 
         #rotation
@@ -4539,7 +4539,7 @@ class PointDialog(PsmapDialog):
             mapObj = self.instruction.FindInstructionByType('initMap')
         self.mapId = mapObj.id
         
-        self.pointDict['east'], self.pointDict['north'] = PaperMapCoordinates(map = mapObj, x = self.pointDict['where'][0], y = self.pointDict['where'][1], paperToMap = True)
+        self.pointDict['east'], self.pointDict['north'] = PaperMapCoordinates(mapInstr = mapObj, x = self.pointDict['where'][0], y = self.pointDict['where'][1], paperToMap = True)
         
         notebook = wx.Notebook(parent = self, id = wx.ID_ANY, style = wx.BK_DEFAULT)
         self.pointPanelName = pointPanelName
@@ -4757,7 +4757,7 @@ class PointDialog(PsmapDialog):
             else:
                 self.pointDict['north'] = self.pointDict['north']
 
-            x, y = PaperMapCoordinates(map = self.instruction[self.mapId], x = float(self.pointDict['east']),
+            x, y = PaperMapCoordinates(mapInstr = self.instruction[self.mapId], x = float(self.pointDict['east']),
                                        y = float(self.pointDict['north']), paperToMap = False)
 
         #rotation
@@ -4923,11 +4923,11 @@ class RectangleDialog(PsmapDialog):
         self.mapId = mapInstr.id
         point1 = self.rectDict['where'][0]
         point2 = self.rectDict['where'][1]
-        self.rectDict['east1'], self.rectDict['north1'] = PaperMapCoordinates(map = mapInstr,
+        self.rectDict['east1'], self.rectDict['north1'] = PaperMapCoordinates(mapInstr = mapInstr,
                                                                                 x = point1[0],
                                                                                 y = point1[1],
                                                                                 paperToMap = True)
-        self.rectDict['east2'], self.rectDict['north2'] = PaperMapCoordinates(map = mapInstr,
+        self.rectDict['east2'], self.rectDict['north2'] = PaperMapCoordinates(mapInstr = mapInstr,
                                                                                 x = point2[0],
                                                                                 y = point2[1],
                                                                                 paperToMap = True)

+ 5 - 5
gui/wxpython/psmap/frame.py

@@ -1210,21 +1210,21 @@ class PsMapBufferedWindow(wx.Window):
                 instr = self.instruction[item.id]
                 if itemType in ('line', 'rectangle'):
                     if itemType == 'line':
-                        e1, n1 = PaperMapCoordinates(map = self.instruction[mapId], x = instr['where'][0][0],
+                        e1, n1 = PaperMapCoordinates(mapInstr = self.instruction[mapId], x = instr['where'][0][0],
                                                      y = instr['where'][0][1], paperToMap = True)
-                        e2, n2 = PaperMapCoordinates(map = self.instruction[mapId], x = instr['where'][1][0],
+                        e2, n2 = PaperMapCoordinates(mapInstr = self.instruction[mapId], x = instr['where'][1][0],
                                                      y = instr['where'][1][1], paperToMap = True)
                     else: 
-                        e1, n1 = PaperMapCoordinates(map = self.instruction[mapId], x = instr['rect'].GetLeft(),
+                        e1, n1 = PaperMapCoordinates(mapInstr = self.instruction[mapId], x = instr['rect'].GetLeft(),
                                                      y = instr['rect'].GetTop(), paperToMap = True)
-                        e2, n2 = PaperMapCoordinates(map = self.instruction[mapId], x = instr['rect'].GetRight(),
+                        e2, n2 = PaperMapCoordinates(mapInstr = self.instruction[mapId], x = instr['rect'].GetRight(),
                                                      y = instr['rect'].GetBottom(), paperToMap = True)
                     instr['east1'] = e1
                     instr['north1'] = n1
                     instr['east2'] = e2
                     instr['north2'] = n2
                 else:
-                    e, n = PaperMapCoordinates(map = self.instruction[mapId], x = instr['where'][0],
+                    e, n = PaperMapCoordinates(mapInstr = self.instruction[mapId], x = instr['where'][0],
                                                y = instr['where'][1], paperToMap = True)
                     instr['east'], instr['north'] = e, n
                 

+ 10 - 10
gui/wxpython/psmap/instructions.py

@@ -869,7 +869,7 @@ class Text(InstructionObject):
             except(IndexError, ValueError):
                 GError(_("Failed to read instruction %s") % instruction)
                 return False
-        instr['where'] = PaperMapCoordinates(map = map, x = instr['east'], y = instr['north'], paperToMap = False)       
+        instr['where'] = PaperMapCoordinates(mapInstr = map, x = instr['east'], y = instr['north'], paperToMap = False)       
         self.instruction.update(instr)
 
         return True 
@@ -937,7 +937,7 @@ class Image(InstructionObject):
             instr['size'] = BBoxAfterRotation(instr['size'][0], instr['size'][1], instr['rotate'])
         self.instruction.update(instr)
         self.ChangeRefPoint(toCenter = False)
-        instr['where'] = PaperMapCoordinates(map = mapInstr, x = self.instruction['east'],
+        instr['where'] = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east'],
                                              y = self.instruction['north'], paperToMap = False)       
         w = self.unitConv.convert(value = instr['size'][0], fromUnit = 'point', toUnit = 'inch')
         h = self.unitConv.convert(value = instr['size'][1], fromUnit = 'point', toUnit = 'inch')
@@ -955,18 +955,18 @@ class Image(InstructionObject):
         mapId = mapInstr.id
         if toCenter:
             center = self.instruction['rect'].GetCentre()
-            ENCenter = PaperMapCoordinates(map = self.settings[mapId],
+            ENCenter = PaperMapCoordinates(mapInstr = self.settings[mapId],
                                            x = center[0], y = center[1], paperToMap = True)
                                            
             self.instruction['east'], self.instruction['north'] = ENCenter
         else:
-            x, y = PaperMapCoordinates(map = self.settings[mapId], x = self.instruction['east'],
+            x, y = PaperMapCoordinates(mapInstr = self.settings[mapId], x = self.instruction['east'],
                                        y = self.instruction['north'], paperToMap = False)
             w = self.unitConv.convert(value = self.instruction['size'][0], fromUnit = 'point', toUnit = 'inch')
             h = self.unitConv.convert(value = self.instruction['size'][1], fromUnit = 'point', toUnit = 'inch')
             x -= w * self.instruction['scale'] / 2
             y -= h * self.instruction['scale'] / 2
-            e, n = PaperMapCoordinates(map = self.settings[mapId], x = x, y = y, paperToMap = True)
+            e, n = PaperMapCoordinates(mapInstr = self.settings[mapId], x = x, y = y, paperToMap = True)
             self.instruction['east'], self.instruction['north'] = e, n
 
     def GetImageOrigSize(self, imagePath):
@@ -1067,7 +1067,7 @@ class Point(InstructionObject):
                 return False
         
         self.instruction.update(instr)
-        instr['where'] = PaperMapCoordinates(map = mapInstr, x = self.instruction['east'],
+        instr['where'] = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east'],
                                              y = self.instruction['north'], paperToMap = False)
         w = h = self.unitConv.convert(value = instr['size'], fromUnit = 'point', toUnit = 'inch')
         instr['rect'] = Rect2D(x = float(instr['where'][0]) - w / 2, y = float(instr['where'][1] - h / 2),
@@ -1122,9 +1122,9 @@ class Line(InstructionObject):
                 return False
         
         self.instruction.update(instr)
-        e1, n1 = PaperMapCoordinates(map = mapInstr, x = self.instruction['east1'],
+        e1, n1 = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east1'],
                                      y = self.instruction['north1'], paperToMap = False)
-        e2, n2 = PaperMapCoordinates(map = mapInstr, x = self.instruction['east2'],
+        e2, n2 = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east2'],
                                      y = self.instruction['north2'], paperToMap = False)
         instr['where'] = [wx.Point2D(e1, n1), wx.Point2D(e2, n2)]
         instr['rect'] = Rect2DPP(instr['where'][0], instr['where'][1])
@@ -1181,9 +1181,9 @@ class Rectangle(InstructionObject):
                 return False
         
         self.instruction.update(instr)
-        e1, n1 = PaperMapCoordinates(map = mapInstr, x = self.instruction['east1'],
+        e1, n1 = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east1'],
                                        y = self.instruction['north1'], paperToMap = False)
-        e2, n2 = PaperMapCoordinates(map = mapInstr, x = self.instruction['east2'],
+        e2, n2 = PaperMapCoordinates(mapInstr = mapInstr, x = self.instruction['east2'],
                                        y = self.instruction['north2'], paperToMap = False)
         instr['rect'] = Rect2DPP(wx.Point2D(e1, n1), wx.Point2D(e2, n2))
         self.instruction.update(instr)

+ 33 - 39
gui/wxpython/psmap/utils.py

@@ -154,50 +154,44 @@ def convertRGB(rgb):
         else:  
             return None
         
-def PaperMapCoordinates(map, x, y, paperToMap = True):
-    """!Converts paper (inch) coordinates -> map coordinates"""
-    unitConv = UnitConversion()
-    currRegionDict = grass.region()
-    cornerEasting, cornerNorthing = currRegionDict['w'], currRegionDict['n']
-    xMap = map['rect'][0]
-    yMap = map['rect'][1]
-    widthMap = map['rect'][2] * 0.0254 # to meter
-    heightMap = map['rect'][3] * 0.0254
-    xScale = widthMap / abs(currRegionDict['w'] - currRegionDict['e'])
-    yScale = heightMap / abs(currRegionDict['n'] - currRegionDict['s'])
-    currScale = (xScale + yScale) / 2
- 
-    if not paperToMap:
-        textEasting, textNorthing = x, y
-        eastingDiff = textEasting - cornerEasting 
-        if currRegionDict['w'] > currRegionDict['e']:
-            eastingDiff = - eastingDiff
-        else:
-            eastingDiff = eastingDiff
+        
+def PaperMapCoordinates(mapInstr, x, y, paperToMap = True):
+    """!Converts paper (inch) coordinates <-> map coordinates.
 
-        northingDiff = textNorthing - cornerNorthing
-        if currRegionDict['n'] > currRegionDict['s']:
-            northingDiff = - northingDiff 
+    @param mapInstr map frame instruction
+    @param x,y paper coords in inches or mapcoords in map units
+    @param paperToMap specify conversion direction
+    """
+    region = grass.region()
+    mapWidthPaper = mapInstr['rect'].GetWidth()
+    mapHeightPaper = mapInstr['rect'].GetHeight()
+    mapWidthEN = region['e'] - region['w']
+    mapHeightEN = region['n'] - region['s']
+
+    if paperToMap:
+        diffX = x - mapInstr['rect'].GetX()
+        diffY = y - mapInstr['rect'].GetY()
+        diffEW = diffX * mapWidthEN / mapWidthPaper
+        diffNS = diffY * mapHeightEN / mapHeightPaper
+        e = region['w'] + diffEW
+        n = region['n'] - diffNS
+
+        if projInfo()['proj'] == 'll':
+            return e, n
         else:
-            northingDiff = northingDiff
+            return int(e), int(n)
 
-        xPaper = xMap + unitConv.convert(value = eastingDiff, fromUnit = 'meter', toUnit = 'inch') * currScale
-        yPaper = yMap + unitConv.convert(value = northingDiff, fromUnit = 'meter', toUnit = 'inch') * currScale
-        return xPaper, yPaper
     else:
-        if currRegionDict['w'] < currRegionDict['e']:
-            eastingDiff = (x - xMap) 
-        else:
-            eastingDiff = (xMap - x)
-        if currRegionDict['n'] < currRegionDict['s']:
-            northingDiff = (y - yMap) 
-        else:
-            northingDiff = (yMap - y)
+        diffEW = x - region['w']
+        diffNS = region['n'] - y
+        diffX = mapWidthPaper * diffEW / mapWidthEN
+        diffY = mapHeightPaper * diffNS / mapHeightEN
+        xPaper = mapInstr['rect'].GetX() + diffX
+        yPaper = mapInstr['rect'].GetY() + diffY
+
+        return xPaper, yPaper
+
 
-        textEasting = cornerEasting + unitConv.convert(value = eastingDiff, fromUnit = 'inch', toUnit = 'meter') / currScale
-        textNorthing = cornerNorthing + unitConv.convert(value = northingDiff, fromUnit = 'inch', toUnit = 'meter') / currScale
-        return int(textEasting), int(textNorthing)
-        
 def AutoAdjust(self, scaleType,  rect, map = None, mapType = None, region = None):
     """!Computes map scale, center and map frame rectangle to fit region (scale is not fixed)"""
     currRegionDict = {}