Advertisement
sombruxo

CortaPoligono

Apr 4th, 2025 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | Source Code | 0 0
  1. from shapely.geometry import LineString, Polygon
  2. # Installation --> pip install shapely
  3. import logging
  4.  
  5. def intersects_polygon_path(polygon_mat, path_mat):
  6.     polygon_points = [tuple(p) for p in polygon_mat]
  7.     path_points = [tuple(p) for p in path_mat]
  8.     logging.debug(f"polygon_points: {polygon_points}")
  9.     logging.debug(f"path_points: {path_points}")
  10.  
  11.     polygon = Polygon(polygon_points)
  12.     result = False
  13.     i = 0
  14.     while (not result) and (i < len(path_points) - 1):
  15.         segment = LineString([path_points[i], path_points[i + 1]])
  16.         result = polygon.intersects(segment)
  17.         logging.debug(f"iteracion: {i}, result: {result}.")
  18.         i += 1
  19.     logging.info(f"result: {result}.")
  20.     return result
  21.  
  22. # Define a rectangular polygon
  23. polygon_mat = [
  24.     [50.097986, 14.410436],
  25.     [50.093511, 14.41595],
  26.     [50.087722, 14.409767],
  27.     [50.084028, 14.4086],
  28.     [50.088344, 14.387347],
  29.     [50.094278, 14.388269],
  30.     [50.097986, 14.410436]
  31. ]
  32.  
  33. # Define a path (sequence of GPS points)
  34. path_mat = [
  35.     [50.105262981329595, 14.267437167464884],
  36.     [50.105262981329595, 14.267437167464884],
  37.     [50.074607754755135, 14.474670112031129],
  38.     [50.0760853, 14.4747344]
  39. ]
  40.  
  41. logging.basicConfig(
  42.     filename='salidaLog.log',
  43.     level=logging.DEBUG,
  44.     encoding='utf-8',
  45.     format='[%(asctime)s] %(process)d-[%(threadName)s-%(thread)d] %(levelname)s %(module)s.%(funcName)s L:%(lineno)d: %(message)s'
  46. )
  47.  
  48. print("Intersects?", intersects_polygon_path(polygon_mat, path_mat))
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement