【问题标题】:OverflowError in cartopycartopy中的溢出错误
【发布时间】:2015-09-23 17:01:08
【问题描述】:

我正在使用 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 是一个 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 commitsome mailing list 上提出的类似问题有关。

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

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

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

【问题讨论】:

    标签: python shapely cartopy


    【解决方案1】:

    我对发生的事情是否正确?

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

    如果是这样,为什么在 64 位系统上仍然出现这些错误?

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

    有没有办法解决它?

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

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

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

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

    HTH

    【讨论】:

    • 是的,我使用的是 64 位 Python。我会在跟踪器上提出一个问题。
    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多