Django Geo访问不使用django shell的polygon的linearRing

时间:2014-05-06 08:05:55

标签: python django geos

我想像这样访问Polygon的LinearRing:

from django.contrib.gis.geos import Polygon
poly = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )
poly[0]

我在装有Django 1.6的virtualenv中工作(Mac osx mavericks,python 2.7.5) 当我在python解释器中写下这几行时,没问题:

<LinearRing object at 0x2761d00>

但是当我使用Django shell时,我收到了这个错误:

python manage.py shell
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.gis.geos import Polygon 
>>> poly = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )
>>> poly[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/xxx/.virtualenvs/rte-back/lib/python2.7/site-packages/django/contrib/gis/geos/geometry.py", line 141, in __repr__
     return '<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr)))
  File "/Users/xxx/.virtualenvs/rte-back/lib/python2.7/site-packages/django/contrib/gis/geos/geometry.py", line 224, in geom_type
    return capi.geos_type(self.ptr).decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

为什么结果不一样?

2 个答案:

答案 0 :(得分:1)

您是否已将django geo添加到安装应用程序中?

答案 1 :(得分:1)

您的settings.py应包含此字符串:

INSTALLED_APPS = (
    ...
    'django.contrib.gis',
    ...
)