【问题标题】:Issue with Cartopy and PILCartopy 和 PIL 的问题
【发布时间】:2021-08-09 16:58:30
【问题描述】:

我在新系统上安装了 Anaconda Spyder,我收到以下错误消息:

C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py:973: 用户警告:透明度以字节表示的调色板图像应转换为 RGBA 图像 警告.warn(

下面是我正在使用的代码。

Python 3.8.8(默认,2021 年 4 月 13 日,15:08:03)[MSC v.1916 64 位 (AMD64)] 输入“copyright”、“credits”或“license”了解更多信息。

IPython 7.22.0 -- 增强的交互式 Python。

正在重启内核...

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy 
import cartopy.io.img_tiles as cimgt
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

cartopy.config['pre_existing_data_dir']='C:/Users/***<USER>***/.local/share/cartopy'

mapSize = '10m'
zoom = 8
rivers = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', mapSize)
states_provinces = cfeature.NaturalEarthFeature(category='physical', 
name='admin_1_states_provinces_lines', scale=mapSize, facecolor='none')
request = cimgt.OSM()
stamen_terrain = cimgt.Stamen('terrain-background')

longMax = 126.38765
longMin = -132.80147
latMax = 54.03728
latMin = 34.90657

# Create a Stamen terrain background instance.
stamen_terrain = cimgt.Stamen('terrain-background')
fig = plt.figure(figsize=(90, 50))
ax = fig.add_subplot(1, 1, 1, projection=request.crs)

gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', 
alpha=0.5, linestyle='--')
gl.top_labels = gl.right_labels = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 22, 'color': 'black', 'weight': 'bold'}
gl.ylabel_style = {'size': 22, 'color': 'black', 'weight': 'bold'}

# Limit the extent of the map to a small longitude/latitude range.
extent = [longMax, longMin, latMax, latMin]
ax.set_extent(extent, crs=ccrs.PlateCarree())

# Set the Stamen data zoom level
ax.add_image(stamen_terrain, zoom)

此错误/警告并不总是发生,但当它发生时,程序似乎冻结并且没有其他任何事情发生。即使我停止当前命令,也不会发生任何事情。我唯一能做的就是重启内核。

非常感谢任何帮助解决此问题。

【问题讨论】:

    标签: python spyder cartopy


    【解决方案1】:

    鉴于地图的面积,缩放级别似乎太高了。将缩放级别更改为 7 解决了该问题。

    我还注意到,如果地图区域缩小,则缩放级别 8 有效。

    为解决此问题,地图大小和缩放级别基于地图区域。代码如下:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import cartopy 
    import cartopy.io.img_tiles as cimgt
    from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
    import numpy as np
    from pyproj import Proj
    
    cartopy.config['pre_existing_data_dir']='C:/Users/Examiner/.local/share/cartopy'
    
    ####################################################################
    # Calculate Map Area
    ####################################################################
    def areaCalc(longMax, longMin, latMax, latMin):
        co = {"type": "Polygon", "coordinates": [[(longMax,latMax), 
            (longMax,latMin), (longMin,latMin), (longMin,latMax), 
            (longMax,latMax)]]}
        lon, lat = zip(*co['coordinates'][0])
        pa = Proj("+proj=aea +lat_1="+str(latMin)+" +lat_2=" + str(latMax) + " 
           +lat_0=" + str((latMax+latMin)/2) + " +lon_0=" + 
           str((longMax+longMin)/2))
        x, y = pa(lon, lat)
        area = np.abs(0.5*np.sum(y[:-1]*np.diff(x) - x[:-1]*np.diff(y)))
        return(area)
    ####################################################################
    
    longMax = 126.38765
    longMin = -132.80147
    latMax = 54.03728
    latMin = 34.90657
    
    area = areaCalc(longMax, longMin, latMax, latMin)
    if area <= 210000000000:
        mapSize = '10m'
        zoom = 8
    elif area > 210000000000 and area <= 14500000000000:
        mapSize = '50m'
        zoom = 6  
    else:
        mapSize = '110m'
        zoom = 4
    
    rivers = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', 
        mapSize)
    states_provinces = cfeature.NaturalEarthFeature(category='physical', 
        name='admin_1_states_provinces_lines', scale=mapSize, facecolor='none')
    request = cimgt.OSM()
    stamen_terrain = cimgt.Stamen('terrain-background')
    
    # Create a Stamen terrain background instance.
    stamen_terrain = cimgt.Stamen('terrain-background')
    fig = plt.figure(figsize=(90, 50))
    ax = fig.add_subplot(1, 1, 1, projection=request.crs)
    
    gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, 
       color='gray', alpha=0.5, linestyle='--')
    gl.top_labels = gl.right_labels = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    gl.xlabel_style = {'size': 22, 'color': 'black', 'weight': 'bold'}
    gl.ylabel_style = {'size': 22, 'color': 'black', 'weight': 'bold'}
    
    # Limit the extent of the map to a small longitude/latitude range.
    extent = [longMax, longMin, latMax, latMin]
    ax.set_extent(extent, crs=ccrs.PlateCarree())
    
    # Set the Stamen data zoom level
    ax.add_image(stamen_terrain, zoom)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2021-01-28
      • 2010-11-26
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多