GeoDataFrame 组织及其对绘制地图的影响

问题描述 投票:0回答:1

完整编辑

我正在制作一张绘制的测深地图,我已将其分成 36 个图块/区域,并通过 Flask 渲染为 HTML。

问题:我的 GDF 的“区域”与我的 HTML 的图块顺序不匹配。我的 GDF 的区域索引遵循从左下到右上的逻辑,而进入我的 Html 的 .png 逻辑遵循从左上到右下的逻辑。

创建区域时,我可以通过在以下函数中的

miny
中反转
maxy
y_points
来匹配索引,但我的绘图有点偏离(见下图):

def create_map_zones_gdf(map_boundaries_gdf):
    # Extract the bounds of the map
    minx, miny, maxx, maxy = map_boundaries_gdf.total_bounds
    
    # Generate a grid of points
    x_points = np.linspace(minx, maxx, GRID_WIDTH + 1)
    y_points = np.linspace(miny, maxy, GRID_WIDTH + 1)

    # Create polygons based on the grid points
    polygons = []
    for row in range(GRID_WIDTH):
        for col in range(GRID_WIDTH):
            x1, x2 = x_points[col], x_points[col + 1]
            y1, y2 = y_points[row], y_points[row + 1]
            polygon = Polygon([(x1, y2), (x2, y2), (x1, y1), (x2, y1)])
            polygons.append(polygon)

    # Create GeoDataFrame for map zones
    map_zones_gdf = gpd.GeoDataFrame(geometry=polygons)
    
    # Add a new column 'zone' with the zone index
    map_zones_gdf['zone'] = range(len(map_zones_gdf))

    return map_zones_gdf

更改后绘图:https://ibb.co/mX1DB3P

我尝试更改绘图函数中

ax.set_extent
的顺序,但情况更糟。上面的图像是这样渲染的:

main_plot, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
minx, miny, maxx, maxy = map_boundaries_gdf.total_bounds
ax.set_extent([minx, maxx, miny, maxy])

我还尝试了一些疯狂的函数,我会重复

create_map_zones_gdf()
两次,但使用不同的
miny
maxy
排序,然后根据其几何形状连接每个数据帧,但即使它们相等,连接函数也不会将它们识别为这是因为我的多边形的坐标不按相同的顺序。老实说,感觉就像为此编写了很多代码......

pandas dataframe matplotlib geopandas cartopy
1个回答
0
投票

通过浏览相关问题找到答案:

Matplotlib 坐标。 sys原点到左上角

通过在左上角说明我的情节的起源,一切都得到了解决:

plt.ylim(max(plt.ylim()), min(plt.ylim()))

© www.soinside.com 2019 - 2024. All rights reserved.