我正在使用以下内容构建一个包含地理数据的数据库:PostgreSQL 9.1,Django 1.3.1,psycopg2 2.4.5以及使用库GEOS 3.3.0,PostGIS 1.5.4和ProJ 4.7.0。
我有一个命令,可以通过一些优化将形状文件加载到数据库中,使形状更简单。
为了保持模型简单,我将只提出有问题的字段:
class Circonscription(models.Model):
shape = models.GeometryField()
simple_shape = models.GeometryField()
centroid = models.PointField(null=True)
当我想从形状文件创建一个时,我这样做:
Circonscription.objects.create(
id_cep=feature['id_cep'],
co_cep=feature['co_cep'],
nm_cep=feature['nm_cep'],
nmtri_cep=feature['nmtri_cep'],
dh_maj=feature['dh_maj'],
shape=geometry.wkt,
simple_shape=simple_geometry.wkt,
centroid=geometry.geos.centroid)
现在,当我运行我的命令时,它会因此错误而失败:
File "/home/pg/Projets/python/myproject/circonscriptions/management/commands/loadshapefiles.py", line 174, in add_boundaries_for_layer
centroid=geometry.geos.centroid)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/manager.py", line 138, in create
return self.get_query_set().create(**kwargs)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/query.py", line 360, in create
obj.save(force_insert=True, using=self.db)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 543, in save_base
for f in meta.local_fields if not isinstance(f, AutoField)]
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 786, in get_db_prep_save
return connection.ops.value_to_db_decimal(self.to_python(value),
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 761, in to_python
return decimal.Decimal(value)
File "/usr/lib/python2.7/decimal.py", line 658, in __new__
raise TypeError("Cannot convert %r to Decimal" % value)
TypeError: Cannot convert <django.contrib.gis.gdal.field.OFTInteger object at 0x2458b50> to Decimal
有人对此有所了解吗?
答案 0 :(得分:1)
它以某种方式与值的编码有关,然后另一个错误是关于this problem,最终解决了它。 在将值传递给要保存的模型之前,请务必将值转换为正确的编码(utf-8)。
答案 1 :(得分:0)
Decimal需要一个String,但是你正在为它提供一个OTFInteger,你需要在进程的某个地方将它转换为字符串