def to_python(self, value):
if not value or isinstance(value, BaseGeometry):<EOL><INDENT>return value<EOL><DEDENT>geometry_type = value['<STR_LIT:type>']<EOL>geometry = value['<STR_LIT>']<EOL>try:<EOL><INDENT>if geometry_type == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:x>' in geometry:<EOL><INDENT>data = json.loads(geometry)<EOL>x, y = data['<STR_LIT:x>'], data['<STR_LIT:y>']<EOL><DEDENT>else:<EOL><INDENT>x, y = [float(val) for val in geometry.split('<STR_LIT:U+002C>')]<EOL><DEDENT>return Point(x, y)<EOL><DEDENT>elif geometry_type == '<STR_LIT>':<EOL><INDENT>data = json.loads(geometry)<EOL>return MultiPoint([(p['<STR_LIT:0>'], p['<STR_LIT:1>']) for p in data['<STR_LIT>']])<EOL><DEDENT>elif geometry_type == '<STR_LIT>':<EOL><INDENT>data = json.loads(geometry)<EOL>return MultiLineString([((l[<NUM_LIT:0>][<NUM_LIT:0>], l[<NUM_LIT:0>][<NUM_LIT:1>]), (l[<NUM_LIT:1>][<NUM_LIT:0>], l[<NUM_LIT:1>][<NUM_LIT:1>])) for l in data['<STR_LIT>']])<EOL><DEDENT>elif geometry_type == '<STR_LIT>':<EOL><INDENT>data = json.loads(geometry)<EOL>rings = [LinearRing([(p[<NUM_LIT:0>], p[<NUM_LIT:1>]) for p in r]) for r in data['<STR_LIT>']]<EOL>return Polygon([r for r in rings if not r.is_ccw], interiors=[r for r in rings if r.is_ccw])<EOL><DEDENT>elif geometry_type == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT>' in geometry:<EOL><INDENT>data = json.loads(geometry)<EOL>xmin, ymin, xmax, ymax = [data[k] for k in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')]<EOL><DEDENT>else:<EOL><INDENT>xmin, ymin, xmax, ymax = [float(val) for val in geometry.split('<STR_LIT:U+002C>')]<EOL><DEDENT>return MultiPoint([(xmin, ymin), (xmax, ymax)]).envelope<EOL><DEDENT>else:<EOL><INDENT>raise ValueError<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>raise ValidationError('<STR_LIT>')<EOL><DEDENT>
This assumes the value has been preprocessed into a dictionary of the form:
{'type': <geometry_type>, 'geometry': <raw_geometry>}