CombineCropRows

Qualified name: crop_row_connector.combine_crop_rows.CombineCropRows

class CombineCropRows(angle_tolerance, vegetation_threshold, min_unhealthy_vegetation_length, max_segment_length, distance_tolerance, *, output_path_connected_crop_rows=None, output_path_vegetation_points=None, output_path_healthy_vegetation_segments=None, output_path_unhealthy_vegetation_segments=None, max_workers=None)[source]

Bases: object

Combine crop rows from different tiles into one line.

Parameters:
  • angle_tolerance (float) – Maximum angle difference in degrees for rows to be considered for connection.

  • vegetation_threshold (float) – Grayscale threshold (0-255) for classifying vegetation as healthy.

  • min_unhealthy_vegetation_length (float) – Minimum spatial extent in meters for unhealthy vegetation segments to be recorded.

  • max_segment_length (float) – Maximum length in meters for vegetation classification segments.

  • output_path_connected_crop_rows (Path | None) – Path to save connected crop rows to CSV.

  • output_path_vegetation_points (Path | None) – Path to save all vegetation points associated with connected rows.

  • output_path_healthy_vegetation_segments (Path | None) – Path to save segments with healthy vegetation.

  • output_path_unhealthy_vegetation_segments (Path | None) – Path to save segments with unhealthy vegetation.

  • max_workers (int | None) – Maximum number of parallel workers for processing.

  • distance_tolerance (float)

Methods

connect_2_tiles

Connect two tiles by linking crop rows if their angles are within the allowed tolerance.

connect_crop_rows

Execute the complete crop row connection pipeline.

connect_rows_in_tiles

Connect crop rows across neighboring tiles in the grid.

connected_crop_rows_to_csv

Write the connected crop rows to a csv file.

create_tile_grid

Create a 2D grid of tile numbers in their respective positions, with -1 for empty cells.

length_of_segments

Calculate total lengths of healthy and unhealthy vegetation segments.

load_csv

Load a CSV file containing row information into a NumPy array.

merge_all_points_in_all_crop_rows_remove

Merge all points from connected crop rows into a single DataFrame, removing overlapping points based on a distance tolerance.

separate_healthy_and_unhealthy_vegetation_segments

Define healthy and unhealthy vegetation segments and optionally write them to shapefiles.

separate_row_information_to_tile

Split row information into tile objects keyed by tile number.

connect_2_tiles(tile_1, tile_2)[source]

Connect two tiles by linking crop rows if their angles are within the allowed tolerance.

Parameters:
  • tile_1 (Tile) – The first tile object to connect.

  • tile_2 (Tile) – The second tile object to connect.

Return type:

None

connect_crop_rows(path_row_information, path_points_in_rows)[source]

Execute the complete crop row connection pipeline. And save desired output.

Parameters:
  • path_row_information (Path) – Path to CSV with row information. Format: [tile_number, x_position, y_position, angle, row, x_start, y_start, x_end, y_end]

  • path_points_in_rows (Path) – Path to CSV with vegetation points. Format: [tile_number, row_number, x, y, vegetation]

Return type:

None

connect_rows_in_tiles(grid, tiles, tqdm_disable=False)[source]

Connect crop rows across neighboring tiles in the grid.

For each tile in the grid: - Connect to its right neighbor (if any) - Connect to its bottom neighbor (if any)

After all connections are made: - Add any uncombined rows to the connected crop rows

Parameters:
  • grid (ndarray) – 2D array representing the tile grid. Each entry is a tile number, or -1 for empty cells.

  • tiles (dict[int, Tile]) – Dictionary mapping tile numbers to tile objects.

  • tqdm_disable (bool)

Return type:

None

connected_crop_rows_to_csv(connected_crop_rows)[source]

Write the connected crop rows to a csv file.

Parameters:

connected_crop_rows (ndarray) – 2D array containing connected crop row information.

Return type:

None

create_tile_grid(row_information, tiles)[source]

Create a 2D grid of tile numbers in their respective positions, with -1 for empty cells.

Notes

  • X indexes columns, Y indexes rows (grid[y, x])

  • The grid is padded by 1 row and 1 column to simplify neighbor connections.

Parameters:
  • row_information (ndarray) – 2D array with row information including tile positions.

  • tiles (dict[int, Tile]) – Dictionary mapping tile numbers to tile objects.

Returns:

2D array where each cell contains a tile number if present, otherwise -1.

Return type:

np.ndarray

length_of_segments(segments)[source]

Calculate total lengths of healthy and unhealthy vegetation segments.

Notes

  • Uses segment endpoints instead of point-to-point distances.

  • Each segment is treated as a straight line.

Parameters:

segments (list[dict]) – Output from separate_healthy_and_unhealthy_vegetation_segments.

Returns:

(total_length, healthy_length, unhealthy_length)

Return type:

tuple[float, float, float]

load_csv(path)[source]

Load a CSV file containing row information into a NumPy array.

Notes

  • Each row corresponds to a crop row.

  • Columns contain:

    [tile_number, x_position, y_position, angle, row, x_start, y_start, x_end, y_end, x_mid, y_mid]

Parameters:

path (Path)

Return type:

ndarray

merge_all_points_in_all_crop_rows_remove(connected_crop_rows, DF_vegetation_rows, row_information, tiles)[source]

Merge all points from connected crop rows into a single DataFrame, removing overlapping points based on a distance tolerance.

Parameters:
  • connected_crop_rows (ndarray) – 2D array containing connected crop row information.

  • path_points_in_rows – Path to CSV containing raw vegetation points per row.

  • row_information (ndarray) – Original row information array.

  • tiles (dict[int, Tile]) – Dictionary mapping tile numbers to tile objects.

  • DF_vegetation_rows (DataFrame)

Returns:

Merged and sorted crop rows including vegetation and duplicate information.

Return type:

pd.DataFrame

separate_healthy_and_unhealthy_vegetation_segments(DF_crop_rows)[source]

Define healthy and unhealthy vegetation segments and optionally write them to shapefiles.

Notes

  • Uses self.vegetation_threshold to separate healthy from unhealthy vegetation.

  • Each segment is represented as a line between two points.

  • Returns segments so they can be reused for further analysis (e.g. length calculation).

  • X indexes columns, Y indexes rows (DF_crop_rows[‘x’], DF_crop_rows[‘y’]).

Parameters:

DF_crop_rows (DataFrame) – DataFrame containing at least [‘row’, ‘x’, ‘y’, ‘vegetation’].

Returns:

List of segment dictionaries: { “row_id”: int, “healthy”: list[[[x1, y1], [x2, y2]]], “unhealthy”: list[[[x1, y1], [x2, y2]]] }

Return type:

list[dict]

separate_row_information_to_tile(row_information)[source]

Split row information into tile objects keyed by tile number.

Parameters:

row_information (ndarray) – A NumPy array containing row information for all tiles. Each row corresponds to a crop row and contains the following columns: [tile_number, x_position, y_position, angle, row, x_start, y_start, x_end, y_end]

Returns:

Dictionary mapping each tile number to a tile object.

Return type:

dict[int, Tile]