LineString找到车辆通过我的2线(4分)

时间:2012-10-29 00:24:37

标签: sql-server-2008 tsql geometry geospatial spatial

我有一个任务,我必须在谷歌地图上绘制2行(4点)和提交事件我需要显示通过这些点的车辆。我可以在谷歌地图上绘制2行,这样就可以得到4点纬度/经度格式。

现在主要的问题是如何查询数据库以使车辆通过两行。我知道我可能不得不在T-SQL中使用LineString函数,但是如何让所有车辆通过这些线?欢迎任何建议。

1 个答案:

答案 0 :(得分:1)

鉴于我不确定你是如何代表你的“汽车”或你的“线”并且必须做出假设,这个代码示例可能会让你为你滚动。 它将返回一个数据集,指示哪些车辆通过了哪些线路。 空间查询不是我的强项;也许其他人可以提供优化。

--  This is the first line as describer by 2 points
DECLARE @line1  GEOMETRY = geometry::STGeomFromText('LINESTRING(0 10, 10 10)', 0)
--  This is the second line as describer by another 2 points
DECLARE @line2  GEOMETRY = geometry::STGeomFromText('LINESTRING(0 20, 10 20)', 0)
--  @Car1's path is represented as a line that does NOT intersect the 2 defined lines above
DECLARE @Car1   GEOMETRY = geometry::STGeomFromText('LINESTRING(5 0, 5 11)', 0)
--  @Car2's path is represented as a line that DOES intersect that 2 defined lines above
DECLARE @Car2   GEOMETRY = geometry::STGeomFromText('LINESTRING(5 0, 5 23)', 0)

;WITH Lines (LineID, LineGeom) AS
(
    SELECT 1, @line1    UNION ALL
    SELECT 2, @line2
)
,Cars (CarID, CarGeom) AS
(
    SELECT 1, @Car1 UNION ALL
    SELECT 2, @Car2
)
SELECT   C.CarID
        ,L.LineID
FROM Cars   C
JOIN Lines  L ON L.LineGeom.STIntersects(C.CarGeom) = 1