Convert canvas' mouse coords to geographic coords

落爺英雄遲暮 提交于 2021-02-19 06:10:51

问题


I'm trying to create a map with all the cities of Italy using Python-Tkinter and Canvas. I found a picture of a map of Italy with a few highlighted cities online and inserted it to my Canvas. After that I used a function to determine what are the canvas coordinates of the 2 highlighted cities using:

import tkinter as tk

class Map():
    #Creator-Function
    def __init__(self,parent):
        self.canvas=tk.Canvas(width=995,height=971,bg='black')
        self.canvas.pack(expand=0,fill='none')
        self.gif=tk.PhotoImage(file='mapOfItl.gif')
        self.canvas.create_image(497.5,485.5, image = self.gif)
        self.canvas.bind('<Button-1>',self.callback)


#Event on mouse-click function
def callback(self,event):
    print(self.long,self.lat,event.x,971-event.y)#lat increases opposite to canvas' y coordinate
root=tk.Tk()
app=Map(root)
root.mainloop()

So for example I clicked on Rome and got x=250 y=150 and Milan and got x=125 y=450

Note that these weren't the original numbers.

Then I searched for the geographical coordinates of Milan and found the: latitude=40.100 and longitude=19.870 of Milan and also latitude=39.850 and longitude=21.975 of Rome (again I'm using random numbers here)

After that by subtracting the longitude/latitude and canvas coords of Milan and Rome (x=longitude, y=latitude) I was able to find the value of 1 canvas pixel in longitude and latitude respectively to the canvas width and canvas height and all I had to do was add an extra number:

coord y to latitude:

  1. Milan-RomeY=450-150=300
  2. MilanLat-RomeLat=40.100-39.850=0.350
  3. So 300 pixels in the Y axis equal to 0.350 degrees
  4. so 1 pixel in the Y axis is equal to 0,350/300=0.1166666...
  5. 0.11666*450(MilanY)=0.525 so I need to add 40.100-0.525=39.575

Accordingly for longitude.

So my function for calculating the latitude of a city in canvas is:

def callback(self,event):
    self.lat=event.y*0.1166666...+39.575
    self.long=.....

However the program seems to work fine for every city between the Milan and Rome:

  • However for every city farther north than Milan(the farthest north of the 2 cities selected at the start) the latitude calculated by the function is corresponds farther south than what it should.
  • For every city farther south than Rome the latitude is bigger(cities are farther north than they should
  • For every city left to Milan the longtitude is farther East
  • For every city right to Rome the longtitude is smaller (Cities left to the Milan the city located farther West are getting measured farther East than they should and the opposite goes for Rome the farthest East of the 2 cities)

Is there something wrong with my thought process, or maybe the map scale? Is there any particular module you would recommend (I've tried looking for such a module but couldnt find any application for canvas gifs) and is there a way to fix this problem without having to install an extra module?

来源:https://stackoverflow.com/questions/47948674/convert-canvas-mouse-coords-to-geographic-coords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!