invalid byte sequence for encoding when I try to save a POINT

421 views
Skip to first unread message

renooble_djangonaut

unread,
Feb 1, 2012, 6:01:05 AM2/1/12
to geodjango
Hi GeoDjango people,

I hope for your advice in regards to my general understanding of
GeoDjango.

For my first steps with GeoDjango, I have created the attribute
geolocation as part of my Project model class:

geolocation = models.PointField(_('Geo Location'),
null = True,
blank = True,
help_text=_('Geolocation with Longitude and
Latitude'))

When I save my project form, I want to save the project location with
the following statement:

Project.geolocation = fromstr('POINT(%s %s)' % (longitude,
latitude))

longitude and latitude are session variables and filled previously by
the user.

Django translates the Insert to PostgreSQL as follows (from the
logging option in the console):

INSERT INTO "project" ("address", "geolocation", "project_name")
VALUES ('São Paulo, Brazil', ST_GeomFromEWKB(E'\001\001\000\000
\346\020\000\000~\311\306\203-\350U\300\243s~
\212\343\360D@'::bytea), 'BrazilProject');

But when Django (or myself in the console) try to run the command, I
receive the error message that the information for the point field
would contain an invalid byte sequence.

ERROR: invalid byte sequence for encoding "UTF8": 0x00

I do not understand why. All steps are following the intro tutorials
for GeoDjango.
Can anyone of you give me an advice why I am running into the postgis
error?

Thank you for your help,
Martin

Vlado

unread,
Feb 1, 2012, 1:10:05 PM2/1/12
to geodjango
I assume you're using Postgres 9.0 or higher?

If that is the case there is a simple solution.
There is something like a bug in django's module "adapter.py", that
can't resolve data input in the spatially enabled models, when using
postgres 9.0 or above.
You just need to replace its code with the following:

"""
This object provides quoting for GEOS geometries into PostgreSQL/
PostGIS.
"""

from psycopg2 import Binary
from psycopg2.extensions import ISQLQuote

class PostGISAdapter(object):
def __init__(self, geom):
"Initializes on the geometry."
# Getting the WKB (in string form, to allow easy pickling of
# the adaptor) and the SRID from the geometry.
self.ewkb = str(geom.ewkb)
self.srid = geom.srid
self._adapter = Binary(self.ewkb)

def __conform__(self, proto):
# Does the given protocol conform to what Psycopg2 expects?
if proto == ISQLQuote:
return self
else:
raise Exception('Error implementing psycopg2 protocol. Is
psycopg2 installed?')

def __eq__(self, other):
return (self.ewkb == other.ewkb) and (self.srid == other.srid)

def __str__(self):
return self.getquoted()

def prepare(self, conn):
# Pass the connection to the adapter: this allows escaping the
binary
# in the style required by the server's
standard_conforming_string setting.
self._adapter.prepare(conn)

def getquoted(self):
"Returns a properly quoted string for use in PostgreSQL/
PostGIS."
# psycopg will figure out whether to use E'\\000' or '\000'
return 'ST_GeomFromEWKB(%s)' % self._adapter.getquoted()

def prepare_database_save(self, unused):
return self


and it will work.

The file is in the: ~django\contrib\gis\db\backends\postgis

Don't change other adapter.py files.

On 1 феб, 12:01, renooble_djangonaut <renooble.djangon...@gmail.com>
wrote:

jpk

unread,
Feb 2, 2012, 2:32:07 AM2/2/12
to geod...@googlegroups.com
I've run into the same thing, but worked around it in a different way.  In /etc/postgresql/9.1/main/postgresql.conf (in ubuntu, other distributions might put configuration in different places), if you set "standard_conforming_strings=off", you won't get this error.  (In 9.0 and later, this defaults to "on", whereas in the 8.x series and before, it was "off".)

I've been using that without issue thus far.  The reason I work around it this way is so I don't have to baby sit the code in adapter.py that might get overwritten by the package manager on an update.

--
You received this message because you are subscribed to the Google Groups "geodjango" group.
To post to this group, send email to geod...@googlegroups.com.
To unsubscribe from this group, send email to geodjango+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/geodjango?hl=en.




--
john p. kiffmeyer
Reply all
Reply to author
Forward
0 new messages