I have built SQL Spatial triggers so that if users of our GIS move a pit (point feature) the pipes (lines) that end or start at the pit have their endpoint/startpoint altered. I basically replace the entire geometry.
The problem is, some pipes have more than one point. In this case I only want to alter the endpoint or startpoint and leave the other points as is, otherwise the other vertices are lost. Can you set the STEndPoint/STStartPoint or is there another way to only alter these points of a line?
Below code is my attempt so far, after trying the recursive CTE which I gave up on as I couldn't reference a table variable inside this. Instead I have built a temp table which will store the x and y values. It would be good to not have to repeat the script 4 times but it works (pipes will have 2 to 4 points). I now need to alter the geometry x/y for just the pipe endpoints/startpoints after the user moves the pit. I Will look at this tomorrow.
--Create temp table to store X and Y values of pipe vertices
CREATE table #PipeGeom
(
ID int IDENTITY(1,1) NOT NULL,
N int,
X float,
Y float,
PipeGMSCKey int
)
--insert vertices. Most pipes have two points. A few have 4.
DECLARE @VertexCount INT
SET @VertexCount = 1
INSERT INTO #PipeGeom (N, X, Y, PipeGMSCKey)
SELECT N=@VertexCount, pipe.Geometry_SPA.STPointN(1).STX AS X, pipe.Geometry_SPA.STPointN(1).STY AS Y, pipe.GMSC_Key FROM [Assets_GMSC_Dev].[dbo].[vw_Pipes] pipe
INNER JOIN [Assets_GMSC_Dev].[dbo].[vw_Pits] pit ON pipe.Geometry_SPA.STDistance(pit.Geometry_SPA) <0.1
WHERE pit.GMSC_Key = '1481532'
--UNION ALL
INSERT INTO #PipeGeom (N, X, Y, PipeGMSCKey)
SELECT N=2, pipe.Geometry_SPA.STPointN(2).STX AS X, pipe.Geometry_SPA.STPointN(2).STY AS Y, pipe.GMSC_Key FROM [Assets_GMSC_Dev].[dbo].[vw_Pipes] pipe
INNER JOIN [Assets_GMSC_Dev].[dbo].[vw_Pits] pit ON pipe.Geometry_SPA.STDistance(pit.Geometry_SPA) <0.1
WHERE pit.GMSC_Key = '1481532'
--UNION ALL
INSERT INTO #PipeGeom (N, X, Y, PipeGMSCKey)
SELECT N=3, pipe.Geometry_SPA.STPointN(3).STX AS X, pipe.Geometry_SPA.STPointN(3).STY AS Y, pipe.GMSC_Key FROM [Assets_GMSC_Dev].[dbo].[vw_Pipes] pipe
INNER JOIN [Assets_GMSC_Dev].[dbo].[vw_Pits] pit ON pipe.Geometry_SPA.STDistance(pit.Geometry_SPA) <0.1
WHERE pit.GMSC_Key = '1481532'
--UNION ALL
INSERT INTO #PipeGeom (N, X, Y, PipeGMSCKey)
SELECT N=4, pipe.Geometry_SPA.STPointN(4).STX AS X, pipe.Geometry_SPA.STPointN(4).STY AS Y, pipe.GMSC_Key FROM [Assets_GMSC_Dev].[dbo].[vw_Pipes] pipe
INNER JOIN [Assets_GMSC_Dev].[dbo].[vw_Pits] pit ON pipe.Geometry_SPA.STDistance(pit.Geometry_SPA) <0.1
WHERE pit.GMSC_Key = '1481532'
--User moves pit, then get new pit location, which pipe start/endpoint will need to move to.
--New Start and Finish points
DECLARE @PitX FLOAT = (SELECT Geometry_SPA.STX FROM vw_Pits WHERE GMSC_Key = '1481532');
DECLARE @PitY FLOAT = (SELECT Geometry_SPA.STY FROM vw_Pits WHERE GMSC_Key = '1481532');
--need to grab just the endpoint/startpoint of line here and build Geometry String
drop table #PipeGeom
Thanks