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

Gtk 如何缩放地图坐标以确保其完全可见,gtk,coordinates,gis,scale,cartography,Gtk,Coordinates,Gis,Scale,Cartography,我正在开发一个类似GIS的应用程序,我需要在Gtk窗口上绘制(2d)地图。 我们从项目4中获取坐标(已转换为标准x,y格式) 所有点都有非常大的数字作为坐标。 例如,一个典型的点可以描述如下: x=820787.12345… y=3937564.12345… 此区域仍然太大,无法在我的GTK窗口中绘制(最大分辨率:1366*768) 因此,我有一个简单而愚蠢的问题:在绘制地图之前,是否有任何标准方法来一致地缩放地图的大小,以确保我的地图在默认绘图区域中完全显示(不损失精度) 是的,我知道我可以在

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

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

x=820787.12345…

y=3937564.12345…

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

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

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

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

谢谢


不确定我是否正确,但我使用cairo在gtk_窗口上绘图。 如果您正在使用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);
//保存cairo以简单地恢复旧比例地图
开罗储蓄银行(cr);
//将cairo中心移动到图形的零位置
开罗翻译(cr、x、y);

//在最大边界上缩放首先,非常感谢! 多亏了你的建议,我已经解决了我的麻烦

下面您可以看到一个简单的包含演示应用程序的U self u u u u u u u:

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()