如何缩放地图坐标以确保它完全可见

时间:2013-03-27 09:55:42

标签: gtk coordinates gis scale cartography

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

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

x = 820787.12345 ...

y = 3937564.12345 ......

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

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

是的,我知道在加入积分之前,我可以先将每个坐标除以一个常数;但这种“原始”的方法似乎(对我来说当然)草率和不准确。

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

由于

IT

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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()