如何在 sql 服务器 2014 中将经度和纬度存储为地理信息?
How to store longitude & latitude as a geography in sql server 2014?
我在经度和纬度坐标中有位置。我的目标是最终能够 select myTable 中距离小于 2km 的所有行。
如何使用经度和纬度在地理列中存储位置?(因为它应该只是一个地理点而不是两个,对吗?不是一个用于经度,一个用于纬度?)
现在我已经有了地理点,我怎样才能 select 特定距离内的所有行(在我的例子中是 2 公里)?
How can i use the longitute and latitute to store location within a geography column?(because it's supposed to be only one geographic point not two right? not one for longitute and one for latitute?)
您可以使用 geography::STPointFromText
/ geography::Point
以地理数据类型存储经度和纬度。
SELECT geography::STPointFromText('POINT(' + CAST([Longitude] AS VARCHAR(20)) + ' ' + CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
或
SELECT geography::Point(Latitude, Longitude , 4326)
引用Link:
Now that I've got the geography points, how can i select all the rows within a specific distance(in my case 2km)?
你可以这样使用STDistance
。
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34720 47.65100)', 4326);
SELECT @g.STDistance(@h);
引用Link:
Distance between two points using Geography datatype in sqlserver 2008?
插入查询
DECLARE @GeoTable TABLE
(
id int identity(1,1),
location geography
)
--Using geography::STGeomFromText
INSERT INTO @GeoTable
SELECT geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326)
--Using geography::Point
INSERT INTO @GeoTable
SELECT geography::Point(47.65100,-122.34720, 4326);
获取距离查询
DECLARE @DistanceFromPoint geography
SET @DistanceFromPoint = geography::STGeomFromText('POINT(-122.34150 47.65234)', 4326);
SELECT id,location.Lat Lat,location.Long Long,location.STDistance(@DistanceFromPoint) Distance
FROM @GeoTable;
以上回答@ughai的补充
添加一列
ALTER TABLE [dbo].[Landmark]
ADD [GeoLocation] GEOGRAPHY
GO
将经度和纬度转换为地理
UPDATE [dbo].[Landmark]
SET [GeoLocation] = geography::STPointFromText('POINT(' + CAST([Longitude]
AS VARCHAR(20)) + ' ' +
CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
GO
寻找半径为 2 公里的地点
DECLARE @Origin GEOGRAPHY,
-- distance defined in meters
@Distance INTEGER = 2000;
SET @Origin = GEOGRAPHY::STGeomFromText('POINT(17.482477 78.546871)', 4326);
-- return all rows from events in 2km radius
SELECT *,GeoLocation.STDistance(@Origin) Distance FROM dbo.Locations WHERE @Origin.STDistance(GeoLocation) <= @Distance;
它让我找到了距离内的地方
您可以将纬度和经度转换为点并将其保存在 table。
声明@geo地理,
@Lat varchar(10),
@long varchar(10)
SET @Lat = '34.738925'
SET @Long = '-92.39764'
SET @geo= geography::Point(@LAT, @LONG, 4326)
在存储过程和选择中使用 GeoPoints 时,总是计算 GeoPoint
变得很累,所以我发现创建一个计算列从 [=16] 生成 GeoPoint
很方便=] FLOAT
字段,当行被选中时。
[GeoPoint] AS ([geography]::Point([Latitude],[Longitude],(4326))) PERSISTED,
我在经度和纬度坐标中有位置。我的目标是最终能够 select myTable 中距离小于 2km 的所有行。
如何使用经度和纬度在地理列中存储位置?(因为它应该只是一个地理点而不是两个,对吗?不是一个用于经度,一个用于纬度?)
现在我已经有了地理点,我怎样才能 select 特定距离内的所有行(在我的例子中是 2 公里)?
How can i use the longitute and latitute to store location within a geography column?(because it's supposed to be only one geographic point not two right? not one for longitute and one for latitute?)
您可以使用 geography::STPointFromText
/ geography::Point
以地理数据类型存储经度和纬度。
SELECT geography::STPointFromText('POINT(' + CAST([Longitude] AS VARCHAR(20)) + ' ' + CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
或
SELECT geography::Point(Latitude, Longitude , 4326)
引用Link:
Now that I've got the geography points, how can i select all the rows within a specific distance(in my case 2km)?
你可以这样使用STDistance
。
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34720 47.65100)', 4326);
SELECT @g.STDistance(@h);
引用Link:
Distance between two points using Geography datatype in sqlserver 2008?
插入查询
DECLARE @GeoTable TABLE
(
id int identity(1,1),
location geography
)
--Using geography::STGeomFromText
INSERT INTO @GeoTable
SELECT geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326)
--Using geography::Point
INSERT INTO @GeoTable
SELECT geography::Point(47.65100,-122.34720, 4326);
获取距离查询
DECLARE @DistanceFromPoint geography
SET @DistanceFromPoint = geography::STGeomFromText('POINT(-122.34150 47.65234)', 4326);
SELECT id,location.Lat Lat,location.Long Long,location.STDistance(@DistanceFromPoint) Distance
FROM @GeoTable;
以上回答@ughai的补充
添加一列
ALTER TABLE [dbo].[Landmark]
ADD [GeoLocation] GEOGRAPHY
GO
将经度和纬度转换为地理
UPDATE [dbo].[Landmark]
SET [GeoLocation] = geography::STPointFromText('POINT(' + CAST([Longitude]
AS VARCHAR(20)) + ' ' +
CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
GO
寻找半径为 2 公里的地点
DECLARE @Origin GEOGRAPHY,
-- distance defined in meters
@Distance INTEGER = 2000;
SET @Origin = GEOGRAPHY::STGeomFromText('POINT(17.482477 78.546871)', 4326);
-- return all rows from events in 2km radius
SELECT *,GeoLocation.STDistance(@Origin) Distance FROM dbo.Locations WHERE @Origin.STDistance(GeoLocation) <= @Distance;
它让我找到了距离内的地方
您可以将纬度和经度转换为点并将其保存在 table。
声明@geo地理, @Lat varchar(10), @long varchar(10)
SET @Lat = '34.738925' SET @Long = '-92.39764'
SET @geo= geography::Point(@LAT, @LONG, 4326)
在存储过程和选择中使用 GeoPoints 时,总是计算 GeoPoint
变得很累,所以我发现创建一个计算列从 [=16] 生成 GeoPoint
很方便=] FLOAT
字段,当行被选中时。
[GeoPoint] AS ([geography]::Point([Latitude],[Longitude],(4326))) PERSISTED,