I have a shape file that I have imported in to MSSQL as a table with a geometry column. The geometry column is essentially the outline of the UK coastline. What I have been asked to do is to be able to supply latitude/longitude and calculate the closest distance to the coast
I have established that the problem I am having is that the point I want to find the distance from to the closest edge of the shape is within the shape and therefore always returns a distance of zero. If I try a point outside the shape (at sea) I get a distance
My question is, is there a way to get SQL to return the distance from with inside? If not then is there a way to invert the shape?
3条答案
按热度按时间s2j5cfk01#
The answer to both of your questions is an enthusiastic "yes!". Here. We. Go.
(I)s there a way to get SQL to return the distance from with inside?
There sure is. You need a way to calculate the boundary of your polygon (which, in this case, represents the UK landmass). While there isn't a
STBoundary()
method like there is with thegeometry
data type, we can get what we're looking for with theSTRingN()
method. Specifically, we're going to useSTRingN(1)
to get the first ring in the polygon (which should represent the outer boundary of the polygon). One caveat is that if you have a MULTIPOLYGON instead of a regular POLYGON (say, to represent the real world fact that mainland England and Ireland are separate landmasses), you'll have do a little work around getting the boundary for each polygon separately withSTGeometryN()
and then get the first ring from each of thoseIf not then is there a way to invert the shape?
Fortunately, we don't need to revert to this solution in this case (but I do applaud your thinking outside of the box!). I say "fortunately" because it doesn't lend itself to a good/generic solution. That is, what if the point were outside of the UK? Inverting the UK polygon puts you right back in the situation you were in. Which you could dance around and do something like "get the distance to both the polygon and its inversion and take whichever number isn't zero". But inverting polygons is a useful technique generally (since the order in which you specify points in a shape matters, sometimes you provide them in "reverse" order, and end up with a polygon of the entire world with a UK-shaped hole in it). All of this is a long-winded way to say: the
ReorientObject()
method is what you're looking for.And now for something completely different, some code:
wz3gfoph2#
if you want the distance or shortestline of linestrings inside the 'polygon' , then its best to work with MULTILINESTRING!
vcudknz33#
Convert the GEOGRAPHY object to GEOMETRY, use STBoundary(), then convert back and test against the new instance witch now is a LINESTRING:
Of couse you dont have to use all the variables I've used, this is just for readability, just put all the code in one select.