UnicodeEncodeError during GeoDjango tutorial documentaion

25 views
Skip to first unread message

Benjamin Smith

unread,
Aug 26, 2018, 3:28:36 PM8/26/18
to geodjango
I am going through the GeoDjango tutorial from the official documentation.
  1. I have installed the required geospatial libraries, installed and enabled spatial functionality by extending the PostgreSQL via postgis.
  2. Downloaded the world borders data, and have unzipped the data.
  3. Created a GeoDjango model (WorldBorder) in the world app.
  4. Created the load.py inside the world app, as instructed in the documentation.
But when I try to import the load module and call the run routine, I am getting an error:

  Failed to save the feature (id: 141) into the model with the keyword arguments:
  Traceback (most recent call last):
    File "/home/vagrant/.virtualenvs/testenv/lib/python3.6/site-packages/django/contrib/gis/utils/layermapping.py", line 577, in _save
      stream.write('%s: %s\n' % ('Updated' if is_update else 'Saved', m))
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)
  During handling of the above exception, another exception occurred:
  Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "/vagrant/geo_project/world/load.py", line 30, in run
      lm.save(strict=True, verbose=verbose)
    File "/home/vagrant/.virtualenvs/testenv/lib/python3.6/site-packages/django/contrib/gis/utils/layermapping.py", line 627, in save
      _save()
    File "/usr/lib/python3.6/contextlib.py", line 52, in inner
      return func(*args, **kwds)
    File "/home/vagrant/.virtualenvs/testenv/lib/python3.6/site-packages/django/contrib/gis/utils/layermapping.py", line 586, in _save
      stream.write('%s\n' % kwargs)
  UnicodeEncodeError: 'ascii' codec can't encode character '\xc3' in position 62: ordinal not in range(128)
 

I am using these version:

gdal-bin v2.2.3+dfsg-2

Python v3.6.5
Django v2.1
psycopg2 v2.7.5
postgis v2.4.3+dfsg-4

I cannot figure out where I am wrong. Could you please help me out? Thank you.

Claude Paroz

unread,
Aug 27, 2018, 6:34:13 AM8/27/18
to geod...@googlegroups.com, Benjamin Smith
Hi Benjamin,

Le 26. 08. 18 à 13:04, Benjamin Smith a écrit :
> I am going through the GeoDjango tutorial from the official documentation.
>
...

> But when I try to import the *load* module and call the *run* routine, I
> am getting an error:

This is most probably an error in the tutorial. Recent versions of GDAL
seem to automatically convert shapefile string encoding to unicode, so
the `encoding=iso-8859-1' parameter of the LayerMapping should be left out.
Could you confirm?

Claude
--
www.2xlibre.net

Claude

unread,
Aug 28, 2018, 1:46:28 PM8/28/18
to geodjango

To close the thread, Benjamin used the solution in this thread (https://groups.google.com/forum/#%21topic/geodjango/Imkq2DDI7qg) to solve his issue.

Nevertheless, I committed this fix in the documentation:
https://github.com/django/django/commit/388165ade

Claude
Reply all
Reply to author
Forward
0 new messages