Problem when evaluating if a point belongs to a polygon using the GEOMETRY data type - SQL SERVER 2014

1

Good morning,

I am setting up a procedure in T-SQL to detect if a point belongs to a polygon in the following way:

By putting an exact point that is in the polygon, the STContains method should return a 1 (Belongs) and is returning a 0 (Does not belong to a polygon).

What am I doing wrong? Is the way I am manipulating these types of data and their methods correct? Thank you very much for the help in advance.

    
asked by Osky 07.12.2017 в 13:31
source

1 answer

0

I think the problem here is interpretation of the operation of STContains , if we read the documentation:

  

Returns 1 if a geometry instance completely contains another geometry   instance. Returns 0 if it does not.

The important thing is "completely contains", and here I ask myself, a point of the vertex of a polygon is completely contained in it? I understand that we can not and we can verify it easily:

DECLARE @G1 GEOMETRY
DECLARE @G2 GEOMETRY

SELECT  @G1 = GEOMETRY::STGeomFromText('POLYGON ((175 150, 20 40, 50 60, 125 100, 175 150))', 0),
    @G2 = GEOMETRY::STGeomFromText('POINT (174 149)', 0)

SELECT  @G1.STContains(GEOMETRY::STGeomFromText('POINT (175 150)', 0))  AS 'P1',
    @G1.STContains(GEOMETRY::STGeomFromText('POINT (20 40)', 0))    AS 'P2',
    @G1.STContains(GEOMETRY::STGeomFromText('POINT (50 60)', 0))    AS 'P3',
    @G1.STContains(GEOMETRY::STGeomFromText('POINT (125 100)', 0))  AS 'P4'

The Exit:

+----+----+----+----+
| P1 | P2 | P3 | P4 |
+----+----+----+----+
| 0  | 0  | 0  | 0  |
+----+----+----+----+

We see that none of the vertex points of the polygon are completely contained in it. What you can do is determine the intersection of a point with the polygon, using STIntersects() :

SELECT  @G1.STIntersects(GEOMETRY::STGeomFromText('POINT (175 150)', 0))    AS 'P1',
    @G1.STIntersects(GEOMETRY::STGeomFromText('POINT (20 40)', 0))      AS 'P2',
    @G1.STIntersects(GEOMETRY::STGeomFromText('POINT (50 60)', 0))      AS 'P3',
    @G1.STIntersects(GEOMETRY::STGeomFromText('POINT (125 100)', 0))    AS 'P4'

The exit:

+----+----+----+----+
| P1 | P2 | P3 | P4 |
+----+----+----+----+
| 1  | 1  | 1  | 1  |
+----+----+----+----+
    
answered by 07.12.2017 / 17:21
source