我有一些 SQL Server 2017 表,我想使用 LeafletJS 在地图上显示点、线和区域。为此,我需要存储为 JSON 文件。
最新的 SQL Server 具有执行此操作的命令:
对于积分:
select 'Feature' AS type, 'Point' as [geometry.type], JSON_QUERY (FORMATMESSAGE('[%s,%s]', FORMAT(WGS84Loc.Long, N'0.#####'), FORMAT(WGS84Loc.Lat, N'0.#####'))) as [geometry.coordinates], rtrim(TargetName) as [properties.name] FROM Targets FOR JSON PATH
对于线路:
select 'Feature' AS type, 'LineString' as [geometry.type], JSON_QUERY (FORMATMESSAGE('[[%s,%s],[%s,%s]]', FORMAT(WGS84Loc.STPointN(1).Long, N'0.#####'), FORMAT(WGS84Loc.STPointN(1).Lat, N'0.#####'),FORMAT(WGS84Loc.STPointN(2).Long, N'0.#####'), FORMAT(WGS84Loc.STPointN(2).Lat, N'0.#####'))) as [geometry.coordinates], ID as [properties.id], rtrim(Name) as [properties.name] FROM TrailLines Where VDateTime > '2017-09-06 00:00:00' FOR JSON PATH
对于区域/多边形:
select 'Feature' AS type, 'Polygon' as [geometry.type], JSON_QUERY (?????????) as [geometry.coordinates], rtrim(Name) as [properties.name] FROM Regions FOR JSON PATH
前两个工作正常,但最后一个让我难过。我有一个包含多边形的标准地理字段,但我无法弄清楚 JSON_QUERY 获取多边形所有顶点的坐标并将它们放入列表的语法。我认为这可能与 [WGS84Loc].STAsText 有关,但无法正确处理。
有人知道正确的 SQL 语法吗?