geolocation - All Lat Lng of a sql table within 15 Km to each Lat lng of a different table-sql 2008 -
sql 2008
i have 2 tables. 1 table (a) have around 4000 locations lat lng. table (b) having 800 locations lat lng.
i need each lat lng of table b corresponding lat lngs within 15 km of radius.
i using sql 2008 , new geographical queries.
/* assuming tables */ if object_id('#xlocation1') not null drop table #xlocation1 create table #xlocation1 ( id int identity(1,1) constraint pk_location_1 primary key--reqire geog spatial index ,locationid int ,latitude float null ,longitude float null ,radius int null ,geogpoint geography null ) if object_id('#xlocation2') not null drop table #xlocation2 create table #xlocation2 ( id int identity(1,1) constraint pk_location_2 primary key--reqire geog spatial index ,locationid int ,latitude float null ,longitude float null ,radius int null ,geogpoint geography null ) declare @radius int = 15 --km /* create geography point datatypes */ update #xlocation1 set geogpoint = geography::stgeomfromtext('point(' + cast(isnull(longitude,'') varchar(20)) + ' ' + cast(isnull(latitude,'') varchar(20)) + ')', 4326) update #xlocation2 set geogpoint = geography::stgeomfromtext('point(' + cast(isnull(longitude,'') varchar(20)) + ' ' + cast(isnull(latitude,'') varchar(20)) + ')', 4326) /* create spatial indexes */ create spatial index [sdx_location1_geogpoint_x1] on #xlocation1 ( [geogpoint] ) using geography_grid ( grids=(level_1 = high,level_2 = high,level_3 = high,level_4 = high) , cells_per_object = 64 , pad_index = off , sort_in_tempdb = off , drop_existing = off , allow_row_locks = on , allow_page_locks = on ) create spatial index [sdx_location2_geogpoint_x2] on #xlocation2 ( [geogpoint] ) using geography_grid ( grids=(level_1 = high,level_2 = high,level_3 = high,level_4 = high) , cells_per_object = 64 , pad_index = off , sort_in_tempdb = off , drop_existing = off , allow_row_locks = on , allow_page_locks = on ) /* find locations each table within @radius of each other */ select * #xlocation1 x inner join #xlocation2 p on x.geogpoint.stdistance(p.geogpoint) <= @radius
Comments
Post a Comment