Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
FranciscoHoff
Frequent Visitor

Iteration over two tables with lat and lon data

I have an task for something really challenging. The idea is to execute the logic described in this link.

 

Given the following tables:

table "points"

FranciscoHoff_1-1655994950207.png

table "polygons"

FranciscoHoff_0-1655994928522.png

 

which represents the following plane:

FranciscoHoff_3-1655996620767.png

Find out if the point is inside any polygon, and which polygon it would be. I believe the logic would be something like:

  1. Iterate through each of the points in the points table.
  2. Within each point iteration, iterate through each of the polygons in the polygon table.
  3. For each polygon, iterate through each of its vertices, seeking its coordinates and the coordinates of the next vertex (which will represent the segment of one of the sides of the polygon) and then run the check.

 

So it would be a main iteration (over the point table) with two nested iterations (one for each polygon, and one for each vertex of that polygon). In the iteration of the vertices, the main calculation is performed.

 

The main calculation is based on the orientation concept for three ordered points.

 

Basically, four coordinates are used in this main calculation: [X, Y] from the point being iterated (P), [X, Y] from vertex n of the polygon (Vn) and [X, Y] from vertex n+1 ( Vn+1). In the last vertex iteration, the next vertex to be considered will be the first vertex. The fourth coordinate would be the X value of the point being iterated, and the Y with the value of 1000 (we'll call it Q).

 

The calculation is four repetitions of the following formula (just changing the variables):


((A[Y] - B[Y]) * (C[X] - A[X])) - ((A[X] - B[X]) * (C[Y] - A[Y]))


Each repetition can be called by On (O1, O2, O3 and O4).

The variables of each repetition are:

O1
A = Vn
B = Vn+1
C = P

 

O2
A = Vn
B = Vn+1
C = Q

 

O3
A = P
B = Q
C = Vn

 

O4
A = P
B = Q
C = Vn+1

 

The condition that will assert that a given point is inside the iterated polygon will be when any of the following is true, at the end of any iteration over any of the vertices:

 

  • (O1 = O2) and (O3 = O4)
  • (O1 = 0) and (ON_SEGMENT)
  • (O2 = 0) and (ON_SEGMENT)
  • (O3 = 0) and (ON_SEGMENT)
  • (O4 = 0) and (ON_SEGMENT)

The verification that returns true for ON_SEGMENT is:

B[x] <= max({A[X], C[X]}) &&
B[x] >= min({A[X], C[X]}) &&
B[Y] <= max({A[Y], C[Y]}) &&
B[Y] >= min({A[Y], C[Y]})

 

On getting true for the above condition, 1 is added to a counter. At the end of the iteration over all vertices of the polygon, if the counter value is odd then the point is inside the polygon.

 

I know it's a lot but the concept can be better understood in the link I provided above. The implementation of this calculation within Power Query would be very interesting, as Power BI does not have any native tool that performs the intersection of points and polygons distributed geographically and could open doors to even more robust analysis. Anyone accept the challenge?

 

 

1 REPLY 1
v-stephen-msft
Community Support
Community Support

Hi @FranciscoHoff ,

 

Hope the following similar blogs/videoes helps you. List.Generate() might be a good way.

Easy Looping in Power BI/Power Query - YouTube

List.Generate() and Looping in PowerQuery - Exceed

Iteration in DAX - Microsoft Power BI Community

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors