北极地区有16个区域,每个区域的纬度/经度顶点都给了我。我需要找到每个区域内存在的所有点。数据的分辨率为1.5x1.5度,经度为0到358.5,纬度为90到-90。
我尝试使用matplotlib.path,并且已经能够处理经度从-180到+180的交点,但是中央北极(即北极)似乎有麻烦。看来,沿北约80度的方向绘图不能识别出一直指向90度的点。
首先列出“问题区域”(中央北极)的代码,然后列出未跨越-180/180线的其他区域的代码。
# regions[i] represents a class of 16 regions
# Problem region!
x, y = np.meshgrid(lons, lats)
x, y = x.flatten(), y.flatten()
points = np.vstack((x,y)).T
lons_temp = regions[i].lons - 180
lons_temp = np.append(lons_temp, lons_temp[0])
lats_temp = np.append(regions[i].lats, regions[i].lats[0])
cross = np.where(np.diff(np.signbit(lons_temp)))[0]
lons_temp = lons_temp + 180
for c in cross:
cross_point = lons_temp[c]
if cross_point<90:
mid_val = 0
elif cross_point >= 90 and cross_point < 270:
mid_val = 180
elif cross_point >=270:
mid_val = 360
interp = np.interp(mid_val, lons_temp[c:c+2], lats_temp[c:c+2])
lons_temp = np.insert(lons_temp, c+1, mid_val)
lats_temp = np.insert(lats_temp, c+1, interp)
lons_neg = lons_temp[np.where(lons_temp <= 180)]
lats_neg = lats_temp[np.where(lons_temp <= 180)]
lons_pos = lons_temp[np.where(lons_temp >= 180)]
lats_pos = lats_temp[np.where(lons_temp >= 180)]
gg_neg = np.array([lons_neg, lats_neg])
gg_pos = np.array([lons_pos, lats_pos])
pp_neg = gg_neg.T # lon lat pair
pp_pos = gg_pos.T
p_neg=Path(pp_neg, closed=False)
p_pos=Path(pp_pos, closed=False)
grid_neg = p_neg.contains_points(points)
grid_pos = p_pos.contains_points(points)
grid = np.logical_or(grid_neg, grid_pos)
grid = np.where(points.T[1]>=82.5, True, grid)
latslons1 = np.where(grid==True)[0]
regions[i].included_points = latslons1
# Other regions that do not cross over -180/180
x, y = np.meshgrid(lons, lats)
x, y = x.flatten(), y.flatten()
points = np.vstack((x,y)).T
gg = np.array([regions[i].lons, regions[i].lats])
pp = gg.T
p=Path(pp)
grid = p.contains_points(points)
latslons1 = np.where(grid==True)[0]
regions[i].included_points = latslons1
我希望有一种方法可以将所有区域包含在一个代码块中,在那里它可以处理围绕-180/180线的换行并解决中心北极的问题。下面的链接显示了我要复制的图像(但是,我确实需要数据点才能正确分析)。 https://nsidc.org/data/masie/browse_regions
答案 0 :(得分:0)
仅此代码无效,因此我尝试通过创建输入数据来填补空白。
请参阅how to create a Minimal, Complete, and Verifiable example
通过如下定义有问题的区域,代码的下部起作用。这表明问题取决于包括极点在内的区域的坐标。如果您修改这些内容,则应该使其与.contains_points兼容
import numpy as np
from matplotlib import path
# create missing data
lons = np.arange(0,360,1.5)
lats = np.arange(-90,90.1,1.5,)
class polyg:
lons = []
lats=[]
# unproblematic region
region = polyg()
region.lats = np.array([80.,85.,80.])
region.lons = np.array([100.,100.,110.])
regions=[region]
# Including North pole
region = polyg()
region.lats = np.array([80.,80.,90.,90.])
region.lons = np.array([0.,360.,360.,0.])
regions.append(region)
i=1
x, y = np.meshgrid(lons, lats)
x, y = x.flatten(), y.flatten()
points = np.vstack((x,y)).T
gg = np.array([regions[i].lons, regions[i].lats])
pp = gg.T
p=path.Path(pp)
grid = p.contains_points(points)
latslons1 = np.where(grid==True)[0]
regions[i].included_points = latslons1
print(x[latslons1])
print(y[latslons1])
[ 0. 1.5 3. ... 355.5 357. 358.5]
[81. 81. 81. ... 90. 90. 90.]