Forum Discussion

DanSanDST's avatar
DanSanDST
Icon for Helper I rankHelper I
1 year ago
Solved

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

  • 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

  • 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 zoneid polpol namelat0long0lat1long1
    11A1112
    11A1222
    11A2221
    11A2111
    12B2224
    12B2444
    12B4442
    12B4222
    23C4448
    23C4888
    23C8884
    23C8444
    34D88816
    34D8161616
    34D1616168
    34D16888

     

    points to test

    point idlatlong
    12030
    233
    33040
    455
    51010
    666
    75060
    81.51.5

     

    Expected result:

    point idlatlongPoint in Poly
    12030Outside
    2331 2 B
    33040Outside
    4552 3 C
    510103 4 D
    6662 3 C
    75060Outside
    81.51.51 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's avatar
      lbendlin
      Icon for Super User rankSuper 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's avatar
        DanSanDST
        Icon for Helper I rankHelper 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's avatar
    v-hashadapu
    Icon for Community Support rankCommunity 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's avatar
    v-hashadapu
    Icon for Community Support rankCommunity 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.