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 |
+----+----+----+----+