0
votes

I am working on creating a GUI representation of a map but am having trouble getting the canvas to scale correctly. I am currently mapping the city of Dubrovnik and all points within Dubrovnik have a Latitude between 42N to 43N and a Longitude between 18E and 19E. I was wondering if there was a way to set the Canvas so that the upper left corner of the window was the coordinates (42,19) and the lower right corner was (43, 18)? Any help is much appreciated.

1
I don't think this is possible. Instead, try using Linear interpolation to convert your long/lat coordinates into pixel coordinates. - Kevin

1 Answers

3
votes

You can't, there is no built-in way to do this as far as I know. But you can certainly implement your own canvas that do this job.

Something like:

class GeoCanvas(Tk.Canvas):
    def __init__(self, master, uperleftcoords, bottomrightcoords, **kwargs):
        Canvas.__init__(self, master, **kwargs)

        self.minLat = uperleftcoords.getLat()
        self.maxLong = uperleftcoords.getLong()

        self.maxLat = bottomrightcoords.getLat()
        self.minLong = bottomrightcoords.getLong()

        self.width = **kwargs["width"]
        self.height= **kwargs["height"]

        self.geoWidth = self.maxLat - self.minLat
        self.geoHeight= self.maxLong - self.minLong

    def fromLatLong2pixels(self, coords):
        """ Convert a latitude/longitude coord to a pixel coordinate use it for every point you want to draw."""
        if ( not self.minLat <= coords.getLat() <= self.maxLat) or (not self.minLong <= coords.getLong() <= self.laxLong ):
            return None
        else:
            deltaLat = coords.getLat() - self.minLat
            deltaLong= coords.getLong()- self.minLong

            latRatio = deltaLat / self.geoWidth
            longRatio= deltaLong/ self.geoHeight

            x = latRation * self.width
            y = longRatio * self.height

            return x,y

You can then override the drawing method that you use to convert each lat/long coordinate into a point on your canvas.