为绘图排序多边形坐标

时间:2014-02-07 14:24:49

标签: python polygon matplotlib-basemap shapely

我有一个由许多单元组成的模型网格,我想在matplotlib basemap上绘制阴影多边形。

使用pyproj,我首先使用shapely.geometry的{​​{1}}类创建多边形,然后从中提取网格的外部坐标。然后我将它们还原为WGS84,以便传递给我的绘图功能:

Polygon

然后,使用grid_x_mesh, grid_y_mesh = pyproj.transform(wgs84, nplaea, grid_lons, grid_lats) grid_x = grid_x_mesh.ravel() grid_y = grid_y_mesh.ravel() grid_poly = Polygon(zip(grid_x, grid_y)) grid_x, grid_y = grid_poly.exterior.coords.xy grid_plons, grid_plats = pyproj.transform(nplaea, wgs84, grid_x, grid_y) 方法,我将WSG84坐标投影到地图投影(在本例中为nplaea)和

matplotlib.basemap

当试图这样做时,我得到了一个十字交叉的图案,我假设它必须对我提供给多边形函数的坐标进行排序。

我认为这与我如何提取外部坐标有关,或者只是在我创建最终多边形时绘制坐标列表的顺序。

如果这是问题,是否有一种巧妙的方式来正确订购这些?

绘制多边形 enter image description here

关闭式 enter image description here

1 个答案:

答案 0 :(得分:0)

Soooo ......显然shapely.geometry.Polygon方法正在绘制具有所有内部网格坐标的多边形,我意识到由于grid_plonsgrid_plats具有与{{np.ravel()相同的长度1}}'网格坐标数组。

我最后只是从网格坐标数组中手动​​提取外部坐标,然后将它们传递给Polygon方法(见下文)。虽然,我想可能有一种更漂亮,更通用的方法。

手动提取方法:

grid_x_mesh, grid_y_mesh = pyproj.transform(wgs84, nplaea, grid_lons, grid_lats)

# The coordinates must be ordered in the order they are to be drawn
[grid_x.append(i) for i in grid_x_mesh[0,:]]
[grid_x.append(i) for i in grid_x_mesh[1:-1,-1]]
# Note that these two sides of the polygon are appended in reverse
[grid_x.append(i) for i in (grid_x_mesh[-1,:])[::-1]]
[grid_x.append(i) for i in (grid_x_mesh[1:-1,0])[::-1]]

[grid_y.append(i) for i in grid_y_mesh[0,:]]
[grid_y.append(i) for i in grid_y_mesh[1:-1,-1]]
[grid_y.append(i) for i in (grid_y_mesh[-1,:])[::-1]]
[grid_y.append(i) for i in (grid_y_mesh[1:-1,0])[::-1]]

grid_poly = Polygon(zip(grid_x, grid_y))