在cartopy中的OverflowError

2024-04-24 05:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用cartopy画一些地图。在某些情况下,在我的轴上调用.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()的调用都能做到这一点。下面是一个解释器会话的摘录(bounds是一个数据帧,其中包含各种形状的边界框的坐标,我打算稍后添加到轴)。你知道吗

>>> 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 commitsome mailing list上提出的类似问题有关。你知道吗

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

不过,有一件令人费解的事情是,我所能找到的关于这个错误的大部分信息(例如,上面提交的commit消息)表明,这应该只是32位系统的一个问题,但我使用的是64位python2.7,它包含了所有库的64位版本。你知道吗

所以我的问题是:我说的对吗?如果是这样,为什么在64位系统上仍然会出现这些错误?有办法解决吗?你知道吗


Tags: inlib错误linetraceminextentmax
1条回答
网友
1楼 · 发布于 2024-04-24 05:12:14

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))

这当然不好看,但可能是目前唯一的解决办法。你知道吗

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

HTH公司

相关问题 更多 >