【问题标题】:how to scale map coordinates to ensure it is completely visible如何缩放地图坐标以确保它完全可见
【发布时间】:2013-04-17 22:43:04
【问题描述】:

我正在开发一个类似 GIS 的应用程序,我需要在 Gtk 窗口上绘制 (2d)地图。 我们从 PROJ.4 获得坐标 - 已转换为 标准 x, y 格式

所有点都有非常的数字作为坐标。 例如,一个典型的点可以描述如下:

x = 820787.12345...

y = 3937564.12345...

这个区域仍然太大无法在我的 GTK 窗口中绘制(最大分辨率:1366 * 768)!

所以,我有一个简单的愚蠢问题:是否有任何标准方法可以在绘制地图之前连贯地缩放地图的大小,以确保我的地图完全显示在我的默认绘图区域中(不损失精度)?

是的,我知道我可以先将每个坐标除以一个常数,然后再加入点;但这种“原始”方法似乎(在我看来,当然)草率且不准确。

如果我能解决我的问题,我承诺制作一个演示与其他用户分享。

谢谢

【问题讨论】:

    标签: gtk coordinates gis scale cartography


    【解决方案1】:

    不确定我是否正确,但我使用 cairo 在 gtk_windows 上绘图。 如果您使用的是 cairo,这可能会有所帮助:

    // save cairo to simply restore old scale_map
    cairo_save(cr);
    
    // move cairo center to your zero pos of your graph
    cairo_translate(cr, x_zero_pos, y_zero_pos);
    
    // scale on max bounds <--- this is what you are looking for
    cairo_scale(cr, graph_width / max_x_amplitude,
                   -graph_height / max_y_amplitude);
    
    // now you can draw your line with real values
    cairo_line....s.o
    
    //restore your cairo. otherwise the linewidth will be depending on the scale
    cairo_restore(cr);
    
    // finally paint
    cairo_set_line_width(cr, linewidth);
    cairo_stroke(cr);
    

    【讨论】:

      【解决方案2】:

      首先,非常感谢您! 多亏了你的建议,我的烦恼都解决了。

      您可以在下面看到一个简单的_self_ contains_演示应用程序:

      import gtk
      from gtk import gdk
      
      class Canvas(gtk.DrawingArea):
      
          # the list of points is passed as argument
      
          def __init__(self, points):
              super(Canvas, self).__init__()
              self.points = points
      
              # Canvas is 800*500 only!
      
              self.set_size_request(800, 500)
              self.scaleFactor = 0.0
              self.connect("expose_event", self.expose)
      
          # Method to paint polygons and lines on screen
      
          def expose(self, widget, event):
              rect = widget.get_allocation()
              w = rect.width
              h = rect.height
      
              # Calculation of the coordinates limits
      
              xMax = max([c[0] for c in self.points])
              yMax = max([c[1] for c in self.points])
              xMin = min([c[0] for c in self.points])
              yMin = min([c[1] for c in self.points])
      
              # Calculation of the size of the bounding box
      
              maxRectWidth = xMax - xMin
              maxRectHeigth = yMax - yMin
      
              # Scale factor calculation
      
              width_ratio = float(w) / maxRectWidth
              height_ratio = float(h) / maxRectHeigth
              self.scaleFactor = min(height_ratio, width_ratio)
      
              # Create Cairo Context object
      
              ctx = widget.window.cairo_create()
      
              # Set colour and line width
      
              ctx.set_line_width(7)
              ctx.set_source_rgb(255, 0, 0)
              ctx.save()
      
              # CRITICAL LINE: the Cairo Context is moved and scaled here
      
              ctx.scale(self.scaleFactor, self.scaleFactor)
              ctx.translate(-xMin, -yMin)
      
              # Drawing of the lines
      
              for i in range(0, len(self.points)):
                  currPoint = self.points[i]
                  currX = float(currPoint[0])
                  currY = float(currPoint[1])
                  nextIndex = i + 1
                  if (nextIndex == len(self.points)):
                      continue
                  nextPoint = self.points[nextIndex]
                  nextX = float(nextPoint[0])
                  nextY = float(nextPoint[1])
                  ctx.move_to(currX, currY)
                  ctx.line_to(nextX, nextY)
              ctx.restore()
              ctx.close_path()
              ctx.stroke()
      
      # a list of single points with large _LARGE _coordinates
      
      li1 = [(200000,200000), (400000,200000), (400000,400000), (200000,400000)]
      
      window = gtk.Window()
      canvas = Canvas(li1)
      window.add(canvas)
      window.show_all()
      gtk.main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 2019-10-03
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多