【问题标题】:Find nearest areas查找最近的区域
【发布时间】:2018-07-09 00:03:01
【问题描述】:

我有一个包含地理区域的数据库,每行包含以下内容,例如:

多边形((-9.001111 40.624981,-9.251389 40.64887,-9.368056 40.382203,-9.4425 40.199703,-9.118056 40.14887,-9.001111 40.624981)

多边形((-8.75 38.216667,-8.75 38.3,-8.85 38.3,-8.85 38.216667,-8.75 38.216667))

多边形((-8.935001 38.909424,-9.002501 38.944424,-9.083056 38.848591,-9.015556 38.813591,-8.935001 38.909424))

等等

我需要一个查询,通过提供纬度、经度和半径来返回最近的查询。

这是我到目前为止的结果,但结果并不如预期:

declare
@radius int = 200,
@latitude decimal = 37.016758,
@longitude decimal = -7.930886;

DECLARE @point geography;
SET @point = geography::Point(@latitude, @longitude, 4326);

SELECT b.idFeature, a.idFeatureGeometry, a.placemark, 
c.name, c.stroke, c.stroke_opacity, c.stroke_width, c.fill, c.fill_opacity
FROM FeaturesGeometries a
inner join Features b on a.idFeature = b.idFeature
inner join FeaturesProperties c on a.idFeature = c.idFeature
WHERE @point.STIntersects([placemark].STAsText()) <> 0;

关于如何改进查询的任何想法?

【问题讨论】:

    标签: sql-server geometry geospatial


    【解决方案1】:

    我不确定您要完成什么,但如果要查找某个点的设定半径内的所有功能,请尝试以下操作:

    declare @polygons table(polygon geography);
    insert @polygons values
        (geography::STGeomFromText('POLYGON ((-9.001111 40.624981, -9.251389 40.64887, -9.368056 40.382203, -9.4425 40.199703, -9.118056 40.14887, -9.001111 40.624981))', 4326)),
        (geography::STGeomFromText('POLYGON ((-8.75 38.216667, -8.75 38.3, -8.85 38.3, -8.85 38.216667, -8.75 38.216667))', 4326)),
        (geography::STGeomFromText('POLYGON ((-8.935001 38.909424, -9.002501 38.944424, -9.083056 38.848591, -9.015556 38.813591, -8.935001 38.909424))', 4326));
    
    declare @radius float = 200 /*kilometers*/ * 1000;
    
    declare @point geography = geography::Point(37.016758, -7.930886, 4326);
    
    select @point , polygon, @point .STDistance(polygon) FROM @polygons where @point.STDistance(polygon) <= @radius
    

    请记住,从 STDistance 返回的单位是米而不是公里。我假设您的意思是 @radius 是公里(而不是米),所以我适当地调整了代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      相关资源
      最近更新 更多