2013-03-27 46 views
0

我正在研究類似GIS的應用程序,並且需要在Gtk窗口上繪製(2d)製圖地圖。 我們獲得座標 - 已經轉換爲標準x,y格式 - 從PROJ.4如何縮放地圖座標以確保其完全可見

所有點都有非常大的大的數字作爲座標。 例如,一個典型的點可以被描述如下:

X = 820787.12345 ...

Y = 3937564.12345 ...

此區域仍太大到繪製在我的GTK窗口中(最大分辨率: * )!

所以,我有一個簡單,愚蠢問題:是否有以任何標準的方法繪製它,以確保我的地圖是我的默認繪圖區域完全顯示之前連貫規模製圖地圖的大小(不失精度)?

是的,我知道在加入點之前,我可以首先將每個座標除以一個常數;但這種「原始」方法似乎(對我來說)是sl散和不準確的。

如果我能解決我的問題,我保證做一個演示與其他用戶分享。

感謝

IT

回答

1

不知道如果我是正確的,但我用開羅對gtk_windows繪製。 如果您使用的開羅,這可能會有幫助:

// 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); 
0

首先,非常感謝你! 感謝您的建議,我解決了我的煩惱。

下面你可以看到一個簡單的_self_containing_演示應用程序:

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() 
相關問題