我正在尝试使几何查询起作用。类似的地理查询工作正常,但我必须使用使用几何类型的表。尽管 Geography 版本按预期返回了许多记录,但我无法让 Geometry 版本返回任何记录。两张表都有完全相同的经纬度记录。
此地理查询工作正常:
DECLARE @home GEOGRAPHY
SET @home = GEOGRAPHY::STPointFromText('POINT(-0.7799193 51.3083162 )', 4326);
SELECT OutwardCode, InwardCode, Latitude, Longitude
FROM dbo.PostCodeData
WHERE GeoLocation.STDistance(@home) <= (5 * 1609) -- 1609 = approx metres in 1 mile
表架构是:
+-------------+--------------+
| Field | Type |
+-------------+--------------+
| OutwardCode | Varchar(4) |
| InwardCode | Varchar(3) |
| Latitude | Decimal(9,6) |
| Longitude | Decimal(9,6) |
| GeoLocation | Geography |
+-------------+--------------+
示例表数据:
+-------------+------------+------------+----------+------------------------------------------------+
| OutwardCode | InwardCode | Longitude | Latitude | GeoLocation |
+-------------+------------+------------+----------+------------------------------------------------+
| GU14 | 9HL | -0.7803759 | 51.30818 | 0xE6100000010C01A4367172A7494027C522E1D6F8E8BF |
+-------------+------------+------------+----------+------------------------------------------------+
此 Geometry 查询不返回任何记录(我在数据库中有完全相同的纬度和经度记录,但将 Geometry 作为街道的中心点,并且 Postcode 是 OutwardCode 和 InwardCode 的结合版本):
DECLARE @home GEOMETRY
SET @home = GEOMETRY::STPointFromText('POINT(51.3083162 -0.7799193)', 0);
SELECT Postcode, Latitude, Longitude
FROM dbo.OS_Locator
WHERE Centre.STDistance(@home) <= (5 * 1609) -- 1609 = approx metres in 1 mile
表架构是:
+-----------+--------------+
| Field | Type |
+-----------+--------------+
| Postcode | nvarchar(10) |
| Latitude | Decimal(9,6) |
| Longitude | Decimal(9,6) |
| Centre | Geometry |
+-----------+--------------+
示例表数据:
+----------+-----------+-----------+------------------------------------------------+
| Postcode | Latitude | Longitude | Centre |
+----------+-----------+-----------+------------------------------------------------+
| GU14 9HL | 51.308304 | -0.779928 | 0x346C0000010C00000000549C1D410000000018330341 |
+----------+-----------+-----------+------------------------------------------------+
我哪里错了?