@@ -223,8 +223,7 @@ def calculate_part_2(self, plot=True):
223223 simplex_id = list (edges [current_edge ]["simplices" ].keys ())[0 ]
224224 simplex = d .simplices [simplex_id ]
225225 if (
226- edges [current_edge ]["length" ] > 1.5 * d_max
227- and edges [current_edge ]["simplices" ][simplex_id ] > 90
226+ edges [current_edge ]["length" ] > 1.5 * d_max and edges [current_edge ]["simplices" ][simplex_id ] > 90
228227 ) or edges [current_edge ]["simplices" ][simplex_id ] > 180 - 180 / 16 :
229228
230229 # delete edge and the simplex start
@@ -260,9 +259,7 @@ def find_cycles(self):
260259 if len (cycles ) == 1 :
261260 geom = Polygon (self .d .points [cycles [0 ]])
262261 else :
263- geom = MultiPolygon (
264- [Polygon (self .d .points [c ]) for c in cycles if len (c ) >= 3 ]
265- )
262+ geom = MultiPolygon ([Polygon (self .d .points [c ]) for c in cycles if len (c ) >= 3 ])
266263 except Exception as e :
267264 print (e , cycles )
268265 return None
@@ -332,13 +329,7 @@ def generate_boundaries(df, x="x_location", y="y_location", cell_id="segger_cell
332329 res = []
333330 group_df = df .groupby (cell_id )
334331 for cell_id , t in tqdm (group_df , total = group_df .ngroups ):
335- res .append (
336- {
337- "cell_id" : cell_id ,
338- "length" : len (t ),
339- "geom" : generate_boundary (t , x = x , y = y ),
340- }
341- )
332+ res .append ({"cell_id" : cell_id , "length" : len (t ), "geom" : generate_boundary (t , x = x , y = y )})
342333
343334 return gpd .GeoDataFrame (
344335 data = [[b ["cell_id" ], b ["length" ]] for b in res ],
@@ -350,7 +341,6 @@ def generate_boundaries(df, x="x_location", y="y_location", cell_id="segger_cell
350341def generate_boundary (t , x = "x_location" , y = "y_location" ):
351342 if len (t ) < 3 :
352343 return None
353-
354344 bi = BoundaryIdentification (t [[x , y ]].values )
355345 bi .calculate_part_1 (plot = False )
356346 bi .calculate_part_2 (plot = False )
@@ -359,24 +349,9 @@ def generate_boundary(t, x="x_location", y="y_location"):
359349 return geom
360350
361351
362- if __name__ == "__main__" :
363- points = np .array (
364- [
365- [0 , 0 ], # Point 0
366- [3 , 0 ], # Point 1
367- [0 , 4 ], # Point 2
368- [5 , 5 ], # Point 3
369- [1 , 6 ], # Point 4
370- ]
371- )
372352
373- simplices = triangles = np .array (
374- [
375- [0 , 1 , 2 ], # Triangle formed by points 0, 1, 2
376- [1 , 3 , 4 ], # Triangle formed by points 1, 3, 4
377- ]
378- )
379353
380- angles = triangle_angles_from_points (points , triangles )
381- print ("Angles of each triangle (in degrees):" )
382- print (angles )
354+ def extract_largest_polygon (geom ):
355+ if isinstance (geom , MultiPolygon ):
356+ return max (geom .geoms , key = lambda p : p .area ) # Keep the largest polygon
357+ return geom # Keep the original polygon if it's already a Polygon
0 commit comments