gpt4 book ai didi

Python 参数中遇到无效类型

转载 作者:行者123 更新时间:2023-11-30 23:04:31 25 4
gpt4 key购买 nike

如何正确地将 shapefile 转换为 MultiPolygon?尝试将 shapefile 转换为 MultiPolygon 时出现以下错误...

.401 395951.4, 238087.5 395958.5, 238086.5 395963, 238086.3 395968.2, 238087.3 395973, 238090.7 395980.6, 238094.2 395989.2, 238101 395993.2, 238107.7 395994.1, 238114 395993.8, 238121.3 395992.6, 238131.6 395988.6, 238146.9 395978.2, 238184.401 395960.9, 238196.6 395958.4, 238203.1 395956.801, 238214.599 395953.4)))
Traceback (most recent call last):
File "geo.py", line 25, in <module>
Multi = MultiPolygon([pol['geometry'] for pol in fiona.open(file)])
File "/Users/user/Documents/workspace/env/lib/python3.4/site-packages/django/contrib/gis/geos/collections.py", line 38, in __init__
self._check_allowed(init_geoms)
File "/Users/user/Documents/workspace/env/lib/python3.4/site-packages/django/contrib/gis/geos/mutable_list.py", line 261, in _check_allowed
raise TypeError('Invalid type encountered in the arguments.')
file = 'NUTS1_DEC_2008_EW_BFE.shp'

生成错误的代码:

from shapely.geometry import shape
c = fiona.open(file)
pol = c.next()
geom = shape(pol['geometry'])

Multi = MultiPolygon([pol['geometry'] for pol in fiona.open(file)])

最佳答案

MultiPolygon对象继承自 GeometryCollection .
寻找GeometryCollection.__ini__()方法你可以看到有一个对 self._check_allowed() 的调用方法。 self._check_allowed()来自GEOSGeometryListMixin类,那就是_check_allowed() ID defined :

...
def _check_allowed(self, items):
if hasattr(self, '_allowed'):
if False in [isinstance(val, self._allowed) for val in items]:
raise TypeError('Invalid type encountered in the arguments.')
...

如您所见,上述方法检查每个项目是否是以下任何类型的实例:

Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon

可以在 django/django/contrib/gis/geos/collections.py 中找到它,行 #108 :

# Setting the allowed types here since GeometryCollection is defined before
# its subclasses.
GeometryCollection._allowed = (Point, LineString, LinearRing, Polygon,
MultiPoint, MultiLineString, MultiPolygon)

也就是说,可能是 fiona.open()不返回任何有效的允许类型。我不是 GeoDjango 专家,但使用 django.contrib.gis.utils.LayerMapping 让我印象深刻就可以了。从 GeoDjango 文档中阅读教程似乎相当简单:GeoDjango Tutorial

关于Python 参数中遇到无效类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33671261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com