1。创建数据表:
CREATE TABLE [dbo].[GpsTable](
[GEOID] [int] NULL,
[GEOCOL] [geography] NULL --geography是球面数据,geometry是几何数据
) ON [PRIMARY]
2。插入数据:
declare @i int
set @i=1
while @i<=200
begin
INSERT INTO GpsTable
([GEOID]
,[GEOCOL])
VALUES
(@i
,geography::Point(30+@i*0.1,80+@i*0.1, 4326)) --4326 WGS 84坐标
set @i=@i+1
end
GO
3.位置查找:
--查找坐标(30,80)周边距离30公里内的点
SELECT *
FROM gpstable
where geocol.STDistance(geography::Point(30,80, 4326)) < 30000
4.面查找:
--面查找,在(80 35,110 35,110 45,80 45,80 35)内的点
DECLARE @region geography
SET @region = Geography::STGeomFromText('POLYGON((80 35,110 35,110 45,80 45,80 35))', 4326);
select * from dbo.GpsTable where @region.STIntersects(GEOCOL)=1