makegrid 在 cartopy 中等效,从底图移动到 cartopy [英] makegrid equivalent in cartopy, moving from basemap to cartopy

查看:120
本文介绍了makegrid 在 cartopy 中等效,从底图移动到 cartopy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我多年来一直在 Python 2.7 中使用 Basemap,我正在迁移到 Python3.7 并想迁移到 cartopy.我处理了很多有投影信息的数据,但我没有数据的经纬度网格.这就是我在 Basemap 中处理事情的方式.

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

所以我在 Basemap 中设置了参考网格m"...然后绘制数据...我用这个:

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)

然后我可以像这样使用 contourf 或 pcolormesh:

I can then use contourf or pcolormesh like this:

m.contourf(x,y,data)

我基本上是在寻找 cartopy 或 pyproj 或 osgeo 中的等价物.我想传递具有网格大小的投影信息并获得纬度/经度,以便我可以使用 cartopy 进行绘图.

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.

感谢任何帮助...

推荐答案

makegrid docs 声明它返回在投影坐标系中等距间隔的纬度和经度.我想您主要是通过让底图为您提供纬度/经度位置来使用它来将投影数据放到任何地图上.Cartopy 以完全不同的方式运行,因为您完全可以在本地坐标系中指定坐标系.

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.

因此,如果您知道 lcc(Lambert Conformal Conic)中数据的坐标,那么您可以将它们传递给 cartopy,它会根据需要为您重新投影:

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())

实际上,您实际上并不需要谈论 lons/lats 来绘制数据.

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

在极少数情况下,即使数据投影到另一个空间,您也只知道纬度/经度.Cartopy 处理此问题:

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())

最后,如果您在投影空间中有一个边界框,但边界框的角在 lon/lat 中,那么您可以简单地变换角,然后使用 linspace.以下(未经测试的)代码应该可以解决问题:

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)

这篇关于makegrid 在 cartopy 中等效,从底图移动到 cartopy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆