Get all points(circles with radius), that overlap given point

醉酒当歌 提交于 2019-12-10 11:12:46

问题


I need to find the best way for a "circles overlapping given point" system.

I have a lot of points e.g. restaurants, and every item has a point of the place, and a radius for e.g. "bringing out food". The radius differ, some have 3 km, others 10 km. I am searching from a point. e.g. "My position" latitude/longitude.

I need to find the best way to find all restaurants that overlaps my point. (Will bring food to me). (Not is point in circle, but what circles are overlapping my point.)

I am thinking of storing lat/lng as geography type in SQLServer 2008. Is this the way to do it?

Can I query directly on top of sqlserver? Or do I need to do it in code? And what is the way to do it?


回答1:


Yes, this is exactly the kind of thing that geography and spatial methods are good at. Here is a short example:

DECLARE @Restaurant TABLE (
    Name nvarchar(50),
    Location geography,
    DeliveryRadiusMetres int
);

INSERT @Restaurant
VALUES
-- long lat
('Dominos','POINT(-0.109339 51.532835)',2000 ),
('Pizza Hut','POINT(-0.102961 51.541157)',2000 );

Note that here to construct the geography values I am using an implicit conversion from string, which behind the scenes calls geography::Parse.

DECLARE @MyLocation geography = 'POINT(-0.115063 51.550231)';

SELECT
    Name
FROM
    @Restaurant R
WHERE
    R.Location.STDistance(@MyLocation) <= R.DeliveryRadiusMetres
;



回答2:


Yes you can draw your circles as points as a Geography / Geometry datatype. Then you can write SQL queries directly against this data with functions like STWithin and STTouches.

Set up your restaurant location as a point with a radius. Then you can write a query like this:

DECLARE @RestaurantCoverage Geometry
SET @RestaurantCoverage = 'Point(10 5)' --Location
SET @RestaurantCoverage = @RestaurantCoverage.STBuffer(5)     --Cover radius
DECLARE @YourLocation Geometry
SET @YourLocation = 'Point(13 5)' --Your location

SELECT @YourLocation.STWithin(@RestaurantCoverage)

http://technet.microsoft.com/en-us/library/bb933991.aspx

Also works directly against table data with where statement.



来源:https://stackoverflow.com/questions/12949605/get-all-pointscircles-with-radius-that-overlap-given-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!