I have a GeoDjango model:
class RouteLeg(models.Model):
"""Models one 'leg' of a route. A leg is one linestring
and belongs to a route."""
# the route which owns it
route = models.ForeignKey(Route)
# A line which represents this leg of the route
line = models.LineStringField()
def get_line(self):
return self.line
I save a RouteLeg
with a line string that looks like this:
LINESTRING (-3.1448364257812500 54.5469778189498768, -2.9031372070312500 54.4233245371087477)
And calling .save()
does not cause any errors. When I go to retrieve the routeleg
print route.routeleg_set.all()[0].get_line()
I get:
DjangoUnicodeDecodeError at /route/1
`'utf8' codec can't decode byte 0xa0 in position 17: invalid start byte.
You passed in '\x00\x00\x00\x00\x01\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xa0(\t\xc0y\x03\x82^\x03FK@\x00\x00\x00\x00\xa09\x07\xc0\xfb<\x99\x7f/6`K@'
(<type 'str'>)
I've trawled SO for answers, most relate to template encoding. I believe that this error is specific to the database. I have tried clear-ing and syncdb-ing the DB, which is MySQL.
Edit This turned out to be a trick question. The problem turned out to be with Shapely. I was using the shapely.wkt.dumps function.
from shapely.geometry import shape
from shapely.wkt import dumps
s = shape(geoJson)
wkt = str(dumps(s))
But dumps()
returns some thing that looks like a string, saved to the MySQL db okay, but could not be retrieved. The working code was:
from shapely.geometry import shape
s = shape(geoJson)
return s.wkt