basemap icon indicating copy to clipboard operation
basemap copied to clipboard

lat_0, lon_0 ignored

Open jasoncpatton opened this issue 10 years ago • 3 comments

I am trying to set up Basemap to use a Cylindrical Equal Area projection and to place the origin at a specific lat/lon (this is to match some input x/y data that uses a false easting/northing), but it seems that setting the lat_0 and lon_0 parameters does nothing.

from mpl_toolkits.basemap import Basemap

# Oklahoma bounds 
# ('o' = origin at corner of Kingfisher/Logan/Canadian/Oklahoma counties)
lats = {'n':  37.1, 's':  33.6,  'c':  35.5, 'o':  35.726}
lons = {'e': -94.4, 'w': -103.0, 'c': -97.5, 'o': -97.674}

buf = 0.05 # plot buffer size (in degrees)

res = 'c' # map shapes resolution

# projection parameters
proj = 'cea' # cylindrical equal area
lat_ts = lats['c'] # true scale at geographic center
lon_0 = lons['o']  # x=0 at counties corner
lat_0 = lats['o']  # y=0 at counties corner

m = Basemap(projection = proj, lat_ts = lat_ts, resolution = res,
            lon_0 = lon_0, lat_0 = lat_0,
            urcrnrlon = lons['e'] + buf, urcrnrlat = lats['n'] + buf,
            llcrnrlon = lons['w'] - buf, llcrnrlat = lats['s'] - buf*4)

Then checking the origin gives:

In [15]: m(0, 0, inverse=True)
Out[15]: (-103.05000000000305, 33.399999999963455)

...which is the lower-left corner of the map bounds, not the coordinates I gave for lat_0 and lon_0. I get the same output if I leave out lat_0 and lon_0 entirely.

At first, I thought maybe the CEA projection doesn't support these parameters and that I should just shift my data manually, but the docs claim that lat_0 and lon_0 are used by all projections to set the origin.

As a workaround, I can set an offset to get the intended results:

offset = m(lon_0, lat_0)
df['x'], df['y'] = (df.x + offset[0], df.y + offset[1]) # where df is a Pandas dataframe with my data

Note: Originally posted to StackOverflow, where I was encouraged to move the issue here. http://stackoverflow.com/questions/31302427/lat-0-lon-0-ignored-by-basemap

jasoncpatton avatar Jul 10 '15 18:07 jasoncpatton

I also have the same issue, and would really like to get this fixed.

gamaanderson avatar Aug 04 '15 22:08 gamaanderson

After some debugging found out this was intentional, in a clear contradiction with the documentation

gamaanderson avatar Aug 04 '15 23:08 gamaanderson

To add to this issue, some psuedocyl projections ('moll' and 'hammer' checked so far) appear to respect the lon_0 value, but there is a strange xaxis switch at specific values. Notice how the second value switches to "celestial" orientation (longitude increasing to the left).

(Note: This was using basemap 1.0.7)

import mpl_toolkits.basemap as basemap
import numpy as np
import pylab as plt

lon_0,lat_0 = -80.5,0
parallels=np.arange(-90.,120.,30)
meridians=np.arange(0,420.,60.)

fig,ax = plt.subplots(3,1,figsize=(6,9))
for i,(lon_0,lat_0) in enumerate([(-80.5,0),(-80.58345277606,0),(-80.6,0)]):
    plt.sca(ax[i])
    m = basemap.Basemap(projection='moll',lon_0=lon_0,lat_0=lat_0)
    m.drawparallels(parallels)
    m.drawmeridians(meridians)
    for mer in meridians[:-1]:
        plt.annotate(r'$%i^{\circ}$'%mer,m(mer,5),ha='center')
plt.savefig('moll.png',bbox_inches='tight')

image

kadrlica avatar Aug 27 '15 01:08 kadrlica