openwisp / django-rest-framework-gis

Geographic add-ons for Django REST Framework. Maintained by the OpenWISP Project.
http://openwisp.org
MIT License
1.08k stars 200 forks source link

GeoFeatureModelSerializer flips lat with long #249

Closed KostasTok closed 3 years ago

KostasTok commented 3 years ago

Hello,

I want to use GeoFeatureModelSerializer to return a GeoJson. The format of the response (see view below) is correct, however the coordinates are flipped, i.e. the latitude is longitude and visa versa.

In the django admin app the polygons appear correctly stored in the database, hence I think the issue is with the serialiser.

Do you think that this is a bug, or a mistake from my side?

My setup is the following:

# in settings.py
INSTALLED_APPS = [...,  'rest_framework',  'rest_framework_gis',  'django.contrib.gis']

# In models.py
from django.contrib.gis.db import models
class Regions(models.Model):
    code = models.CharField(max_length=2, primary_key=True)
    name = models.CharField(max_length=30)
    poly = models.MultiPolygonField(srid=4326)

# In serializers.py
from rest_framework_gis.serializers import GeoFeatureModelSerializer
class RegionsSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = Regions
        geo_field = 'poly'
        fields = ('code', 'name',)

# In view.py
class RegionsGet(viewsets.ModelViewSet):
    queryset = Regions.objects.all()
    serializer_class = RegionsSerializer

# In urls.py
app_name = 'polygons'
urlpatterns = [path('regions/', RegionsGet.as_view({'get': 'list'})),]
KostasTok commented 3 years ago

So I changed the way I import the data in the database and now the serialiser works correctly, but the lat/long are flipped in the admin of the django. This seams to be a bug with the django admin after all, so I will close this issue.

nemesifier commented 3 years ago

Thanks for updating the issue @KostasTok :+1: