matplotlib Basemap plotting lat/long coordinates incorrectly

拟墨画扇 提交于 2019-12-02 01:36:42

问题


I am trying to learn matplotlib and the mapping function Basemap, and am trying to plot a simple list of lat/long coordinates on a map. However, the Basemap coordinate conversion to simple x/y coordinates are plotting points wildly off the map, and I can't work out why. My code is below:

locationData = parseFile(locationFile)

fig = plt.figure(figsize=(8,8))
m = Basemap(projection='merc', resolution='h',
            llcrnrlat=49, llcrnrlon=-13.5,
            urcrnrlat=59.5, urcrnrlon=4)
m.drawcoastlines()
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='green', lake_color='aqua')

index=0
for entry in locationData:
    lat = entry["latitudeE7"]/1E7
    long = entry["longitudeE7"]/1E7
    x,y = m(lat, long)
    print("Plotting {} {} to x{} y{}".format(lat,long,x,y))
    plt.plot(x,y, 'ok', markersize=20)
    # break at 30 points for testing
    if index>30: break
    index+=1
plt.show()

And here is the output I get from the print statement:

Plotting 52.------- 1.------- to x79364--.------- y-31476--.-------
Plotting 52.------- 1.------- to x79368--.------- y-31475--.-------
Plotting 52.------- 1.------- to x79362--.------- y-31471--.-------
Plotting 52.------- 1.------- to x79361--.------- y-31472--.-------
Plotting 52.------- 1.------- to x79360--.------- y-31475--.-------
Plotting 52.------- 1.------- to x79365--.------- y-31476--.-------
Plotting 52.------- 1.------- to x79361--.------- y-31476--.-------
...

I've censored the exact values for obvious reasons, but you can see that the intended values of 52°N 1°E which points to the UK are being placed wildly off chart, which is of the UK. Expanding the map to the whole globe reveals that it is plotting the points off the north coast of Madagascar.

I am taking the coordinates from a Google Location History download, then dividing it by 10^7 as they are stored as integers. The print statement shows that these are being parsed correctly.

I am new to matplotlib and basemap, and am running Python 3.6 on Windows 10. Any help?

edit - posted my old code my mistake


回答1:


From the basemap documentation

Calling a Basemap class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y map projection coordinates (in meters). If optional keyword inverse is True (default is False), the inverse transformation from x/y to lon/lat is performed.

Instead of x,y = m(lat, long) you hence need

x,y = m(long, lat)


来源:https://stackoverflow.com/questions/49158130/matplotlib-basemap-plotting-lat-long-coordinates-incorrectly

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