proj = ccrs.AlbersEqualArea(central_longitude=(lon1 + lon2) * 0.5,#central_longtitude:中央经线 central_latitude=(lat1 + lat2) * 0.5,#central_latitude:中央纬线 )#修改投影投影
时间: 2024-04-28 11:23:09 浏览: 121
这段代码使用了Cartopy库中的AlbersEqualArea投影方法,将地球表面投影到平面上,以便于进行地图绘制和分析。其中,central_longitude和central_latitude分别表示投影中心的经度和纬度。AlbersEqualArea投影方法是一种等面积投影方法,可以保持地图上区域面积的真实性,适用于大范围的区域地图绘制。
相关问题
geodetic_to_gauss_trans(double lon, double lat, int zone_mode, double custom_longitude) { if ((lon >= -180 && lon <= 180) && (lat >= -90 && lat <= 90) && (zone_mode == -1 || zone_mode == 0 || zone_mode == 1) && (custom_longitude >= -180 && custom_longitude <= 180)) { switch (zone_mode) { case 1: if (lon >= 1.5) { zone_ = int((lon + 1.5) / 3); central_meridian_ = zone_ * 3; } if (lon < 1.5) { zone_ = int((lon + 1.5) / 3) + 120; central_meridian_ = zone_ * 3 - 360; } break; case -1: if (lon >= 0) { zone_ = int(lon / 6) + 1; central_meridian_ = zone_ * 6 - 3; } if (lon < 0) { zone_ = int(lon / 6) + 60; central_meridian_ = (zone_ * 6 - 3) - 360; } break; case 0: central_meridian_ = custom_longitude; break; } } else { x_ = 0; y_ = 0; return false; } std::string proj_string = "+proj=tmerc +lat_0=0 +lon_0=central_meridian +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs"; std::string to_replace = "central_meridian"; std::string replace_with = std::to_string(central_meridian_); size_t pos = proj_string.find(to_replace); proj_string.replace(pos, to_replace.length(), replace_with); PJ_CONTEXT *C = proj_context_create(); PJ *P = proj_create(C, proj_string.c_str()); PJ *G = proj_crs_get_geodetic_crs(C, P); PJ_AREA *A = nullptr; const char *const *options = nullptr; PJ *G2P = proj_create_crs_to_crs_from_pj(C, G, P, A, options); PJ_COORD c_in{}; c_in.lpzt.z = 0.0; c_in.lpzt.t = HUGE_VAL; c_in.lp.lam = lon; c_in.lp.phi = lat; PJ_COORD c_out = proj_trans(G2P, PJ_FWD, c_in); x_ = c_out.enu.n; y_ = c_out.enu.e; // PJ_COORD c_inv = proj_trans(G2P, PJ_DIRECTION::PJ_INV, c_out); std::cout.precision(20); std::cout << std::fixed; std::cout << x_ << "," << y_ << std::endl; std::cout << std::fixed << c_inv.lp.lam << "," << c_inv.lp.phi << std::endl; proj_destroy(P); proj_destroy(G); proj_destroy(G2P); proj_context_destroy(C); return true; }
这段代码是一个函数,其功能是将一个经纬度坐标转换为高斯投影坐标系下的坐标。函数接收4个参数:经度、纬度、投影区域模式和自定义中央经线。其中,经度和纬度是输入参数,表示待转换的点坐标;投影区域模式和自定义中央经线是可选参数,用于指定采用哪种投影方式以及中央经线的位置。
在函数内部,首先对输入参数进行合法性判断,如果不合法则直接返回false。然后根据投影区域模式和自定义中央经线计算中央经线的位置,并构造高斯投影坐标系的参数字符串。接着,利用Proj库中的函数进行投影变换,将经纬度坐标转换为高斯投影坐标系下的坐标。最后,将计算得到的坐标存储在函数的成员变量x_和y_中,并返回true表示转换成功。
需要注意的是,这段代码中使用了Proj库进行投影变换,因此需要在项目中引入该库的头文件和链接库。
已知程序 import xarray as xr from collections import namedtuple import numpy as np from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter import matplotlib.ticker as mticker import cartopy.feature as cfeature import cartopy.crs as ccrs import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colors as mcolors def region_mask(lon, lat, extents): lonmin, lonmax, latmin, latmax = extents return ( (lon >= lonmin) & (lon <= lonmax) & (lat >= latmin) & (lat <= latmax) ) Point = namedtuple('Point', ['x', 'y']) Pair = namedtuple('Pair', ['start', 'end']) time = '2023-05-04' filepath_DPR = r"C:\pythontest\zFactor\test1.nc4" extents = [110, 122, 25, 38] with xr.open_dataset(filepath_DPR) as f: lon_DPR = f['FS_Longitude'][:] lat_DPR = f['FS_Latitude'][:] zFactorFinalNearSurface = f['FS_SLV_zFactorFinalNearSurface'][:] nscan, nray = lon_DPR.shape midray = nray // 2 mask = region_mask(lon_DPR[:, midray], lat_DPR[:, midray], extents) index = np.s_[mask] lon_DPR = lon_DPR[index] lat_DPR = lat_DPR[index] zFactorFinalNearSurface = zFactorFinalNearSurface[index] for data in [ zFactorFinalNearSurface, ]: data.values[data <= -9999] = np.nan proj = ccrs.PlateCarree() fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection=proj) ax.coastlines(resolution='50m', lw=0.5) ax.add_feature(cfeature.OCEAN.with_scale('50m')) ax.add_feature(cfeature.LAND.with_scale('50m')) ax.set_xticks(np.arange(-180, 181, 5), crs=proj) ax.set_yticks(np.arange(-90, 91, 5), crs=proj) ax.xaxis.set_minor_locator(mticker.AutoMinorLocator(2)) ax.yaxis.set_minor_locator(mticker.AutoMinorLocator(2)) ax.xaxis.set_major_formatter(LongitudeFormatter()) ax.yaxis.set_major_formatter(LatitudeFormatter()) ax.set_extent(extents, crs=proj) ax.tick_params(labelsize='large') def make_zF_cmap(levels): '''制作雷达反射率的colormap.''' nbin = len(levels) - 1 cmap = cm.get_cmap('jet', nbin) norm = mcolors.BoundaryNorm(levels, nbin) return cmap, norm levels_zF = [0, 1, 5, 10, 15, 20, 25, 30, 35, 40, 45] cmap_zF, norm_zF = make_zF_cmap(levels_zF) im = ax.contourf( lon_DPR, lat_DPR, zFactorFinalNearSurface, levels_zF, # 三个物理量为 (500, 49)就是在500*49的格点上赋予这三个物理量 cmap=cmap_zF, norm=norm_zF, extend='both', transform=proj ) cbar = fig.colorbar(im, ax=ax, ticks=levels_zF) cbar.set_label('zFactor (dBZ)', fontsize='large') cbar.ax.tick_params(labelsize='large') ax.set_title(f'DPR zFactor on {time}', fontsize='x-large') plt.show()如何将其中的zFactorFinal变量变为二维
在这段代码中,zFactorFinalNearSurface的shape是(500, 49),其中第一维是nscan(扫描线数),第二维是nray(每个扫描线的射线数)。如果要将zFactorFinalNearSurface变为二维数组,可以将nscan和nray相乘得到总的格点数,然后使用reshape方法将其变为二维数组。代码如下:
```
zFactorFinalNearSurface = zFactorFinalNearSurface[index]
nscan, nray = zFactorFinalNearSurface.shape
zFactorFinalNearSurface_2d = zFactorFinalNearSurface.reshape(nscan*nray, 1)
```
其中,index是通过region_mask函数得到的布尔索引,用于筛选出位于指定区域内的数据。reshape方法中第一个参数是新的shape,这里是(nscan*nray, 1),表示将数据变成nscan*nray行,1列的形状。注意reshape方法返回的是一个新的数组,不会改变原始数组的值。
阅读全文