MSSQL Hatta Node Ekleme

Aşağıdaki kod ile bir hatta node ekleme işlemi gerçekleştirilebilir. Eğer eklenmek istenen yerde node varsa o noktaya tekrardan ekleme yapmaz.

USE [master]
GO

/****** Object:  UserDefinedFunction [dbo].[STAddNodeToLine]    Script Date: 23.02.2021 16:02:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE Function [dbo].[STAddNodeToLine](@lineString nvarchar(MAX),
	@pointString nvarchar(MAX),
	@srid int)

returns Nvarchar(MAX)
as
begin
declare @orjGeom geometry;
declare @intersectionGeom geometry;
declare @newLine nvarchar(MAX)='';
declare @nodeCount int=0;
declare @i int=0;
declare @x1 nvarchar(150);
declare @y1 nvarchar(150);
declare @x2 nvarchar(150);
declare @y2 nvarchar(150);
declare @tmpLine nvarchar(MAX);

Set @orjGeom=geometry::STGeomFromText(@lineString,@srid);
Set @intersectionGeom=geometry::STGeomFromText(@pointString,@srid);
Set @nodeCount=@orjGeom.STNumPoints ();

set @i=1;
	

	while @i<@nodeCount
	begin
		set @x1=STR(@orjGeom.STPointN(@i).STX,18,8);
		set @y1=STR(@orjGeom.STPointN(@i).STY,18,8);

		set @x2=STR(@orjGeom.STPointN(@i+1).STX,18,8);
		set @y2=STR(@orjGeom.STPointN(@i+1).STY,18,8);

		set @tmpLine='LINESTRING ('+@x1+' '+@y1+' , '+@x2+' '+@y2+')';

		if geometry::STGeomFromText(@tmpLine,3857).STIntersects(@intersectionGeom.STBuffer(0.001))=1 begin
			if LEN(@newLine)=0 begin
				set @newLine=@x1+' '+@y1+','+STR(@intersectionGeom.STX,18,8)+' '+STR(@intersectionGeom.STY,18,8)+','+@x2+' '+@y2;
			end
			else begin
				set @newLine=@newLine+','+@x1+' '+@y1+','+STR(@intersectionGeom.STX,18,8)+' '+STR(@intersectionGeom.STY,18,8)+','+@x2+' '+@y2;
			end
		end
		else begin
			if LEN(@newLine)=0 begin
				set @newLine=@x1+' '+@y1+','+@x2+' '+@y2;
			end
			else begin
				set @newLine=@newLine+','+@x1+' '+@y1+','+@x2+' '+@y2;
			end
		end
		set @i=@i+1;
	end;

 set @newLine='LINESTRING ('+TRIM(@newLine)+')';

 set @newLine=geometry::STGeomFromText(@newLine,@srid).MakeValid().Reduce(0).STAsText();

return @newLine;
end;
GO


 

Add comment

Loading