Forum Discussion
Getting what Name or Id is a point inside multiple Polygons
Greetings, i managed to get an algorithm that calculates if a point (LAT, LONG) is inside a polygon (arranged as an LAT/LONG edges table). It returns True or False, but now i also need whats the name of the polygon the point its in.
If i say it to return the FIRSTNONBLANK(Poligonos[name], Poligonos[Name]), it just returns the first check on all the polygons it did, so its the same name for all. I need it to work by blocks, since i have a "name" column for each polygon, so if it returns true on that block, it goes outside the loop and give me that "name" as the result.
Im also trying to optimize the code so it doesnt have to test every polygon for each point. Its thousand points against about 100 polygons wich each one with about 1000 points/edges (it takes a lot to calculate). I have a "zone" name on the points table and a "zone" on the polygons, so i could match those first before go into the loop.
My DAX code for the algorirthm
The edges tables with x, y, meaning lat and long. (doing this table from a .KML was some work)
Hi DanSanDST , Thank you for reaching out to the Microsoft Community Forum.
We reproduced your setup and built a DAX calculated column that filters polygons by zone, groups edges by polygon, applies the ray-casting algorithm per group and returns the first polygon that contains the point. If none match, it returns Outside. This gives you exactly what you need, dynamically, without hardcoding.
Please check the attached .pbix file for reference.
That said, this approach won't scale well. DAX has no spatial engine and evaluating every polygon’s edges per point is expensive. For larger datasets, I recommend moving this logic to Power Query or a Fabric notebook, where spatial operations are more efficient and maintainable.
9 Replies
- lbendlin
Super User
That algorithm will only work for very simple polygons (rectangles, pretty much) and will likely fail for real world shapes that have bulges and indents and inclusions etc.
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.
Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523 - DanSanDST
Helper I
The algoritm actually works for the sample data im using (a single zone) i havent tested it for all the zones since it takes some time to calculate it.
first what im trying to do is this (calculated column):
Making the algorithm loop for "id zone" then "id polygon", and the column should return the id zone, id polygon and polygon name so i can split the column in the 3 values.
some data example to calculate would be: Polygon edges table for squares, please asume its a complex polygon, whats important is to get the other columns when the algorithm calculates with all the rows on this polygons table.
id zone id pol pol name lat0 long0 lat1 long1 1 1 A 1 1 1 2 1 1 A 1 2 2 2 1 1 A 2 2 2 1 1 1 A 2 1 1 1 1 2 B 2 2 2 4 1 2 B 2 4 4 4 1 2 B 4 4 4 2 1 2 B 4 2 2 2 2 3 C 4 4 4 8 2 3 C 4 8 8 8 2 3 C 8 8 8 4 2 3 C 8 4 4 4 3 4 D 8 8 8 16 3 4 D 8 16 16 16 3 4 D 16 16 16 8 3 4 D 16 8 8 8 points to test
point id lat long 1 20 30 2 3 3 3 30 40 4 5 5 5 10 10 6 6 6 7 50 60 8 1.5 1.5 Expected result:
point id lat long Point in Poly 1 20 30 Outside 2 3 3 1 2 B 3 30 40 Outside 4 5 5 2 3 C 5 10 10 3 4 D 6 6 6 2 3 C 7 50 60 Outside 8 1.5 1.5 1 1 A like i said, if the point is inside or not is covered, im missing first: getting the zone and name data for when it finds what polygon is into. And trying to at least filter the calculation for each zone, since both the polygon and values to find tables have a "zone", but it would need to be a loop so it searches the data for each zone with its zone on the polygon table. I cant have a table for each polygon since i need it to be automated, for when new polygons are added or removed.
- lbendlin
Super User
please check your zone sample data. All of the "zones" are lines, not rectangles. A point cannot be "in" a zone in that case.
- DanSanDST
Helper I
The algorithm works with an Edges table, meaning in a line you need x0/y0 and x1/y1 to make a segment.
The real polygons are complex and the algorithm works fine
- v-hashadapu
Community Support
Hi DanSanDST , hope you are doing great. May we know if your issue is solved or if you are still experiencing difficulties. Please share the details as it will help the community, especially others with similar issues.
- v-hashadapu
Community Support
Hi DanSanDST , Hope you're doing okay! May we know if it worked for you, or are you still experiencing difficulties? Let us know — your feedback can really help others in the same situation.