makegrid equivalent in cartopy, moving from basemap to cartopy

老子叫甜甜 提交于 2021-02-11 05:33:36

问题


So, I have been using Basemap for years in Python 2.7, I am moving to Python3.7 and would like to move to cartopy. I work with a lot of data where I have the projection info but I don't have lat and lon grids of the data. This is how I would handle things in Basemap.

m=Basemap(
    llcrnrlon=-118.300,
    llcrnrlat=20.600,
    urcrnrlon=-58.958,
    urcrnrlat=51.02,
    projection='lcc',
    lat_1=38.,
    lat_2=38.,
    lon_0=-95.,
    resolution ='l',
    area_thresh=1000.
)
mwidth =  1008 #for 163 5km AWIPS2 grid
mheight = 722  #for 163 5km AWIPS2 grid

So I have setup the reference grid 'm' in Basemap... Then to plot the data...I use this:

lons,lats=m.makegrid(mwidth,mheight)
x,y=m(lons,lats)

I can then use contourf or pcolormesh like this:

m.contourf(x,y,data)

I am basically looking for an equivalent in cartopy or pyproj or osgeo. I want to pass projection information with the size of the grid and get lat/lons so I can plot with cartopy.

Any help is appreciated...


回答1:


The makegrid docs state that it returns lats and lons which are equally spaced in the projected coordinate system. I imagine you mostly use this to put projected data onto any map by letting basemap give you the lat/lon locations. Cartopy operates in quite a different way, in that you are perfectly allowed to specify the coordinate system of your coordinates in the native coordinate system.

So, if you know your data's coordinates in lcc (Lambert Conformal Conic) then you can pass those through to cartopy, which will reproject it for you as appropriate:

xs = np.linspace(llc_x0, llc_x1, n_xs),
ys = np.linspace(llc_y0, llc_y1, n_ys),
plt.contourf(xs, ys, data, transform=ccrs.LambertConformalConic())

In effect, you don't actually need to talk lons/lats in order to be able to draw your data.

In some rare situations you only know lats/lons, even if the data is projected in another space. Cartopy deals with this with:

plt.contourf(lons, lats, data, transform=ccrs.PlateCarree())

Finally, if you have a bounding box in projected space, but the bounding box corners are in lon/lat then you can simply transform the corners then use linspace. The following (untested) code should do the trick:

import cartopy.crs as ccrs
import numpy as np

llc = ccrs.LambertConformal()

width = 20
height = 25
llcrnrlon=-118.300
llcrnrlat=20.600
urcrnrlon=-58.958
urcrnrlat=51.02

lons = np.array([llcrnrlon, urcrnrlon])
lats = np.array([llcrnrlat, urcrnrlat])

projected_corners = llc.transform_points(
    ccrs.PlateCarree(), lons, lats)

xs = np.linspace(
    projected_corners[0, 0], projected_corners[1, 0], width)
ys = np.linspace(
    projected_corners[0, 1], projected_corners[1, 1], height)


来源:https://stackoverflow.com/questions/53873435/makegrid-equivalent-in-cartopy-moving-from-basemap-to-cartopy

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