Question

I'm building a database with geo data in it using : PostgreSQL 9.1, Django 1.3.1, psycopg2 2.4.5 and using libraries GEOS 3.3.0, PostGIS 1.5.4 and ProJ 4.7.0.

I have a command to load the shape files into the database with a bit of optimization to have the shape more simple.

To keep the model simple, i will just put the problematic fields:

class Circonscription(models.Model):
    shape = models.GeometryField()
    simple_shape = models.GeometryField()
    centroid = models.PointField(null=True)

When i want to create one from the shape file, i do this:

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)

Now when i run my command, it fails with this error:

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

Does anybody have a clue about it?

Was it helpful?

Solution

It somehow had to do with the encoding of the values and then another error was about this problem which finally solved it. Be sure to convert the values to the correct encoding (utf-8) before passing them to the model to save.

OTHER TIPS

Decimal expects a String, but you are feeding it an OTFInteger, you need to convert it to string somewhere in the process

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top