【发布时间】: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 commit 与 some mailing list 上提出的类似问题有关。
我对这个问题的理解是,这些 Shapely 对象的 _geom 属性存储了某种指向某个 C 库中对象的指针。如果此指针的整数值对于 C long 来说太大,则会引发错误。该错误无法重现,因为每次我创建一个新的GeoAxes 时都会创建一个新的_geom,而新的_geom 可能会也可能不会太大。
不过,令人费解的是,我能找到的有关此错误的大部分信息(例如,上述提交中的提交消息)表明它应该只是 32 位系统的问题,但我正在使用64 位 Python 2.7 以及所有库的 64 位版本。
所以我的问题是:我对正在发生的事情是否正确?如果是这样,为什么在 64 位系统上仍然会出现这些错误?有没有办法解决它?
【问题讨论】: