INPOLY

=INPOLY(LATITUDE,LONGITUDE,POLYGON)

LATITUDE
Latitude of lookup location
LONGITUDE
Longitude of lookup location
POLYGON
2 column range with latitude and longitude coordinates of a polygon

INPOLY will return if a given latitude and longitude fall within a polygon. The polygon is a 2 column range/array in order of latitude then longitude.

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,957
Office Version
  1. 365
Platform
  1. Windows
INPOLY will return if a given latitude and longitude falls within a polygon. The polygon is a 2 column range/array in order of latitude then longitude.

This function was a conversion of a VBA algorithm written by @Rick Rothstein in this post. The example came from this thread.

In the example we used Power Query to get the points of the polygon. But, the polygon could just be a range of latitude and longitude coordinates on any sheet. The user will provide latitude and longitude coordinates of a given location and the function will return whether the location falls within the given polygon.

Excel Formula:
=LAMBDA(LATITUDE,LONGITUDE,POLYGON,
    LET(r,ROWS(POLYGON),
        IF(NOT(AND(INDEX(POLYGON,1,1)=INDEX(POLYGON,r,1),INDEX(POLYGON,1,2)=INDEX(POLYGON,r,2))),
            "Invalid Polygon",
            IF(MOD(SUM(
                SCAN(0,SEQUENCE(r-1),
                    LAMBDA(s,c,
                        IF(XOR(INDEX(POLYGON,c,2)>LONGITUDE,INDEX(POLYGON,c+1,2)>LONGITUDE),
                            LET(
                                m,(INDEX(POLYGON,c+1,1)-INDEX(POLYGON,c,1))/(INDEX(POLYGON,c+1,2)-INDEX(POLYGON,c,2)),
                                b,(INDEX(POLYGON,c,1)*INDEX(POLYGON,c+1,2)-INDEX(POLYGON,c,2)*INDEX(POLYGON,c+1,1))/(INDEX(POLYGON,c+1,2)-INDEX(POLYGON,c,2)),
                                IF(m*LONGITUDE+b>LATITUDE,s+1,s)
                            ),0
                        )
                    )
                )
            ),2),"In Polygon","Not In Polygon"
            )
        )
    )
)

InPoly Final.xlsm
ABCDEFGHIJ
1Street AddressCityBounding Boxes
2User Inputs45 Esandar DrTorontoDivisionLongMinLongMaxLatMinLatMaxIn Polygon
3Lat-Long of address43.706916-79.356874D11-79.51221491-79.4286474443.6317078443.6751081Not In Polygon
4Location in:D53D12-79.53960143-79.4559692843.6626422443.72469095Not In Polygon
5Send to:75 Eglinton Av WTorontoD13-79.4697805-79.4073370543.6672694943.71926757Not In Polygon
6D14-79.44622267-79.3915620143.6262715443.67543759Not In Polygon
7D22-79.60873798-79.4691308543.5810045643.68413286Not In Polygon
8D23-A-79.63926019-79.5102363343.6646309143.76302651Not In Polygon
9D23-B-79.56540241-79.5653683543.7373598443.73742627Not In Polygon
10D31-79.58521503-79.4725302943.7128231543.78390308Not In Polygon
11D32-79.4844639-79.3811068643.7108129743.80326709Not In Polygon
12D33-79.39632203-79.3026482343.716241843.81565937Not In Polygon
13D41-79.31979738-79.2191349843.6710724143.7793425Not In Polygon
14D42-79.34132113-79.1339120543.7683719143.85546297Not In Polygon
15D43-79.26476975-79.1154546543.7082867443.80463003Not In Polygon
16D51-79.3867913-79.3376722243.6332101943.67533123Not In Polygon
17D52-A-79.40490262-79.349390543.6119629843.63534512Not In Polygon
18D52-B-79.40387398-79.3742051143.6370161743.67023498Not In Polygon
19D53-79.4298194-79.3354428243.6666658743.73036965In Polygon
20D55-79.36637166-79.280028543.6127405443.72629795Not In Polygon
User_Input
Cell Formulas
RangeFormula
E3:E20E3=UNIQUE(Table_tbl_Main[DIVX])
F3:F20F3=MINIFS(Table_tbl_Coords[Longitude],Table_tbl_Coords[DIVX],User_Input!E3#)
G3:G20G3=MAXIFS(Table_tbl_Coords[Longitude],Table_tbl_Coords[DIVX],User_Input!E3#)
H3:H20H3=MINIFS(Table_tbl_Coords[Latitude],Table_tbl_Coords[DIVX],User_Input!E3#)
I3:I20I3=MAXIFS(Table_tbl_Coords[Latitude],Table_tbl_Coords[DIVX],User_Input!E3#)
B3:C3B3=TEXTSPLIT(TEXTBEFORE(TEXTAFTER(WEBSERVICE("http://geocoder.ca/?locate=" & SUBSTITUTE(B2," ","+")&"%2c+"&C2&"%2c+Ontario&geoit=GeoCode+it!"),"""ICBM"" content="""),""""),", ")+0
B4B4=INDEX(E3#,MATCH("In Polygon",$J$3:$J$20,0))
B5:C5B5=FILTER(Table_tbl_Main[[ADDRESS]:[CITY]],Table_tbl_Main[DIVX]=User_Input!B4)
J3:J20J3=INPOLY($B$3,$C$3,FILTER(Table_tbl_Coords[[Longitude]:[Latitude]],Table_tbl_Coords[DIVX]=User_Input!E3))
Dynamic array formulas.
 
Upvote 0
Not sure why I used the combination of SUM + SCAN instead of just using REDUCE...

Excel Formula:
=LAMBDA(XC,YC,P,
        IF(MOD(REDUCE(0,SEQUENCE(ROWS(P)-1),
            LAMBDA(s,c,
                IF(XOR(INDEX(P,c,1)>XC,INDEX(P,c+1,1)>XC),
                    LET(
                        m,(INDEX(P,c+1,2)-INDEX(P,c,2))/(INDEX(P,c+1,1)-INDEX(P,c,1)),
                        b,(INDEX(P,c,2)*INDEX(P,c+1,1)-INDEX(P,c,1)*INDEX(P,c+1,2))/(INDEX(P,c+1,1)-INDEX(P,c,1)),
                        IF(m*XC+b>YC,s+1,s+0)
                    ),s
                )
            )),2),"In Polygon","Not in Polygon"
        ))
 

Forum statistics

Threads
1,223,445
Messages
6,172,176
Members
452,446
Latest member
walkman99

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