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​
 
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:
View attachment 94781
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:
Thank you so much. You're awesome.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Glad to help. I had a look at creating a version that would work for earlier versions of Excel. I wasn't sure about the preferred approach for accomplishing this. One idea follows the path used for the Excel 365 solution, which identifies divisions that might contain the point of interest based on the extremes of bounding boxes around each division. Ultimately, this would require some conditional statements either to perform the edge-crossing checks if the division were identified, or not to consider the division at all. Instead, I opted to ignore the bounding boxes (I left them in the worksheet, but they are not used) and perform the edge-crossing check on all line segments of all divisions because it is simple to apply and avoids the additional logic checks.

I've utilized the PQ M code from @lrobbo314 , as it more elegantly extracts data from the GeoJSON file. The initial query (ExtractGeoJSON) is created as a connection only type, and it contains all of the data needed...a table describing police division/address and subtables holding the polygon coordinates for each division. The ShapefileCoord query utilizes ExtractGeoJSON to create the long coordinates table. And the BoundingBox query determines the min and max latitude and longitude from the ShapefileCoord query and merges those results with the base table initially created by ExtractGeoJSON to create a combination police division/address/bounding box table.

Helper columns to the right of the ShapefileCoord data compute the slope and intercept of each line segment, and the "Crossings" column determines whether the ray cast out from the target location crosses the line segment.

Helper cells on the BoundingBox worksheet take the user-inputted address and apply the WEBSERVICE function to obtain geocoding data. Those data are further processed to extract the latitude and longitude in a few cells using text search/extraction functions, and then the final coordinates are made available to the main sheet (LocationDiv) that the user interacts with. Returning to the BoundingBox worksheet, which holds one of the tables generated by PQ, to the right of that table is a column holding a formula that determines whether, for each division, the number of ray edge crossings on the ShapefileCoord worksheet is odd or even (with odd indicating the target point is located in that division). Ultimately, it is this column ([BoundingBox]J2:J19) that is examined to determine the "Location in:" on the main worksheet.

I haven't tested this exhaustively, but it appears to generate the same results as the Excel365 version. The functions used in this worksheet should work for Excel 2013 and later, I believe.
 
Upvote 0
Glad to help. I had a look at creating a version that would work for earlier versions of Excel. I wasn't sure about the preferred approach for accomplishing this. One idea follows the path used for the Excel 365 solution, which identifies divisions that might contain the point of interest based on the extremes of bounding boxes around each division. Ultimately, this would require some conditional statements either to perform the edge-crossing checks if the division were identified, or not to consider the division at all. Instead, I opted to ignore the bounding boxes (I left them in the worksheet, but they are not used) and perform the edge-crossing check on all line segments of all divisions because it is simple to apply and avoids the additional logic checks.

I've utilized the PQ M code from @lrobbo314 , as it more elegantly extracts data from the GeoJSON file. The initial query (ExtractGeoJSON) is created as a connection only type, and it contains all of the data needed...a table describing police division/address and subtables holding the polygon coordinates for each division. The ShapefileCoord query utilizes ExtractGeoJSON to create the long coordinates table. And the BoundingBox query determines the min and max latitude and longitude from the ShapefileCoord query and merges those results with the base table initially created by ExtractGeoJSON to create a combination police division/address/bounding box table.

Helper columns to the right of the ShapefileCoord data compute the slope and intercept of each line segment, and the "Crossings" column determines whether the ray cast out from the target location crosses the line segment.

Helper cells on the BoundingBox worksheet take the user-inputted address and apply the WEBSERVICE function to obtain geocoding data. Those data are further processed to extract the latitude and longitude in a few cells using text search/extraction functions, and then the final coordinates are made available to the main sheet (LocationDiv) that the user interacts with. Returning to the BoundingBox worksheet, which holds one of the tables generated by PQ, to the right of that table is a column holding a formula that determines whether, for each division, the number of ray edge crossings on the ShapefileCoord worksheet is odd or even (with odd indicating the target point is located in that division). Ultimately, it is this column ([BoundingBox]J2:J19) that is examined to determine the "Location in:" on the main worksheet.

I haven't tested this exhaustively, but it appears to generate the same results as the Excel365 version. The functions used in this worksheet should work for Excel 2013 and later, I believe.
This works perfectly. Thanks. I really appreciate this.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
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