1

I'm trying to plot some data with matplotlib and basemap, but I have trouble making the coordinates match between Google Maps and basemap. For example, I click at a distinctive feature on the map in Google Maps, and read the coordinates (this is on the north east coast of Greenland:

Google Maps example

I then try to plot those same coordinates on a map with matplotlib and basemap:

basemap example

The two points seem to differ by about 10 km. Are there any parameters I can pass to basemap, or any conversions between ellipsoiods or anything I can do to make these points match up?

The python code I used was this:

#!/usr/bin/env python

import matplotlib
matplotlib.use('agg')
from mpl_toolkits.basemap import Basemap
import mpl_toolkits.basemap.pyproj as pyproj
import matplotlib.pyplot as plt

# These are the coordinates I read out from Google Maps
lat, lon = 80.615439, -15.474109

m = Basemap(width=200000, height=150000, resolution='i', projection='stere', lat_ts=80, lat_0=lat, lon_0=lon)

m.fillcontinents(color='#aaaaaa', lake_color='#cccccc')
m.drawcountries(linewidth=0.2)
m.drawmapboundary(fill_color='#cccccc')

X, Y = m(lon, lat)

plt.scatter(X, Y, s = 20, color = 'red')
plt.savefig('map.png')

I tried this:

# These are the coordinates I read out from Google Maps
lat, lon = 80.615439, -15.474109

gmaps = pyproj.Proj('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs')
wgs84 = pyproj.Proj(init='epsg:4326')

print(lat, lon)
lon, lat = pyproj.transform(gmaps, wgs84, lon, lat)
print(lat, lon)

This converts from 80.615439 -15.474109 to 0.000724 -0.000139.

What I'm looking for is a way to convert between two different lat-lon systems.

1

I do not believe that matplotlib natively supports Spherical Mercator. So you will need to convert your cords from that to the matplotlib projection.

Set projection to 'merc'.

Pyproj should help you with the transform from google. Something like below.

project = partial(
    pyproj.transform,
    pyproj.Proj(init='epsg:4326'),
    pyproj.Proj('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'))

google_point = transform(project, google_point_original)
  • Not positive, but you may need to reverse the coordinates before you transform. – Frank Phillips Nov 14 '14 at 2:08
  • No luck. Your code doesn't work as is (you can't call transform on a partiually evaluated transform), but I rewrote a bit and tried the following: ` # These are the coordinates I read out from Google Maps lat, lon = 80.615439, -15.474109 gmaps = pyproj.Proj('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs') wgs84 = pyproj.Proj(init='epsg:4326') print(lat, lon) lon, lat = pyproj.transform(gmaps, wgs84, lon, lat) print(lat, lon) ` but it doesn't seem to work as I expect (i.e. convert from one lat-lon system to another. – Tor Nov 14 '14 at 14:05
  • Yeah sorry should have made it clear it wasn't functioning. I am not a python guru, but that is the general solution. – Frank Phillips Nov 14 '14 at 14:09
  • Updated my comment, but it seems multi-line code blocks don't work. I edited you post instead. – Tor Nov 14 '14 at 14:10
  • Invert the latlong before you transform? – Frank Phillips Nov 14 '14 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.