Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib Basemap plotting lat/long coordinates incorrectly

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

like image 392
theParanoidAndroid Avatar asked Sep 17 '25 11:09

theParanoidAndroid


1 Answers

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)
like image 130
ImportanceOfBeingErnest Avatar answered Sep 20 '25 00:09

ImportanceOfBeingErnest