Search code examples
pythongisgeodjangogeosdjango-rest-framework-gis

django-rest-framework-gis GeoFeatureModelSerializer returning altered/incorrect coordinates upon serialization


Currently, I have a GeometryField, which holds a Polygon, which is a GEOSGeometry. I print the coordinates of the polygon, and they seem fine, right where I specified. Then, I save the instance of the model, and then deserialize with the GeoFeatureModelSerializer, only to find out that my polygon's coordinates have been changed to something very small and close to the equator.

This is the GEOSGeometry stored in the GeometryField initially that gets stored in the database.

POLYGON ((-79.94751781225206 40.44287206073545, 
          -79.94751781225206 40.44385187931003, 
          -79.94502872228624 40.44385187931003, 
          -79.94502872228624 40.44287206073545, 
          -79.94751781225206 40.44287206073545))

This is after that is serialized with the GeoFeatureModelSerializer and returned.

[[-0.000718176362453, 0.000363293553554], 
 [-0.000718176362453, 0.000363316438548], 
 [-0.000718135112337, 0.000363316438548], 
 [-0.000718135112337, 0.000363293553554], 
 [-0.000718176362453, 0.000363293553554]]

I have no idea what could be causing this.

Thanks a lot in advance.


Solution

  • This was resolved by specifying the SRID. According to the Django docs, the SRID is

    Choosing an appropriate SRID for your model is an important decision that the developer should consider carefully. The SRID is an integer specifier that corresponds to the projection system that will be used to interpret the data in the spatial database. (https://docs.djangoproject.com/en/2.0/ref/contrib/gis/model-api/)

    I performing operations on polygons with a particular SRID and returning another polygon with a different SRID. I simply had to 'cast' the polygon I was returning to the SRID I wanted, with GEOSGeometry(polygon, srid=some_value). Basically, the polygon I was returning was being projected to some other format that I didn't want.