Hello,
I am looking for assistance in trying to pass points (MS 2007 Table Called GRID_BOX) that make up several polygons, where a box with the same name is a single polygon. Using this information I pass the four values from a sub routine into another routine to test whether a point (from a different table) is inside one of the polygons in the list.

When I hardcode the point to test and polygon points into pointInPolygon(), the routine works. However, my GRID_BOX table has over 700 records with a total of 96 Boxes. The table , which I will eventually read in (a .csv file) with all the single points (pointData) I am trying to test can have over 500K records. I am attempting to have the pointInPolygon routine read in both datasets (pointData and GRID_BOX). Read the first record in the pointData table, run it through the pointInPolygon() routine and if a point is inside one of the polygons tag that record with the box name it is in. Once the first record has been tested, then go on to the next record in the pointData Table until all the records in pointData have been tested against each polygon or box in the GRID_BOX table.
The code currently almost works. The code only reads the very last point in the GRID_BOX table and does the test. I want it to test each point for each polygon (box)
Here is what I have so far. I appreciate any help with this.

This is a sample of the larger GRID_BOX table, but is enough to test routines. The polygon table is broken down by:

AI_NAME * LONGITUDE * LATITUDE * VERTICES
BOX1 36.671365 64.462451 5
BOX1 36.667541 64.462217
BOX1 36.667762 64.468790
BOX1 36.671351 64.468481
BOX1 36.671365 64.462451
BOX2 36.671192 64.476694 5


BOX2 36.667608 64.476578
BOX2 36.667325 64.487119
BOX2 36.671320 64.487551
BOX2 36.671192 64.476694


**The Sub Routine I use to get polygon points for each box and pass to the routine pointInPolygon.**

Code:
Sub getPolyGridPoints(ByRef polyA As Variant, ByRef polyB As Variant, ByVal n As Long, ByRef AIname As String)
 
Dim db As DAO.Database
Dim rs As DAO.Recordset
 
Set db = CurrentDb
 
sSql = "SELECT AI_NAME, LONGITUDE, LATITUDE, VERTICES FROM GRID_BOX"
Set rs = db.OpenRecordset(sSql, dbOpenDynaset)
 
rs.MoveLast
rs.MoveFirst
While Not rs.EOF
n = rs![vertices]
polyA = (rs![Longitude])
polyB = (rs![Latitude])
AIname = rs![AI_Name]
rs.MoveNext
Wend
 
End Sub

*****The routine I am trying to pass this information to*****
I found a variant of this routine on the internet. I can’t recall the source, but I did use it and modified to suit my needs.

Code:
Sub pointInPolygon()
 
Dim i As Integer
Dim j As Integer  ‘Used to loop through each polygon counting the number of vertices and loop through the points n (vertices) times.
Dim x As Double  ‘X point to be tested.
Dim y As Double  ‘Y point to be tested.
Dim n As Long  ‘The number of vertices in the polygon
 
Dim polyx As Variant  ‘The X point in the vertices.
Dim polyy As Variant  ‘The Y point in the vertices.
Dim rcrossOdd As Boolean ‘Test Boolean to see if x, y points cross the polygon.
Dim onedge As Boolean ‘Test Boolean if x, y point is on edge.
Dim AIname As String  ‘Name of the polygon
 
‘Call this procedure to get values from getPolyGridPoints for each polygon to test each point.
getPolyGridPoints polyA, polyB, n, AIname
 
polyx = Array(polyA)
polyy = Array(polyB)
j = n
Box = AIname
 
‘Right now, I hard code a single test point.  This will eventually be an external .csv file
‘I will read in to populate the x and y values.  
x = 64.484012
y = 36.669574
 
‘Initiate the loop to count all the points for a single polygon.  When the single point is tested for a single
‘box see if it falls within the current polygon, if not go onto the next box and test again.  If the point does
‘not fall within any polygons, don’t do anything with the record.  If the point is inside one of the 
‘polygons, tag that record with the name of the polygon it is inside.
 
For i = 0 To j
 
‘Polygon point vertex in GRID_BOX Table and test point are the same.
If polyx(i) = x Then If polyy(i) = y Then MsgBox "Point is a vertex"  
 
‘Test if point is on the edge of two connecting polygon points, if not, then test whether point is inside one of the polygons. 
If Not onedge Then
   If (polyy(i) > y) <> (polyy(j) > y) Then
 
       side = (y * (polyx(j) - polyx(i)) + (polyx(i) * polyy(j)) - (polyx(j) * polyy(i))) / (polyy(j) - polyy(i))
 
       If side > x Then
           rcrossOdd = Not rcrossOdd
       ElseIf side = x Then
           onedge = True
       End If
   ElseIf y = polyy(i) Then
       If y = polyy(j) Then
           onedge = (polyx(i) > x) <> (polyx(j) > x)
       End If
   End If
End If
 
j = i
Next i
 
‘TEST RESULT OPTIONS.
If onedge Then
MsgBox "Point is on the edge"  ‘I may be interested in this point.
ElseIf rcrossOdd Then
MsgBox "Point is inside polygon"  ‘I WANT THIS POINT, TAG it to the record with the name of the polygon it is inside.
Else
MsgBox "Point is outside polygon" ‘I do not want this point, don’t do anything.
End If
 
End Sub
Hope this helps anyone else out there that may need this code or a modified version of it.

Thanks,
Ken