Question

I'm working on a GIS-like app, and I need to draw a (2d)cartographic map on a Gtk window. We get the coordinates - already converted into standard x, y format - from PROJ.4.

All points have very large numbers as coordinates. For example, a typically point could be described as below:

x = 820787.12345...

y = 3937564.12345...

This area is still too large to be drawn in my GTK window(max resolution: 1366 * 768)!

So, I have a simple, stupid question: is there any standard method to coherently scale the size of a cartographic map before drawing it, to ensure that my map is fully displayed in my default drawing area(without loss of precision)?

Yes, I know I simply could first divide each coordinate by a constant, before joining the points; but this "raw" method seems(to me, of course) sloppy and inaccurate.

If i could solve my problem, I promise to make a demo to share with other users.

Thanks

IT

Was it helpful?

Solution

Not sure if i am right, but i use cairo for drawing on gtk_windows. if you are using cairo, this might be helpful:

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

OTHER TIPS

First of all, thank you very much! I have solved my troubles thanks to your suggestion.

below you can see a simple _self_containing_ demo application:

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top