Domanda

In an ideal world I would be running on SQL Server 2012 and be able to use the .ShortestLineTo() function to find the closest point on a line, to that of another point. I currently am able to find the closest line to my point - but now I need to find the coordinates of the point closest to my point in question.

Unfortunately I am stuck on SQL Server 2008 R2, so don't have the option to use .ShortestLineTo().

How do others achieve this in SQL Server Spatial Geometry types?

Cheers,

Matt

È stato utile?

Soluzione

Late answer, but if it still helps (or for anyone else) you should be able to do the following with SQL 2008.

DECLARE @point GEOMETRY = GEOMETRY::STPointFromText('POINT(0 0)', 0);
DECLARE @line GEOMETRY = GEOMETRY::STLineFromText('POINT(10 10, 20 20)', 0);

SELECT STIntersection(@point.STBuffer(@point.STDistance(@line)));

Essentially, you calculate the distance between the two geometries,use that as a buffer on the point which should result in the geometries touching, and take the intersection (point) of that.

Altri suggerimenti

Note @Road is the same as geography MULTILINESTRING

SET INITIAL ROAD SEGMENT
SET @Road = geography::STMLineFromText('MULTILINESTRING ((-79.907603999999992 32.851905999999985, -79.907708999999983 32.851751, -79.907879999999992 32.851555999999995, -79.907889999999981 32.851542999999992, -79.907995999999983 32.851461, -79.90813399999999 32.851373999999986, -79.90833499999998 32.851286999999992, -79.908529 32.85121, -79.909110999999982 32.850974))', 4269);

GET START/END POINTS OF ROAD SEGMENT FOR NEW MIDPOINT CALCULATION
SET @x1 = CAST(@Road.STStartPoint().Lat AS decimal(11,6))
SET @y1 = CAST(@Road.STStartPoint().Long AS decimal(12,6))
SET @x2 = CAST(@Road.STEndPoint().Lat AS decimal(11,6))
SET @y2 = CAST(@Road.STEndPoint().Long AS decimal(12,6))

ASSIGN ROAD SEGMENT MIDPOINT LAT/LON
SET @MidPointLat = CAST( ((@x1 + @x2) / 2) AS nvarchar(11))
SET @MidPointLon = CAST( ((@y1 + @y2) / 2) AS nvarchar(12))

SET INITIAL OFF ROAD CENTROID
SET @RoadMidPt = geography::STPointFromText('POINT(' + @MidPointLon + ' ' + @MidPointLat + ')', 4269)

CALCULATE BUFFER DISTANCE BACK TO ROAD FOR .STIntersection (add .02 to ensure intersect)
SET @RoadMidPtBuffer = @RoadMidPt.STBuffer(@RoadMidPt.STDistance(@Road) + .02)

Might intersect at multiple points! Use 1st point that intersects road as centroid
SET @RoadCentroid = @RoadMidPtBuffer.STIntersection(@Road).STPointN(1);

FINAL NEW LAT/LON OF CLOSEST ROAD/LINE CENTROID
SELECT @RoadCentroid.Lat, @RoadCentroid.Long

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top