Skip to main content
 首页 » 编程设计

python之cartopy 中的 OverflowError

2025年05月04日36exmyth

我正在使用 cartopy 绘制一些 map 。在某些情况下,在我的轴上调用 .set_extent() 时,出现此错误:

Traceback (most recent call last): 
  File "<pyshell#315>", line 1, in <module> 
    ax.set_extent([bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()], cartopy.crs.AlbersEqualArea()) 
  File "C:\FakeProgs\Python27\lib\site-packages\cartopy\mpl\geoaxes.py", line 587, in set_extent 
    projected = self.projection.project_geometry(domain_in_crs, crs) 
  File "C:\FakeProgs\Python27\lib\site-packages\cartopy\crs.py", line 172, in project_geometry 
    return getattr(self, method_name)(geometry, src_crs) 
  File "C:\FakeProgs\Python27\lib\site-packages\cartopy\crs.py", line 178, in _project_line_string 
    return cartopy.trace.project_linear(geometry, src_crs, self) 
  File "lib\cartopy\trace.pyx", line 109, in cartopy.trace.project_linear (lib/cartopy\trace.cpp:1135) 
  File "lib\cartopy\trace.pyx", line 71, in cartopy.trace.geos_from_shapely (lib/cartopy\trace.cpp:838) 
OverflowError: Python int too large to convert to C long 

问题是行为有点随机。并非每次调用 .set_extent() 都会这样做。这是一个解释器 session 的摘录(bounds 是一个 pandas DataFrame,它保存各种形状的边界框的坐标,我打算稍后添加到轴上)。

>>> ax = pyplot.axes(projection=cartopy.crs.AlbersEqualArea()) 
... ax.set_extent([bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()], cartopy.crs.AlbersEqualArea()) 
# result is exception shown above 
>>> [bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()] 
[-2218681.0391451684, 
 -2103178.2838086924, 
 -195096.93292225525, 
 7468.2970529943705] 
>>> [int(x) for x in [bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()]] 
[-2218681, -2103178, -195096, 7468] 
>>> [long(x) for x in [bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()]] 
[-2218681L, -2103178L, -195096L, 7468L] 
>>> ax = pyplot.axes(projection=cartopy.crs.AlbersEqualArea()) 
... ax.set_extent([bounds.X1.min(), bounds.X2.max(), bounds.Y1.min(), bounds.Y2.max()], cartopy.crs.AlbersEqualArea()) 
# works without problem! 

相同的代码可以运行,无需更改中间的任何变量。

错误似乎是由 trace,pyx 中的这一行引起的:

cdef ptr geos_geom = shapely_geom._geom 

我做了一些搜索并找到了 an old commit与在 some mailing list 上提出的类似问题相关.

我对这个问题的理解是,这些 Shapely 对象的 _geom 属性存储了某种指向某个 C 库中对象的指针。如果此指针的整数值对于 C long 来说太大,则会引发错误。该错误不可重现,因为每次我创建一个新的 GeoAxes 时都会创建一个新的 _geom,并且新的 _geom 可能会也可能不会太大。

但是,一件令人费解的事情是,我能找到的关于此错误的大部分信息(例如,上述提交中的提交消息)表明它应该只是 32 位系统的问题,但我正在使用64 位 Python 2.7,所有库的 64 位版本。

所以我的问题是:我对正在发生的事情是否正确?如果是这样,为什么我在 64 位系统上仍然会收到这些错误?有没有办法解决这个问题?

请您参考如下方法:

am I right about what is going on?

我不能断然确认你是对的,但它看起来确实有道理。我以前从未见过这个问题,但同样我也不倾向于定期在 Windows 上使用 cartopy。

If so, why am I still getting these errors on a 64-bit system?

您的机器可能是 64 位的,但您使用的 Python 是 64 位的吗?

And is there a way to work around it?

鉴于这似乎是随机的,解决方法可能是:

for attempt in range(10): 
    try: 
        ... 
    except OverflowError: 
        print('Failed attempt {}, retrying upto 10 times.'.format(attempt)) 

这当然不是很好,但可能是目前唯一的解决方法。

很明显你发现的是一个错误,所以我认为 cartopy issue tracker是找到问题的长期解决方案的正确位置。我认为提供您正在使用的软件版本是个好主意,理想情况下,您找到的坐标会触发问题(即使是随机的)。

HTH