# Test if point inside polygon VBA

**URL:** <https://mapwindow.discourse.group/t/test-if-point-inside-polygon-vba/1308>\
**Category:** MapWinGIS\
**Created:** [April 11, 2022, 11:11am UTC](https://mapwindow.discourse.group/t/test-if-point-inside-polygon-vba/1308 "2022-04-11T11:11:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clapter](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Clapter](https://mapwindow.discourse.group/u/Clapter)\
**Post date:** [April 11, 2022, 11:11am UTC](https://mapwindow.discourse.group/t/test-if-point-inside-polygon-vba/1308/1 "2022-04-11T11:11:48Z")

</div>

I’m trying to test if a point is insde a polygon. Polygon is stored in SHP file. Coordinate system WGS-84. Point coordinates are known (i.e. 55.638717,37.459322). How to specify the coordinates of a point to test if a point is insde a polygon?

```auto
Sub test()
Dim SNT As New MapWinGIS.ShapeFile
Dim ShapeFile As New MapWinGIS.ShapeFile
Dim TestSHP As New MapWinGIS.Shape
Dim a, b, c

SNT.Open ("D:\TEMPtemp\MyShp.shp")
a = TestSHP.ImportFromWKT("POINT (4169952.65050127 7486828.05204518 0)")
b = ShapeFile.EditAddShape(TestSHP)
Set c = SNT.GetIntersection(False, ShapeFile, False, SHP_NULLSHAPE)

End Sub

```

a = True, b = -1, c is Nothing  
What should I do next?  
I would be very happy to receive any help.  
Best regards Clapter.

---

<div class="post-metadata">

**Author:** ![Clapter](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Clapter](https://mapwindow.discourse.group/u/Clapter)\
**Post date:** [April 13, 2022, 8:11am UTC](https://mapwindow.discourse.group/t/test-if-point-inside-polygon-vba/1308/2 "2022-04-13T08:11:13Z")

</div>

I answer myself:

```auto
SNT.Open ("D:\TEMPtemp\MyShp.shp")
xx = 4169952.65050127
yy = 7486828.05204518
SNT.BeginPointInShapefile 'doesn't work at all without this
a = SNT.PointInShapefile(xx, yy)
If a > 0 Then
  ' Point in Shape!
End if
SNT.EndPointInShapefile

```

Example helped me:

> <https://github.com/MapWindow/MapWinGIS/issues/113>
>
> I've seen this error over the years and though it was time it was reported as it…'s now becoming mission critical. My code is in VBA (Excel) and I'm using data with OSGB projection. If I try and geocode coordinates it usually works fine and returns the correct shapeindex, just occasionally it's wildly wrong - altering the x-y coordinates slightly solves the problem, but a blanket change then causes different coordinate pairs to fail instead.
> 
> Here's a shapefile and a spreadsheet with minimal code to illustrate the problem, all the points are definitely in shape index 5 even though the ocx puts most of them in shape 20. To the non-expert eye I'm thinking rounding errors or a test condition slipping through the net. Slightly unrelated is that the whole thing seems to fail if I don't cache the shapefile. I also find that if the coordinates are held in variant variables it fails much more often unless I use cDbl to force it to be a double.
> 
> To use: put the shapefile somewhere, edit the Excel cell to say where it is and hit "go", the code will then find out the index for the coordinate and the eight one metre away in all directions. The code is idiotically simple so I'm sure I'm not doing anything wrong. (32 bit Excel, current 32 bit ocx)
> 
> \[GeocodeBug.zip\](https://github.com/MapWindow/MapWinGIS/files/2194272/GeocodeBug.zip)

Best regards, Clapter.

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [April 14, 2022, 7:37am UTC](https://mapwindow.discourse.group/t/test-if-point-inside-polygon-vba/1308/3 "2022-04-14T07:37:08Z")

</div>

Yes Clapter, that is the correct process.

Here’s the official documentation: [MapWinGIS: Shapefile optimizations](https://www.mapwindow.org/mapwingis/group __shapefile__ optimizations.html#gaf4b37a628d3aaee1972cf0d5789f720b)
