问题描述
我有2个带有几何形状列的地理数据框,并将某些几何形状从1复制到另一个.
I have 2 geodataframes with a geometry column and I copy some geometries from 1 to the other.
这对多边形很有效,但是对于任何(有效)多多边形都返回ValueError.
This works well with Polygons but returns a ValueError for any (valid) MultiPolygon.
请咨询如何解决此问题?我不知道是否/如何/为什么要更改MultiPolygon以获取相等的len键和值"?
Please advice how to solve this? I don't know if / how / why I should change the MultiPolygon to get "equal len keys and value"?
geodata01.loc[index, 'geometry'] = geodata02.loc[index, 'geometry']
这对多边形很有效.只有使用MultiPolygons才能得到:
This works well with Polygons. Only with MultiPolygons I get:
ValueError
if len(labels) != len(value):
--> 611 raise ValueError('Must have equal len keys and value '
612 'when setting with an iterable')
我也不能对缓冲的或简化的MultiPolygon进行赋值(MultiPolygon有效,我可以绘制,缓冲,简化但不能进行赋值):
I also cannot do assignments of a buffered or simplified MultiPolygon either (the MultiPolygon is valid and I can plot, buffer, simplify but I cannot assign it):
geodata01.loc[index, 'geometry'] = geodata01.loc[index, 'geometry'].buffer(0)
#or
geodata01.loc[index, 'geometry'] = geodata01.loc[index, 'geometry'].simplify(tolerance=0)
这将返回相同的ValueError.
This returns the same ValueError.
推荐答案
原因是,pandas检查您要分配的值的长度,以查看它是否与您要分配的元素的数量(这里是单个元素,因为索引是标量)是否匹配.因此,我们可能需要在熊猫方面讨论如何处理.
"The reason is that pandas checks the length of the value you want to assign, to see if it matches with the number of elements you are assigning to (here a single element, since the index is a scalar).So we might need to discuss this on the pandas side how to deal with that.
作为一种解决方法,您可以分配一个元素的列表:
As a work-around, you can assign to a list of one element:
df.loc[[0], 'geometry'] = df.loc[[1], 'geometry'].values
请注意,我还使用列表来选择要分配的单个元素,然后执行.values,所以我基本上是在分配一个具有一个值的数组(问题是当不转换为数组时,pandas会尝试对齐索引,则分配有误). "
Note that I also use a list to select the single element I want to assign, but then do .values, so I am basically assigning an array of one value (the problem is when not converting to an array, pandas will try to align on the index, and the assignment goes wrong). "
再次感谢Joris提供了解决方法并创建了一个熊猫问题来解决此问题: REGR:指定长度的标量不再有效
Many thanks again to Joris who provided the workaround and created a pandas issue to resolve this: REGR: assigning scalar with a length no longer works
这篇关于Geopandas设置了几何图形:MultiPolygon的ValueError“相等的len键和值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!