是否可以使用STWithin或STContains根据多边形关系中的点更新列?

时间:2012-01-28 15:33:08

标签: sql sql-server-2008 spatial

鉴于以下表格:

CREATE TABLE #USGS_24K_TOPOMAP_BOUNDARIES(
    [OBJECTID] [int] NOT NULL,
    [AREA] [numeric](38, 8) NULL,
    [PERIMETER] [numeric](38, 8) NULL,
    [QD24K_GRSM] [numeric](38, 8) NULL,
    [QD24K_GR_1] [numeric](38, 8) NULL,
    [QUADID] [numeric](38, 8) NULL,
    [CENTLAT] [numeric](38, 8) NULL,
    [CENTLONG] [numeric](38, 8) NULL,
    [NAME] [nvarchar](35) NULL,
    [STATE] [nvarchar](2) NULL,
    [LATLONG] [nvarchar](9) NULL,
    [OHIO_INDEX] [nvarchar](8) NULL,
    [GRID60] [nvarchar](5) NULL,
    [Reviewed] [int] NULL,
    [Corrected] [int] NULL,
    [Verified] [int] NULL,
    [GlobalID] [uniqueidentifier] NOT NULL,
    [SHAPE] [geometry] NULL)

CREATE TABLE #tbl_locations(

[OBJECTID] [int] NOT NULL,
[FCategory] [varchar](16) NULL,
[MapMethod] [varchar](4) NULL,
[HError] [varchar](50) NULL,
[MapSource] [varchar](255) NULL,
[SourceDate] [datetime2](7) NULL,
[EditDate] [datetime2](7) NULL,
[Notes] [varchar](255) NULL,
[Species_Community] [varchar](50) NULL,
[Location_ID] [uniqueidentifier] NOT NULL,
[Site_ID] [uniqueidentifier] NULL,
[GIS_Location_ID] [varchar](50) NULL,
[Meta_MID] [varchar](50) NULL,
[X_Coord] [numeric](38, 8) NULL,
[Y_Coord] [numeric](38, 8) NULL,
[Coord_Units] [varchar](50) NULL,
[Coord_System] [varchar](50) NULL,
[UTM_Zone] [varchar](50) NULL,
[Accuracy_Notes] [varchar](255) NULL,
[Unit_Code] [varchar](12) NULL,
[Loc_Name] [varchar](100) NULL,
[Loc_Type] [varchar](25) NULL,
[Updated_Date] [varchar](50) NULL,
[Loc_Notes] [varchar](255) NULL,
[Datum] [varchar](5) NULL,
[Watershed] [varchar](50) NULL,
[StreamName] [varchar](50) NULL,
[NHDReachCode] [varchar](14) NULL,
[TOPO_NAME] [varchar](50) NULL,
[Trail] [varchar](100) NULL,
[Road] [varchar](50) NULL,
[Elevation] [numeric](38, 8) NULL,
[LAT] [numeric](38, 8) NULL,
[LON] [numeric](38, 8) NULL,
[Population_ID] [uniqueidentifier] NULL,
[Year_] [varchar](4) NULL,
[WGS_DAT] [varchar](5) NULL,
[WGS_CS] [varchar](5) NULL,
[County] [varchar](20) NULL,
[State] [varchar](15) NULL,
[IsExtant] [varchar](3) NULL,
[IsSenstive] [varchar](3) NULL,
[SpeciesName] [varchar](125) NULL,
[SpeciesID] [varchar](50) NULL,
[Species_ID] [int] NULL,
[SHAPE] [geometry] NULL)

我想用#USGS_24K_TOPOMAP_BOUNDARIES.Name填充#tbl_locations.Topo_Name。换句话说,我试图确定一个点所在的地形图的名称,并以编程方式将其写入点表。理论上看似简单,但是tbl_locations包含可能出现在36个拓扑图多边形边界之一中的数千个点。

我到目前为止

Select NAME, Loc_Name, Location_ID
From #USGS_24K_TOPOMAP_BOUNDARIES a, #TBL_LOCATIONS b
where a.Shape.STContains(b.Shape)=1

返回一个整洁的表,我可以通过连接交叉回到tbl_locations,但我坚持通过单个查询更新语句完成这个,并且我有许多类似的点 - 多边形关系,我' d喜欢以这种方式自动化(例如分水岭,县,州等......这一点发生在)。谢谢!

2 个答案:

答案 0 :(得分:1)

update TBL_LOCATIONS
set TOPO_NAME = dbo.QD24K_GRSM.NAME
--(SELECT     a.NAME, b.Loc_Name, b.Location_ID
FROM dbo.tbl_locations
inner join dbo.QD24K_GRSM 
on TBL_LOCATIONS.Location_ID = TBL_LOCATIONS.Location_ID
WHERE  (QD24K_GRSM.Shape.STContains(TBL_LOCATIONS.SHAPE) = 1) ;

答案 1 :(得分:0)

UPDATE 
    tbl_locations 
SET 
    tbl_locations.Topo_Name = t.Name
FROM 
    (SELECT Shape, Name FROM USGS_24K_TOPOMAP_BOUNDARIES) t
WHERE 
    t.Shape.STContains(tbl_locations.Shape) = 1