Jun-24-2023, 02:45 PM
(This post was last modified: Jun-24-2023, 08:08 PM by snippsat.
Edit Reason: Fix code tag
)
Hello everyone, I'm trying to cover an entire area with a step size of 0.4. I would like that with each change of "parallel line" the following point is that of a side. Currently the program will always look for the lowest point. I put pictures to make it more clear ;)
Thank you in advance
The result I get is picture 1
The result I would like to get is picture 2
Thank you in advance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import math import matplotlib.pyplot as plt from shapely.geometry import Point, Polygon def boustrophedon_path(latitude_orange, longitude_orange, step_size): path = [] # Create the polygon from the coordinates poly = Polygon( zip (latitude_orange, longitude_orange)) # Find the bounds of the polygon min_lat = min (latitude_orange) max_lat = max (latitude_orange) min_lon = min (longitude_orange) max_lon = max (longitude_orange) # Iterate through the zone in parallel lines current_lat = min_lat # latitude actuelle = latitude minimale while current_lat < = max_lat: # tant que latitude actuelle <= latitude maximale current_lon = min_lon # longitude actuelle = longitude minimale while current_lon < = max_lon: # tant que longitude actuelle <= longitude maximale point = Point(current_lat, current_lon) # ajout de point( latitude actuelle, longitude actuelle) if poly.contains(point): # si le polygone contient point path.append((current_lat, current_lon)) # alors le point est ajouté au chemin current_lon + = step_size # longitude actuelle += un pas de 0.4 current_lat + = step_size # latitude actuelle += pas de 0.4 return path # retourne le chemin latitude_blue = [ 0 , 0 , 2 , 3 , 3 , 0 ] longitude_blue = [ 0 , 4 , 8 , 4 , 0 , 0 ] # Espacement entre les droites parallèles (40 cm) step_size = 0.6 # En degrés # Check if latitude_blue and longitude_blue have the same length if len (latitude_blue) ! = len (longitude_blue): print ( "Error: The latitude and longitude lists have different lengths." ) else : # Create the blue polygon using Shapely blue_polygon = Polygon( zip (latitude_blue, longitude_blue)) # Calculate the coordinates for the orange polygon by shrinking the blue polygon by 0.2 units orange_polygon = blue_polygon. buffer ( - 0.2 ) # Extract the coordinates of the orange polygon latitude_orange, longitude_orange = orange_polygon.exterior.xy # Calcul du chemin boustrophédon path = boustrophedon_path(latitude_orange, longitude_orange, step_size) # Préparation des données pour le tracé x = [point[ 0 ] for point in path] # Longitude y = [point[ 1 ] for point in path] # Latitude print (x,y) for i,j in zip (x,y): plt.plot(x,y,color = 'red' ,marker = 'x' ) plt.plot(latitude_blue, longitude_blue, color = 'blue' ) plt.plot(latitude_orange, longitude_orange, color = 'orange' ) plt.title( 'Polygons' ) plt.grid( True ) plt.show() |
The result I would like to get is picture 2