Skip to main content
 首页 » 编程设计

python之如何在 GeoDjango 中使用子查询获取相交几何图形

2025年02月15日36txw1958

我正在尝试从一个表中获取所有记录,该表的几何形状与同一个表中的缓冲几何形状相交,这是我通过子查询获得的。

我的工作纯 SQL 语句是:

SELECT id FROM table  
WHERE ST_INTERSECTS(geom, ( 
    SELECT ST_BUFFER(geom, 10) FROM table WHERE id = 1) 
); 

如何使用 GeoDjango 实现这一目标?

编辑:

得到它与以下工作:

records = table.objects.filter( 
    geom__intersects = table.objects.filter(pk=1).values('geom') 
) 

但我仍然缺少缓冲区。
有什么建议吗?

请您参考如下方法:

经过一番研究,我通过以下方式解决了这个问题:

# First get the geometry i tried to get in the subquery 
tempgeom = table.objects.get(pk=1) 
# Apply the buffer 
tempgeom = tempgeom.geom.buffer(10) 
# Use the buffered geometry to get the records 
records = table.objects.filter(geom__intersects = tempgeom) 

我认为有更好的方法,但就目前而言,它有效。