Determine if map coordinates are within area

EvonS

Board Regular
Joined
Jun 1, 2016
Messages
111
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I have this information below that define areas on a map. Is there a way to determine if a longitude and latitude are within one of these areas?

OBJECTIDAGENCYDIVUNIT_NAMEADDRESSCITYAREA_SQKMShape__AreaShape__Length
1​
Toronto Police ServiceD1111 Division2054 Davenport RdToronto
18.67845803​
35692935.07​
28190.11106​
2​
Toronto Police ServiceD1212 Division200 Trethewey DrToronto
24.78034332​
47417573.68​
33029.13457​
3​
Toronto Police ServiceD1313 Division1435 Eglinton Av WToronto
18.60191063​
35591722.93​
24616.57917​
4​
Toronto Police ServiceD1414 Division350 Dovercourt RdToronto
14.17525415​
27083979.14​
30017.70564​
5​
Toronto Police ServiceD2222 Division3699 Bloor St WToronto
68.12188386​
130097609.1​
72765.03189​
6​
Toronto Police ServiceD2323 Division5230 Finch Av WToronto
57.64228008​
110369052.3​
54270.05361​
7​
Toronto Police ServiceD3131 Division40 Norfinch DrToronto
42.66749447​
81789149.85​
44477.14656​
8​
Toronto Police ServiceD3232 Division30 Ellerslie AvToronto
61.06048027​
117079222.9​
43658.49725​
9​
Toronto Police ServiceD3333 Division50 Upjohn RdToronto
52.7888815​
101244281.9​
45148.84874​
10​
Toronto Police ServiceD4141 Division2222 Eglinton Av EToronto
44.57511239​
85395988.8​
58875.05415​
11​
Toronto Police ServiceD4242 Division242 Milner Av EToronto
84.61174885​
162528804.9​
59824.09155​
12​
Toronto Police ServiceD4343 Division4331 Lawrence Av EToronto
59.37000474​
113873893.7​
54424.50711​
13​
Toronto Police ServiceD5151 Division51 Parliament StToronto
8.601682841​
16437802.1​
25324.5062​
14​
Toronto Police ServiceD5252 Division255 Dundas St WToronto
9.228055772​
17625448.21​
35966.54447​
15​
Toronto Police ServiceD5353 Division75 Eglinton Av WToronto
31.29485302​
59895251.81​
37395.46015​
16​
Toronto Police ServiceD5555 Division101 Coxwell AvToronto
41.03398633​
78486159.8​
82307.80296​
 
My initial Google search was for "determine polygon that point lies in" (without quotes). Several of the more noteworthy results describe a ray-casting technique that counts the number of edges crossed for a given polygon. Also described is a winding number method. One of the search results linked to a stackoverflow site:
...where there is a lot of discussion about solution strategies, and in some cases code.
On the 2nd page of the stackoverflow site was a post that included a link to the Geometry Algorithm page I mentioned earlier:
Another link from that search led to a Wikipedia article:
...which provides a good overview of the problem, and it describes some approaches and interesting history. Who knew that "Ray Tracing News" was discussing this stuff in 1990?

Anyway, about the time I began to adapt some code for an Excel-based solution, you offered the link to your "Corner" on excelfox (in post #11). There discussion occurs between 2013 up to 2021, and in the last post from 2021 by DocAElstein (Alan), he offers links to several PointInPolygon excel files on Box. I downloaded this one:
Metadata for that file--which includes the PtInPoly code in UDF form--indicates that you were the author with a creation date of 2/28/2003!
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
My initial Google search was for "determine polygon that point lies in" (without quotes). Several of the more noteworthy results describe a ray-casting technique that counts the number of edges crossed for a given polygon. Also described is a winding number method. One of the search results linked to a stackoverflow site:
...where there is a lot of discussion about solution strategies, and in some cases code.
On the 2nd page of the stackoverflow site was a post that included a link to the Geometry Algorithm page I mentioned earlier:
Another link from that search led to a Wikipedia article:
...which provides a good overview of the problem, and it describes some approaches and interesting history. Who knew that "Ray Tracing News" was discussing this stuff in 1990?

Anyway, about the time I began to adapt some code for an Excel-based solution, you offered the link to your "Corner" on excelfox (in post #11). There discussion occurs between 2013 up to 2021, and in the last post from 2021 by DocAElstein (Alan), he offers links to several PointInPolygon excel files on Box. I downloaded this one:
Metadata for that file--which includes the PtInPoly code in UDF form--indicates that you were the author with a creation date of 2/28/2003!
Thank you for the complete and extensive follow-up!
 
Upvote 0
Hey everyone, can this be accomplished without macros?
 
Upvote 0
Yes...I believe you mentioned you were using Excel 365. So you'll want to use the LAMBDA version of Rick's code that @lrobbo314 described above.
 
Upvote 0
Yes...I believe you mentioned you were using Excel 365. So you'll want to use the LAMBA version of Rick's code that @lrobbo314 described above.
I am using 365 but unfortunately the users of the submission for aren't. They are having problems with it working. Since you made everything for me, I have no idea what to edit :-(. You mind converting it for me please or pointing me in the right direction to make the edits myself?
 
Upvote 0
Oh...that's going to be a problem then. There are a lot of steps being handled by the VBA code because each side of a potential polygon needs to be examined (i.e. we need to determine if a ray extending out from the input location in some pre-defined but arbitrary direction crosses each line segment comprising the division's boundary edges...and then all of those crossings are counted). This is readily done in VBA, and it can be done with a compact formula in Excel 365 because the LAMBDA function allows for making this type of examination repeatedly by looping through segment by segment. If both of those options are off the table, then you would need to perform this analysis using helper columns and additional formulas. Are you saying that both of these approaches are a problem? You can't use VBA code (macros) for some reason? And you need this to work for people who are not using Excel 365?
 
Upvote 0
Some of the orgs of the people using the form don't allow macros so the location feature doesn't work.
 
Upvote 0
...and they are not using Excel 365? Is that correct? If so, do you know what version(s) are being used?

If they have 365 but don't allow macros, then the easy solution is to use the LAMBDA version of the PtInPoly routine. If they don't allow macros and they are not using Excel 365 (which gives us LAMBDA), then a much messier solution would be needed.
 
Upvote 0
Some use office 365 while others use older versions. I guess the LAMBDA formula would be ideal since it will be compatible most partners I work with.
 
Upvote 0
Below is a worksheet using the conversion of Rick's code into a LAMBDA form that @lrobbo314 offered in post #24. Grab that formula by clicking on the clipboard icon in the upper right of the code posted. Then establish a function name using Excel's Name Manager (Formula > Name Manager, then click New, assign a Name, and paste the code into the "Refers to:" field)...it will resemble this:
1688594204016.png

Click Close to confirm, and then the LAMBDA function you just created will be available in the spreadsheet. You see in this example, I named the LAMBDA function "PtInPolyL"...the L at the end providing a clue (to me) that the function being called is actually a LAMBDA function. Other than that, the arguments are the same as in the original implementation. So a working version of the main processing worksheet looks like this:
Cell Formulas
RangeFormula
E3:E19E3=UNIQUE(Table_TPS_AllDivs[Division])
F3:F19F3=MIN(FILTER(FILTER(Table_TPS_AllDivs,Table_TPS_AllDivs[Division]=LocationDivisionL!$E3),Table_TPS_AllDivs[#Headers]="Longitude"))
G3:G19G3=MAX(FILTER(FILTER(Table_TPS_AllDivs,Table_TPS_AllDivs[Division]=LocationDivisionL!$E3),Table_TPS_AllDivs[#Headers]="Longitude"))
H3:H19H3=MIN(FILTER(FILTER(Table_TPS_AllDivs,Table_TPS_AllDivs[Division]=LocationDivisionL!$E3),Table_TPS_AllDivs[#Headers]="Latitude"))
I3:I19I3=MAX(FILTER(FILTER(Table_TPS_AllDivs,Table_TPS_AllDivs[Division]=LocationDivisionL!$E3),Table_TPS_AllDivs[#Headers]="Latitude"))
K3:K5K3=LET(potdivs,($B$4>=$H$3:$H$19)*($B$4<=$I$3:$I$19)*($C$4>=$F$3:$F$19)*($C$4<=$G$3:$G$19),FILTER(E3#,potdivs=1))
B4:C4B4=TEXTSPLIT(TEXTBEFORE(TEXTAFTER(WEBSERVICE("http://geocoder.ca/?locate=" & SUBSTITUTE(B3," ","+")&"%2c+"&C3&"%2c+Ontario&geoit=GeoCode+it!"),"""ICBM"" content="""),""""),", ")+0
B5B5=INDEX(K3#,MATCH("In Polygon",$L$3:$L$19,0))
B6:C6B6=FILTER(Table_TPS_Police_Divisions_Top[[ADDRESS]:[CITY]],Table_TPS_Police_Divisions_Top[DIV]=IF(LEFT(B5,3)="D52","D52",B5))
L3:L19L3=IF(K3<>"",PtInPolyL($C$4,$B$4,FILTER(Table_TPS_AllDivs[[Longitude]:[Latitude]],Table_TPS_AllDivs[Division]=LocationDivisionL!K3)),"")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
TPS_AllDivs!ExternalData_18=TPS_AllDivs!$A$1:$D$24438F3:I19

This version should work for those using Excel 365, regardless of whether they are allowed to run VBA code...so you may want to make this the default 365 version. I've replaced the earlier version of the full Excel file with one that includes both versions of the location-determining worksheet: one relying on the VBA code, the other relying on the LAMBDA function:
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top