简体   繁体   English

空间加入SQL服务器

[英]Spatial Join in SQL Server

I come from a mining and exploration background.我来自采矿和勘探背景。 I have a list of points (with ID and X and Y coordinates).我有一个点列表(带有 ID 以及 X 和 Y 坐标)。

Example例子

I have another table which contains polygons (claim numbers).我有另一个包含多边形(索赔编号)的表。 I am trying to find out which points fall within which polygons.我试图找出哪些点属于哪些多边形。 My coordinates are in UTM Projection.我的坐标在 UTM 投影中。

Thank you in advance!先感谢您!

I was trying the following code however my query is not returning results.我正在尝试以下代码,但是我的查询没有返回结果。

SELECT 
      pg.claim_number, 
      p.DHId 
FROM leasehold as pg
JOIN
(SELECT 
     DHId, 
     X, 
     Y,
     geometry::STPointFromText('POINT(',X,' ',Y,'), 0) AS [geom]
 FROM collar 
      WHERE X is not null AND 
      Y is not null AND 
      claim_number is null) AS p
ON pg.Shape.STIntersects(p.geom) = 1

I was expecting to get a list with claim_number from polygon table which each DHId from point table intersects or falls within.我期望从多边形表中获得一个包含 claim_number 的列表,点表中的每个 DHId 都相交或落在其中。

It looks like there just a syntax/quoting issue.看起来只有语法/引用问题。 I cleaned it up a bit and replaced STPointFromText with Point which is an MS-specific extension but doesn't require you to create WKT just to get a point.我稍微清理了一下并将STPointFromText替换为Point ,这是一个特定于 MS 的扩展,但不需要您创建 WKT 只是为了获得一个点。 But that's really all I changed - I'd expect your general approach to work.但这真的是我改变的全部 - 我希望你的一般方法有效。

SELECT 
      pg.claim_number, 
      p.DHId 
FROM leasehold as pg
JOIN (
    SELECT 
        DHId, 
        X, 
        Y,
        geometry::Point(X, Y, 0) AS [geom]
    FROM collar 
    WHERE X is not null AND 
        Y is not null AND 
        claim_number is null
) AS p
    ON pg.Shape.STIntersects(p.geom) = 1;

That said, I would expect this to not be super performant.就是说,我希望这不会表现出色。 You're creating the points on the fly and so will be incurring that cost at run time.您正在即时创建点,因此会在运行时产生该成本。 As such, there's no way to put a spatial index on that data.因此,无法在该数据上放置空间索引。 If you can, I'd suggest adding a column to your collar table that is the geometry point and, as I implied, put a spatial index on it.如果可以的话,我建议在您的领表中添加一列作为几何点,并且正如我所暗示的那样,在其上放置一个空间索引。 Also, if there's not an index on the leasehold.Shape column, I'd put one there as well.此外,如果 leasehold.Shape 列上没有索引,我也会在那里放一个索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM