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:


select
@GmPoint.STBuffer(@GmPoint.STDistance(@GmLine)).STIntersection(@GmLine).STStartPoint().ToString()

But the result returns a null.  Any ideas?  I have attached the sql that I am using on SQL 2008 R2
GetClosestPoint.sql
yanci1179Asked:
Who is Participating?
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


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

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

'GEOMETRYCOLLECTION EMPTY' is returned
0
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();
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

DECLARE @GmPoint AS GEOMETRY ,
    @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

0

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!!
0
ste5anSenior DeveloperCommented:
Well, what kind of "line" is it? Looks like a polygon.
0
yanci1179Author Commented:
it is a pipeline.  not a polygon.  I attached a picture of it.
Line.GIF
0
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();
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.