【发布时间】:2020-07-21 01:31:32
【问题描述】:
当我尝试在 MultiPolygonField 上设置 Polygon 时,会引发以下异常:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/mattrowbum/.virtualenvs/my_env/lib/python3.7/site-packages/django/contrib/gis/db/models/proxy.py", line 75, in __set__
instance.__class__.__name__, gtype, type(value)))
TypeError: Cannot set Location SpatialProxy (MULTIPOLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>
这是可以理解的,除了GeoDjango tutorial 中的注释指出:
...GeoDjango MultiPolygonField 将接受多边形几何体。
我查看了source of proxy.py,它正在检查值(Polygon)是否是相关几何类(MultiPolygon)的实例。我已经尝试手动进行此检查,以确认 Polygon 不会从 MultiPolygon 继承:
>>> from django.contrib.gis.geos import MultiPolygon, Polygon
>>> ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
>>> int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))
>>> poly = Polygon(ext_coords, int_coords)
>>> isinstance(poly, Polygon)
True
>>> isinstance(poly, MultiPolygon)
False
我在尝试简化现有存储的MultiPolygon 值时注意到了这一点。下面是我的模型:
from django.contrib.gis.db import models
class Location(models.Model):
name = models.CharField(max_length=180)
mpoly = models.MultiPolygonField(geography=True, blank=True, null=True)
我用来简化MultiPolygon 的过程如下。最后一行导致引发异常:
>>> from my_app.models import Location
>>> location = Location.objects.get(pk=1)
>>> geom = location.mpoly
>>> simplified_geom = geom.simplify(0.0002)
>>> location.mpoly = simplified_geom
如果我使用Polygon 创建MultiPolygon,它可以正常工作:
>>> multi = MultiPolygon([simplified_geom,])
>>> location.mpoly = multi
教程中的注释是否具有误导性或我做错了什么?
编辑:对几何形状的进一步测试。
原MultiPolygon直接来自模型字段:
>>> geom = Location.mpoly
>>> type(geom)
<class 'django.contrib.gis.geos.collections.MultiPolygon'>
>>> geom.geom_type
'MultiPolygon'
>>> geom.valid
True
>>> geom.srid
4326
应用simplify() 方法:
>>> simplified_geom = geom.simplify(0.0002)
>>> type(simplified_geom)
<class 'django.contrib.gis.geos.polygon.Polygon'>
>>> simplified_geom.geom_type
'Polygon'
>>> simplified_geom.valid
True
>>> simplified_geom.srid
4326
从简化几何创建MultiPolygon:
>>> multi = MultiPolygon([simplified_geom,])
>>> type(multi)
<class 'django.contrib.gis.geos.collections.MultiPolygon'>
>>> multi.geom_type
'MultiPolygon'
>>> multi.valid
True
>>> multi.srid
>>> print(multi.srs)
None
请注意,上面的MultiPolygon 没有 SRID。我认为这可能是被接受的原因。我使用srid=4326 参数创建了一个,但它也被该领域接受。
这是一个非常基本的问题示例:
>>> # This works
>>> location.mpoly = MultiPolygon()
>>> # This doesn't
>>> location.mpoly = Polygon()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/mattrowbum/.virtualenvs/musicteacher/lib/python3.7/site-packages/django/contrib/gis/db/models/proxy.py", line 75, in __set__
instance.__class__.__name__, gtype, type(value)))
TypeError: Cannot set Location SpatialProxy (MULTIPOLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>
【问题讨论】:
-
您能分享一下
Model,更具体地说是MultiPolygonField的创作吗?你在那里使用geography=True选项吗? -
@JohnMoutafis - 我已经用模型声明和流程更新了问题。从那以后,我研究了LayerMapping 实用程序的来源,在这种情况下,如果提供了单个几何图形,它就会构造一个多几何图形。
-
是的,我以前见过,问题在于
geography=True设置和您在该字段中传递的多边形:stackoverflow.com/questions/55451675/…