我正在考虑使用django(2.0.6)和Postgres / postGIS作为后端(10 / 2.4?)。
但是,我打算使用的模型几何不会“反对”一个球体(无论是地球火星等)还是任何其他类型的SRID。而是3D笛卡尔坐标中的100%纯几何,其中1单位= 1米。
我如何声明模型字段并确保它们都是纯粹的几何图形:
这会有用吗?
geometry = models.MultiPolygonField(_('Geometry'), spatial_index=True, dim=3)
或者更好地设置SRID?:
geometry = models.MultiPolygonField(_('Geometry'), spatial_index=True, dim=3, srid=0)
非常感谢
答案 0 :(得分:0)
您可以将SRID 3857用于1米笛卡尔网格:
models.py
:
class Place(models.Model):
geom = models.PointField(srid=3857, dim=3)
def __str__(self):
return str(self.geom.coords)
但是,GeoDjango不支持开箱即用的3D距离计算。例如:
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance
from myapp.models import Place
p = Point(100, 100, 0)
Place.objects.create(geom=p)
Place.objects.create(geom=Point(150, 100, 0))
Place.objects.create(geom=Point(100, 100, 22))
Place.objects.create(geom=Point(100, 100, 500))
Place.objects.create(geom=Point(150, 150, 0))
Place.objects.create(geom=Point(250, 250, 0))
for o in Place.objects.filter(geom__distance_lte=(p, Distance(m=100))):
print(o.geom.coords, p.distance(o.geom))
输出:
(100.0, 100.0, 0.0) 0.0
(150.0, 100.0, 0.0) 50.0
(100.0, 100.0, 22.0) 0.0
(100.0, 100.0, 500.0) 0.0
(150.0, 150.0, 0.0) 70.71067811865476
在纯PostGIS中,比较ST_Distance
和ST_3DDistance
:
SELECT *
FROM (
SELECT
"myapp_place"."id",
ST_Distance("myapp_place"."geom",
ST_GeomFromEWKT('SRID=3857;POINT(100 100 0)')) d,
ST_AsEWKT("myapp_place"."geom")
FROM "myapp_place"
) t
WHERE d <= 100.0
VS
SELECT *
FROM (
SELECT
"myapp_place"."id",
ST_3DDistance("myapp_place"."geom",
ST_GeomFromEWKT('SRID=3857;POINT(100 100 0)')) d,
ST_AsEWKT("myapp_place"."geom")
FROM "myapp_place"
) t
WHERE d <= 100.0