Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeometryField containing an empty Point geometry #168

Closed
kordian-kowalski opened this issue Aug 8, 2018 · 3 comments · Fixed by #211
Closed

GeometryField containing an empty Point geometry #168

kordian-kowalski opened this issue Aug 8, 2018 · 3 comments · Fixed by #211
Labels

Comments

@kordian-kowalski
Copy link

When trying to serialize a GeometryField that contains an empty Point, the serialization fails with the following trace:

File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
  527.                 ret[field.field_name] = field.to_representation(attribute)

File "/usr/local/lib/python3.6/site-packages/rest_framework_gis/fields.py" in to_representation
  31.         geojson = GeoJsonDict(value.geojson)

File "/usr/local/lib/python3.6/site-packages/rest_framework_gis/fields.py" in __init__
  126.         super(GeoJsonDict, self).__init__(*args, **kwargs)

Here's the geometry Point object in question:

>>> geometry
<Point object at 0x7f2da4334450>
>>> geometry.wkt
'POINT EMPTY'
>>> geometry.json

Using

django==2.0.7
djangorestframework==3.8.2
djangorestframework-gis==0.13.0

How to reproduce

from django.contrib.gis.geos import Point
from rest_framework.serializers import Serializer
from rest_framework_gis.fields import GeometryField

class MySerializer(Serializer):
    geometry = GeometryField()

p = Point()
instance = {'geometry': p}
my_serializer = MySerializer()

my_serializer.to_representation(instance)
@nemesifier
Copy link
Member

@kordian-kowalski what would you expect instead of the exception? What's the correct behaviour according to the GeoJSON spec?

@atb00ker
Copy link
Member

A GeoJSON Geometry object of any type other than
"GeometryCollection" has a member with the name "coordinates".
The value of the "coordinates" member is an array.  The structure
of the elements in this array is determined by the type of
geometry. GeoJSON processors MAY interpret Geometry objects with
empty "coordinates" arrays as null objects.

Source: RFC #7946 - Section 3.1.

@nemesifier
Copy link
Member

Thanks @atb00ker, so that means GeometryField should return [] instead of crashing if my understanding is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants