使用 GeoDjango 和 OSMGeoAdmin 进行纬度经度混淆

Latitude longitude confusion using GeoDjango and OSMGeoAdmin

我正在尝试使用 OSMGeoAdmin 地图小部件存储多边形,并且我已经声明我的管理员 class 如下:

class ProjectAdmin(OSMGeoAdmin):
    default_lon = 4600000
    default_lat = 180000
    default_zoom = 4

这是我的几何模型基础 class:

class AbstractGeometryModel(models.Model):
    class Meta:
        abstract = True

    geometry = models.MultiPolygonField()

    @property
    def boundary(self):
        boundary = self.geometry.boundary[0]
        return [[x[1], x[0]] for x in boundary]

    @property
    def bbox(self):
        box = self.geometry.extent
        return [[box[1], box[0]], [box[3], box[2]]]

    @property
    def centroid(self):
        return [self.geometry.centroid[1], self.geometry.centroid[0]]

如 Django 文档中所述,默认 SRID 应为 4326,正如我亲自检查的那样,当您获得几何体的范围时,它的格式为 [lat, lon, lat, lon],我需要它们为 [=16] =] 所以我写了那些 属性 方法。

问题是,每当我在新的生产环境中部署我的服务或在本地开发环境中从头开始启动它时,我都会看到 lat, lon 以反向格式存储。

为了检查这个问题,我决定将生产数据库与本地开发数据库进行比较,结果如下:

>>> from django.contrib.gis.geos import GEOSGeometry
>>> production_data = GEOSGeometry('0106...(Geometry copied from production db)...1532')
>>> production_data.extent
(17.2922570380823, 44.879874515170165, 18.00644604063442, 46.02245264001086)
>>> production_data.srid
4326
>>> development_data = GEOSGeometry('0106...(Geometry copied from development db)...1532')
>>> development_data.extent
(44.75627832378126, 17.887728306166515, 46.09661035484472, 18.10414680358874)
>>> development_data.srid
4326

如您所见,SRID 相同,但数据在生产数据库中存储为 lat, lon,在本地数据库中也存储为 lon, lat

我尝试调试以了解发生了什么,但没有成功。如果有人可以帮助我解决这个问题,我将不胜感激。 谢谢。

更新 我还在服务器和本地 python 控制台中 运行 下面的代码:

本地:

Python 3.8.3rc1 (default, May 10 2020, 12:11:09) 
[GCC 7.4.0] on linux
Django 2.2.4
>>> from django.contrib.gis.geos import GEOSGeometry
>>> a = GEOSGeometry('SRID=3857;MULTIPOLYGON(((..lots of geo numbers...)))')
>>> a.transofrm(4326)
>>> a.extent
(34.109070627613065, 46.87535791416505, 36.24604033684087, 53.423209475753794)

服务器:

root@fdfd7ccf489b:/code# python manage.py shell
Python 3.7.6 (default, Feb 26 2020, 15:34:58) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.contrib.gis.geos import GEOSGeometry                                                                                                                                                                                      

In [2]: a = GEOSGeometry('SRID=3857;MULTIPOLYGON(((..lots of geo numbers...)))')                       

In [3]: a.extent                                                                                                                                                                                                                              
Out[3]: (5218140.9737573, 4043456.9660937, 5947044.4753833, 4334529.1697632)

In [4]: a.transform(4326)                                                                                                                                                                                                                     

In [5]: a.extent                                                                                                                                                                                                                              
Out[5]: (46.875357914165036, 34.10907062761305, 53.42320947575378, 36.246040336840856)

看来问题与 GeoDjango 版本或配置有关...

问题的根源似乎是在生产环境 (GDAL 2.4.0) 和本地 (GDAL 3.0.1) 环境中安装的不同版本的 GDAL。

https://code.djangoproject.com/ticket/31695#no1