In Rasters, Pixels and Code Oh My! I extended an automation trigger to populate an attribute field based on its location within a raster pixel. But I still want more data automation. I want it all!
Not only do we want to know some administrative attributes of new data points (what county, state is it in?: Spatial Intersect), how high it is, but in an era of not-so-precise data points, what is nearest to this point?
In this particular application, a water quality monitoring dataset, I also want to know the name of the stream this point occurs on, as well as the stream order. I'm using NHD Stream Lines which have been run through Arc Hydro to add Strahler Stream Order as an attribute.
Using a Nearest Neighbor Query I can accomplish that, albeit with a little more complexity because now I'm asking SQL to look for the "Nearest" feature, and not a simple intersect of two geography objects. The code block starts on line 48.
ALTER TRIGGER [dbo].[TEST_GEOGRAPHY] ON [dbo].[TEST] /****** fire on inserts and updates ******/ /****** disable trigger when SQL replication or mirroring is enabled ******/ after INSERT,UPDATE NOT FOR REPLICATION AS BEGIN SET NOCOUNT ON; UPDATE p SET /****** hypothetically we could enter the lat/lon as text and create a geography object ******/ SHAPE = CASE WHEN i.SHAPE IS NOT NULL THEN p.SHAPE ELSE Geography::STPointFromText('POINT(' + CAST(p.LON AS VARCHAR(20)) + ' ' + CAST(p.LAT AS VARCHAR(20)) + ')', 4269) END, /****** usual case point is created with ARC and casts the LAT/LON as text ******/ /****** from the geography object ******/ LON = CASE WHEN p.SHAPE IS NULL THEN p.LON ELSE p.SHAPE.Long END, LAT = CASE WHEN p.SHAPE IS NULL THEN p.LAT ELSE p.SHAPE.Lat END, QuadName = COALESCE(b.name, p.QuadName), Watershed = COALESCE(c.HUC_12_Name, p.Watershed), County = COALESCE(d.Name, p.County), State= COALESCE(e.Name, p.State), PARKDISTRICT = COALESCE(f.District, p.PARKDISTRICT), ELEVATION = (SELECT pdata.getValueByLoc(1,p.SHAPE.Long,p.SHAPE.Lat) FROM [dbo].[DEM10MP]), StreamName = COALESCE(g.GNIS_Name, p.StreamName), RiverOrder = COALESCE(h.RiverOrder, p.RiverOrder) FROM TEST AS p /****** allow upate of lat/lon on update ******/ INNER JOIN inserted AS i ON i.globalid = p.globalid LEFT OUTER JOIN USGS_24K_TOPOMAP_BOUNDARIES AS b ON b.Shape.STIntersects(i.Shape) = 1 LEFT OUTER JOIN WATERSHEDS AS c ON c.Shape.STIntersects(i.Shape) = 1 LEFT OUTER JOIN GRSM_COUNTIES AS d ON d.Shape.STIntersects(i.Shape) = 1 LEFT OUTER JOIN GRSM_States AS e ON e.Shape.STIntersects(i.Shape) = 1 LEFT OUTER JOIN GRSM_SUB_RANGER_DISTRICTS AS f ON f.Shape.STIntersects(i.Shape) = 1 /****** Nearest Neighbor ******/ CROSS APPLY (SELECT TOP 1 GNIS_Name, shape FROM dbo.NHDFLOWLINE /****** force spatial index hint ******/ WITH(index ([S208_idx])) WHERE NHDFLOWLINE.Shape.STDistance(i.Shape) IS NOT NULL ORDER BY NHDFLOWLINE.Shape.STDistance(i.Shape) ASC) as g CROSS APPLY (SELECT TOP 1 RiverOrder, shape FROM dbo.NHDFLOWLINE /****** force spatial index hint ******/ WITH(index ([S208_idx])) WHERE NHDFLOWLINE.Shape.STDistance(i.Shape) IS NOT NULL ORDER BY NHDFLOWLINE.Shape.STDistance(i.Shape) ASC) as h ; END GO
Note the use of an index hint here. In SQL 2008 use of a the spatial index is not honored, and in fact, doesn't work without a lot of tweaking. Here we're telling it to find the name of the nearest stream and its order, but use the spatial index of the NHD Flowline feature class to optimize the query. Without the index hint this query takes for ever!
If the stream name is null (as many are) the crossapply and order by statements stop the search at the nearest stream instead of looking for the nearest named stream.
I've found this works best when I enforce some data entry rules: add a new point only using snapping (to the NHD layer in the map interface). There is the danger that someone could erroneously add a new point that is "somewhat" between two streams, and the wrong name and order could get attributed to the point.
In this particular application, we are using it in conjunction with the USGS HEM tools, which handles the snapping, assignment, and population of reachcode to the point data, so we're not to worried about the wrong-point-in-the-wrong-place scenario.
This is a personal blog and does not recommend, endorse, or support the methods described above. Alteration of data using SQL outside of the ESRI software stack, of course, is not supported and should not be applied to a production database without a thorough understanding and disaster recovery plan.