This repository contains a set of scripts designed to help you work with segmented trees within a plot area. The scripts allow you to select areas of interest, extract coordinates, and filter files based on boundaries.
This script processes a plot boundary polyline to create polygon representations of the plot area, with options to expand or shrink the plot area using buffers.
- Converts a
LineStringpolyline to aPolygon. - Expands or shrinks the polygon area by a specified buffer distance.
- Converts polygons back to polylines for visualization.
- Reprojects data to a specified CRS (EPSG:32755 - UTM Zone 55S, suitable for Queensland).
- Outputs results as GeoJSON files.
- A GeoJSON file containing the polyline representing the plot boundary.
expand_distance(float): Positive value to expand the plot area.shrink_distance(float): Negative value to shrink the plot area.
GeoJSON files saved to the specified output directory:
original_plot_polygon.geojsonexpanded_plot_polygon.geojson(if expansion is applied)shrunk_plot_polygon.geojson(if shrinking is applied)original_plot_polyline.geojsonexpanded_plot_polyline.geojsonshrunk_plot_polyline.geojson
output_dict = process_plot(
file_path=r"C:\path\to\your\Plot_circumference.geojson",
shrink_distance=-5 # Shrink by 5m
)
output_dir = r"C:\path\to\output\directory"
save_to_geojson(output_dict, output_dir)This script cleans and standardizes filenames in a directory by removing unwanted characters and ensuring consistent naming conventions.
- Removes special characters and spaces from filenames.
- Standardizes filename format for consistent processing.
- Processes all files in a specified directory.
- Creates a backup log of original vs. cleaned filenames.
- A directory containing files with inconsistent or problematic filenames.
- Files with cleaned, standardized names in the same directory.
- A log file showing the mapping of original to cleaned filenames.
input_directory = r"C:\path\to\your\messy_files"
clean_filenames(input_directory)This script extracts the coordinates of tree stems from .las files and saves them into a CSV file. It calculates the centroid of a defined cross-section of each tree.
- Processes
.lasfiles in a specified folder. - Filters points based on a cross-section height range (
0.5mabove the minimum Z value). - Calculates the centroid (
center_x,center_y) of points within the cross-section. - Saves results to a CSV file.
- A folder containing
.lasfiles of segmented trees. - Cross-section height: 0.5m above the minimum Z value.
- A CSV file named
tree_locations.csvwith the following columns:filename: The name of the.lasfile processed.center_x: X-coordinate of the tree center.center_y: Y-coordinate of the tree center.
folder_path = r"C:\path\to\your\extracted_trees_folder"
output_csv = r"C:\path\to\output\directory\tree_locations.csv"| filename | center_x | center_y |
|---|---|---|
| tree_01_fixed.las | 654321.123 | 1234567.456 |
| tree_02_fixed.las | 654328.321 | 1234573.789 |
| ... | ... | ... |
This script checks which trees (based on their center coordinates) fall within a specified plot boundary and saves the valid filenames to a CSV file.
- Loads a polygon plot boundary from a GeoJSON file.
- Loads tree center points from a CSV file generated by
coordinate_extraction.py. - Checks if points fall within the plot boundary.
- Saves filenames of valid points to a new CSV file.
- Plot boundary: A GeoJSON file containing a polygon or polyline plot boundary (e.g.,
shrunk_plot_polygon.geojson). - Tree center points: A CSV file containing filenames and their respective
center_xandcenter_ycoordinates (e.g.,tree_locations.csv). - CRS: The CRS of the plot boundary (should match the CRS of the input points).
- A CSV file named
trees_within_plot.csvcontaining:filename: The filenames of the trees within the plot boundary.
plot_geojson_path = r"C:\path\to\your\shrunk_plot_polygon.geojson"
csv_path = r"C:\path\to\your\tree_locations.csv"
output_csv_path = r"C:\path\to\output\directory\trees_within_plot.csv"The output CSV will have a simple structure:
| filename |
|---|
| tree_01_fixed.las |
| tree_02_fixed.las |
| ... |
This script checks which trees (based on their center coordinates) fall within a specified plot boundary and saves the valid filenames to a CSV file.
- Loads a polygon plot boundary from a GeoJSON file.
- Loads tree center points from a CSV file generated by
coordinate_extraction.py. - Checks if points fall within the plot boundary.
- Saves filenames of valid points to a new CSV file.
- Plot boundary: A GeoJSON file containing a polygon or polyline plot boundary (e.g.,
shrunk_plot_polygon.geojson). - Tree center points: A CSV file containing filenames and their respective
center_xandcenter_ycoordinates (e.g.,tree_locations.csv). - CRS: The CRS of the plot boundary (should match the CRS of the input points).
- A CSV file named
trees_within_plot.csvcontaining:filename: The filenames of the trees within the plot boundary.
plot_geojson_path = r"C:\path\to\your\shrunk_plot_polygon.geojson"
csv_path = r"C:\path\to\your\tree_locations.csv"
output_csv_path = r"C:\path\to\output\directory\trees_within_plot.csv"The output CSV will have a simple structure:
| filename |
|---|
| tree_01_fixed.las |
| tree_02_fixed.las |
| ... |
This script efficiently copies tree files based on a CSV selection list. It supports resumable operations and separates files into "selected" and "leftover" folders.
- Resumable: Automatically skips files already processed if the script is interrupted.
- Dual sorting: Copies CSV-listed files to "selected" folder, others to "leftover" folder.
- Statistics tracking: Shows detailed progress and completion summary.
- CSV file: A CSV file containing a column named
filename(e.g.,trees_within_plot.csv). - Source folder: The folder where the original tree files are located.
- Destination folders: Two folders - one for selected files, one for leftovers.
- Files from CSV copied to the "selected" folder.
- All other files copied to the "leftover" folder.
- Progress tracking and completion statistics.
csv_path = r"C:\path\to\trees_within_plot.csv"
source_folder = r"C:\path\to\extracted_trees_folder"
destination_folder = r"C:\path\to\selected_trees"
leftover_folder = r"C:\path\to\leftover_trees"This is a simplified script for quickly copying only leftover files when you've already processed the selected files separately. Useful for completing interrupted operations from older versions of the copy script.
- Simple and fast: Only processes leftover files (not in CSV selection).
- Resume-friendly: Skips files already in the leftover folder.
- Minimal overhead: Streamlined for quick execution when you only need leftovers.
- When you've already copied selected files using an older script version.
- For completing interrupted operations where only leftovers remain.
- Quick one-time usage when you don't need the full functionality.
csv_path = r"C:\path\to\trees_within_plot.csv"
source_folder = r"C:\path\to\extracted_trees_folder"
leftover_folder = r"C:\path\to\leftover_trees"Total files in source: 5000
Files in CSV (to skip): 150
Already in leftover folder: 1200
Done! Copied 3650 new leftover files, skipped 1200 already processed
- Modify paths and parameters within each script to suit your project.
- Run scripts in order:
polyline.py→name_cleaner.py(if needed) →coordinate_extraction.py→select_in_boundaries.py→file_copy_new_folder.py
- Alternative for leftovers only: Use
quick_leftovers.pyif you've already processed selected files. - Resume capability: All copy scripts can be safely restarted if interrupted.
- Check your output directories to verify results.
- This script was made and tested with Python 3.11
- Required libraries:
geopandasshapelylaspynumpypandasdatetime