1

I'd like to get the nearest feature to a given point in Spatialite using a spatial SQL query. I'd like to speed it up using index table. Spatial index boundary should be calculated from defined point and a given tolerance, all features that are totally/partly within rtree bounding box should be used in the query.

I have tried several way, but I had always problem with spatial index boundary.

Like this:

SELECT *, Distance(GeomFromText('POINT(19.02658 47.51574)'),mo_utak_polyline.Geometry) as distance FROM mo_utak_polyline WHERE ROWID IN (SELECT pkid FROM idx_mo_utak_polyline_Geometry WHERE xmin > 19.01408 AND xmax < 19.03908 AND ymin > 47.50324 AND ymax < 47.52824) AND distance <=0.0025) ORDER by distance

Or this:

SELECT *,Intersects(GeomFromText('POINT(19.02658 47.51574)'), megyehatar_region.Geometry) as intersects FROM megyehatar_region WHERE ROWID IN (SELECT pkid FROM idx_megyehatar_region_Geometry WHERE xmin > 14.02658 AND xmax < 24.02658 AND ymin > 42.51574 AND ymax < 52.51574) AND intersects=1

I can always get features that are totally contained by my index defined bounding box. It causes a huge problem for me, e.g. when I try to query line features, when their length is totally different, it can be 1 cm or even 1000 km, so it is hard to set the size of spatial index bounding box.

Which do you think the best way to do it?

How to change this part of the query

SELECT pkid FROM idx_mo_utak_polyline_Geometry WHERE xmin > 19.01408 AND xmax < 19.03908 AND ymin > 47.50324 AND ymax < 47.52824)

to return not only the features that are contained by the bounding box, but also those intersects with it?

Thanks in advance!

4

4 回答 4

0

以下情况如何:

SELECT count(*) FROM idx_mo_utak_polyline_Geometry WHERE 
MBRContains(BuildMBR('19.01408','47.50324' , '19.03908', '47.52824'), BuildMBR(xmin,ymin , xmax, ymax)) OR
MBRIntersects(BuildMBR('19.01408','47.50324' , '19.03908', '47.52824'), BuildMBR(xmin,ymin , xmax, ymax)) 
于 2012-02-13T09:27:47.097 回答
0

对索引进行第一次查询,而不是你可以做的intersects,甚至更好,距离 == 0到你从索引查询中获得的几何:

您的点坐标 - X,Y

    SELECT * FROM table 
        WHERE pk_uid IN (SELECT pkid FROM idx_table_geometry 
                         WHERE xmin < X AND ymin < Y AND xmax > X AND ymax > Y) 
              AND distance( makepoint(X,Y), geometry ) == 0
于 2013-09-04T15:53:46.567 回答
0

在 SpatiaLite 4.4 或更高版本中,现在有一个运行良好的 KNN(K-Nearest Neighbors)索引。我最近写了一个查询,它为大约 500 个点中的每一个点从 500 万条线记录中查找最近的线串。使用 KaeptnHaddock 提到的较新的 VirtualSpatialIndex 方法,查询运行了大约 3 分钟。使用新的 KNN 索引,查询耗时不到 20 秒。这是我的 KNN 查询:

select k.* from knn k, points p
WHERE f_table_name = 'linestrings' 
AND ref_geometry = p.geometry
AND max_items = 1;
于 2017-02-07T14:40:45.507 回答
0

这个线程有点老了(与此同时,空间索引的处理似乎在 spatialite 中发生了一些变化),但这是我刚刚在我的项目中提出的内容(2015 年)。查询应该从点层中获取每个点,并从线层中找到最近的线。

我不确定这段代码的结构有多好,以及它对大型数据集的计算速度有多快(我的测试集很小)。

如果您看到此查询有所改进,我对获得反馈非常感兴趣(我的 sql 非常生锈 - 已经有一段时间了......)

select * 
from pointlayer as p
left join linelayer as l on
    Distance(p.geometry, l.geometry) in (
       select MIN(Distance(p.geometry, geometry)) 
       from linelayer
       where pk in (
           select rowid
           from SpatialIndex
           where f_table_name = 'linelayer'
           and search_frame = BuildCircleMbr(X(b.geometry), Y(b.geometry),25))
)
于 2015-07-28T06:24:48.303 回答