Get the Closest Point to a Line: SQL Server 2008 R2

I am trying to find the closest point on a line.

Based on some articles I used the following:


But the result returns a null.  Any ideas?  I have attached the sql that I am using on SQL 2008 R2
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, shouldn't that not be:

SELECT @GmLine.STIntersection(@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine)));

Open in new window

yanci1179Author Commented:
I tried that, however when you display the ToString()

SELECT @GmLine.STIntersection(@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine))).ToString();

yanci1179Author Commented:
please see the coordinates that I have attached for example of how it is returning 'GEOMETRYCOLLECTION EMPTY' with SELECT @GmLine.STIntersection(@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine))).ToString();
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

ste5anSenior DeveloperCommented:
hmm, when I need to guess: It's due to your weird line definition. E.g. this works:

    @GmLine AS GEOMETRY;

SET @GmPoint = GEOMETRY::STGeomFromText('POINT ( 9 11 )', 0);
SET @GmLine = GEOMETRY::STGeomFromText('LINESTRING ( 0 0 null 1, 20 20 null 2 )', 0);

SELECT  @GmLine.STIntersection(@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine))).ToString();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yanci1179Author Commented:
any ideas on how I can make mine work?  The linestring contains the Measure and by default SQL server does not include the Z value

Thank you for your quick responses!!
ste5anSenior DeveloperCommented:
Well, what kind of "line" is it? Looks like a polygon.
yanci1179Author Commented:
it is a pipeline.  not a polygon.  I attached a picture of it.
yanci1179Author Commented:
I stripped of the M and Z and only included a portion of the line.  I'm still getting GEOMETRYCOLLECTION EMPTY.  I really appreciate the help, I've been trying to figure this out for a few days and i'm not getting anywhere.

Declare @GmPoint as Geometry
,@GmLine as Geometry

set @GmPoint = geometry::STGeomFromText('POINT (-3456626.2357009738 -167238.147067269)',0)

set @GmLine =
geometry::STGeomFromText('LINESTRING (-839114.95222563 -3451172.56977921, -839114.79576344 -3451167.59279138)',0)

SELECT @GmLine.STIntersection(@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine))).ToString();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.