BERATools 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. beratools/__init__.py +9 -0
  2. beratools/core/__init__.py +0 -0
  3. beratools/core/algo_centerline.py +351 -0
  4. beratools/core/constants.py +86 -0
  5. beratools/core/dijkstra_algorithm.py +460 -0
  6. beratools/core/logger.py +85 -0
  7. beratools/core/tool_base.py +133 -0
  8. beratools/gui/__init__.py +15 -0
  9. beratools/gui/batch_processing_dlg.py +463 -0
  10. beratools/gui/beratools.json +2300 -0
  11. beratools/gui/bt_data.py +487 -0
  12. beratools/gui/bt_gui_main.py +691 -0
  13. beratools/gui/cli.py +18 -0
  14. beratools/gui/gui.json +8 -0
  15. beratools/gui/img/BERALogo.png +0 -0
  16. beratools/gui/img/closed.gif +0 -0
  17. beratools/gui/img/closed.png +0 -0
  18. beratools/gui/img/open.gif +0 -0
  19. beratools/gui/img/open.png +0 -0
  20. beratools/gui/img/tool.gif +0 -0
  21. beratools/gui/img/tool.png +0 -0
  22. beratools/gui/map_window.py +146 -0
  23. beratools/gui/tool_widgets.py +493 -0
  24. beratools/gui_tk/ASCII Banners.txt +248 -0
  25. beratools/gui_tk/__init__.py +20 -0
  26. beratools/gui_tk/beratools_main.py +515 -0
  27. beratools/gui_tk/bt_widgets.py +442 -0
  28. beratools/gui_tk/cli.py +18 -0
  29. beratools/gui_tk/gui.json +8 -0
  30. beratools/gui_tk/img/BERALogo.png +0 -0
  31. beratools/gui_tk/img/closed.gif +0 -0
  32. beratools/gui_tk/img/closed.png +0 -0
  33. beratools/gui_tk/img/open.gif +0 -0
  34. beratools/gui_tk/img/open.png +0 -0
  35. beratools/gui_tk/img/tool.gif +0 -0
  36. beratools/gui_tk/img/tool.png +0 -0
  37. beratools/gui_tk/main.py +14 -0
  38. beratools/gui_tk/map_window.py +144 -0
  39. beratools/gui_tk/runner.py +1481 -0
  40. beratools/gui_tk/tooltip.py +55 -0
  41. beratools/third_party/pyqtlet2/__init__.py +9 -0
  42. beratools/third_party/pyqtlet2/leaflet/__init__.py +26 -0
  43. beratools/third_party/pyqtlet2/leaflet/control/__init__.py +6 -0
  44. beratools/third_party/pyqtlet2/leaflet/control/control.py +59 -0
  45. beratools/third_party/pyqtlet2/leaflet/control/draw.py +52 -0
  46. beratools/third_party/pyqtlet2/leaflet/control/layers.py +20 -0
  47. beratools/third_party/pyqtlet2/leaflet/core/Parser.py +24 -0
  48. beratools/third_party/pyqtlet2/leaflet/core/__init__.py +2 -0
  49. beratools/third_party/pyqtlet2/leaflet/core/evented.py +180 -0
  50. beratools/third_party/pyqtlet2/leaflet/layer/__init__.py +5 -0
  51. beratools/third_party/pyqtlet2/leaflet/layer/featuregroup.py +34 -0
  52. beratools/third_party/pyqtlet2/leaflet/layer/icon/__init__.py +1 -0
  53. beratools/third_party/pyqtlet2/leaflet/layer/icon/icon.py +30 -0
  54. beratools/third_party/pyqtlet2/leaflet/layer/imageoverlay.py +18 -0
  55. beratools/third_party/pyqtlet2/leaflet/layer/layer.py +105 -0
  56. beratools/third_party/pyqtlet2/leaflet/layer/layergroup.py +45 -0
  57. beratools/third_party/pyqtlet2/leaflet/layer/marker/__init__.py +1 -0
  58. beratools/third_party/pyqtlet2/leaflet/layer/marker/marker.py +91 -0
  59. beratools/third_party/pyqtlet2/leaflet/layer/tile/__init__.py +2 -0
  60. beratools/third_party/pyqtlet2/leaflet/layer/tile/gridlayer.py +4 -0
  61. beratools/third_party/pyqtlet2/leaflet/layer/tile/tilelayer.py +16 -0
  62. beratools/third_party/pyqtlet2/leaflet/layer/vector/__init__.py +5 -0
  63. beratools/third_party/pyqtlet2/leaflet/layer/vector/circle.py +15 -0
  64. beratools/third_party/pyqtlet2/leaflet/layer/vector/circlemarker.py +18 -0
  65. beratools/third_party/pyqtlet2/leaflet/layer/vector/path.py +5 -0
  66. beratools/third_party/pyqtlet2/leaflet/layer/vector/polygon.py +14 -0
  67. beratools/third_party/pyqtlet2/leaflet/layer/vector/polyline.py +18 -0
  68. beratools/third_party/pyqtlet2/leaflet/layer/vector/rectangle.py +14 -0
  69. beratools/third_party/pyqtlet2/leaflet/map/__init__.py +1 -0
  70. beratools/third_party/pyqtlet2/leaflet/map/map.py +220 -0
  71. beratools/third_party/pyqtlet2/mapwidget.py +45 -0
  72. beratools/third_party/pyqtlet2/web/custom.js +43 -0
  73. beratools/third_party/pyqtlet2/web/map.html +23 -0
  74. beratools/third_party/pyqtlet2/web/modules/leaflet_193/images/layers-2x.png +0 -0
  75. beratools/third_party/pyqtlet2/web/modules/leaflet_193/images/layers.png +0 -0
  76. beratools/third_party/pyqtlet2/web/modules/leaflet_193/images/marker-icon-2x.png +0 -0
  77. beratools/third_party/pyqtlet2/web/modules/leaflet_193/images/marker-icon.png +0 -0
  78. beratools/third_party/pyqtlet2/web/modules/leaflet_193/images/marker-shadow.png +0 -0
  79. beratools/third_party/pyqtlet2/web/modules/leaflet_193/leaflet.css +656 -0
  80. beratools/third_party/pyqtlet2/web/modules/leaflet_193/leaflet.js +6 -0
  81. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/.codeclimate.yml +14 -0
  82. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/.editorconfig +4 -0
  83. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/.gitattributes +22 -0
  84. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/.travis.yml +43 -0
  85. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/LICENSE +20 -0
  86. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/layers-2x.png +0 -0
  87. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/layers.png +0 -0
  88. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/marker-icon-2x.png +0 -0
  89. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/marker-icon.png +0 -0
  90. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/marker-shadow.png +0 -0
  91. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/spritesheet-2x.png +0 -0
  92. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/spritesheet.png +0 -0
  93. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/images/spritesheet.svg +156 -0
  94. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/leaflet.draw.css +10 -0
  95. beratools/third_party/pyqtlet2/web/modules/leaflet_draw_414/leaflet.draw.js +10 -0
  96. beratools/third_party/pyqtlet2/web/modules/leaflet_rotatedMarker_020/LICENSE +22 -0
  97. beratools/third_party/pyqtlet2/web/modules/leaflet_rotatedMarker_020/leaflet.rotatedMarker.js +57 -0
  98. beratools/tools/Beratools_r_script.r +1120 -0
  99. beratools/tools/Ht_metrics.py +116 -0
  100. beratools/tools/__init__.py +7 -0
  101. beratools/tools/batch_processing.py +132 -0
  102. beratools/tools/canopy_threshold_relative.py +670 -0
  103. beratools/tools/canopycostraster.py +222 -0
  104. beratools/tools/centerline.py +176 -0
  105. beratools/tools/common.py +885 -0
  106. beratools/tools/fl_regen_csf.py +428 -0
  107. beratools/tools/forest_line_attributes.py +408 -0
  108. beratools/tools/forest_line_ecosite.py +216 -0
  109. beratools/tools/lapis_all.py +103 -0
  110. beratools/tools/least_cost_path_from_chm.py +152 -0
  111. beratools/tools/line_footprint_absolute.py +363 -0
  112. beratools/tools/line_footprint_fixed.py +282 -0
  113. beratools/tools/line_footprint_functions.py +720 -0
  114. beratools/tools/line_footprint_relative.py +64 -0
  115. beratools/tools/ln_relative_metrics.py +615 -0
  116. beratools/tools/r_cal_lpi_elai.r +25 -0
  117. beratools/tools/r_generate_pd_focalraster.r +101 -0
  118. beratools/tools/r_interface.py +80 -0
  119. beratools/tools/r_point_density.r +9 -0
  120. beratools/tools/rpy_chm2trees.py +86 -0
  121. beratools/tools/rpy_dsm_chm_by.py +81 -0
  122. beratools/tools/rpy_dtm_by.py +63 -0
  123. beratools/tools/rpy_find_cellsize.py +43 -0
  124. beratools/tools/rpy_gnd_csf.py +74 -0
  125. beratools/tools/rpy_hummock_hollow.py +85 -0
  126. beratools/tools/rpy_hummock_hollow_raster.py +71 -0
  127. beratools/tools/rpy_las_info.py +51 -0
  128. beratools/tools/rpy_laz2las.py +40 -0
  129. beratools/tools/rpy_lpi_elai_lascat.py +466 -0
  130. beratools/tools/rpy_normalized_lidar_by.py +56 -0
  131. beratools/tools/rpy_percent_above_dbh.py +80 -0
  132. beratools/tools/rpy_points2trees.py +88 -0
  133. beratools/tools/rpy_vegcoverage.py +94 -0
  134. beratools/tools/tiler.py +206 -0
  135. beratools/tools/tool_template.py +54 -0
  136. beratools/tools/vertex_optimization.py +620 -0
  137. beratools/tools/zonal_threshold.py +144 -0
  138. beratools-0.2.0.dist-info/METADATA +63 -0
  139. beratools-0.2.0.dist-info/RECORD +142 -0
  140. beratools-0.2.0.dist-info/WHEEL +4 -0
  141. beratools-0.2.0.dist-info/entry_points.txt +2 -0
  142. beratools-0.2.0.dist-info/licenses/LICENSE +22 -0
@@ -0,0 +1,116 @@
1
+ import os
2
+ import time
3
+
4
+ from beratools.tools.common import *
5
+
6
+
7
+ class OperationCancelledException(Exception):
8
+ pass
9
+
10
+
11
+ try: # integrated R env
12
+ # check R language within env
13
+ current_env_path = os.environ['CONDA_PREFIX']
14
+ # if os.path.isdir(current_env_path):
15
+ os.environ['R_HOME'] = os.path.join(current_env_path, r"Lib\R")
16
+ os.environ['R_USER'] = os.path.expanduser('~')
17
+ os.environ['R_LIBS_USER'] = os.path.join(current_env_path, r"Lib\R\library")
18
+
19
+ except FileNotFoundError:
20
+ print("Warning: Please install R for this process!!")
21
+ exit()
22
+
23
+ import rpy2.robjects as robjects
24
+ from rpy2.robjects.packages import importr, data
25
+ from rpy2.robjects.vectors import StrVector
26
+
27
+
28
+ def ht_metrics(callback, in_las_folder, cell_size, out_folder, processes, verbose):
29
+ r = robjects.r
30
+ import psutil
31
+ stats = psutil.virtual_memory() # returns a named tuple
32
+ available = getattr(stats, 'available') / 1024000000
33
+ if 2 < processes <= 8:
34
+ if available <= 50:
35
+ rprocesses = 2
36
+ elif 50 < available <= 150:
37
+ rprocesses = 4
38
+ elif 150 < available <= 250:
39
+ rprocesses = 8
40
+ else:
41
+ rprocesses = 8
42
+
43
+ in_las_folder = in_las_folder.replace("\\", "/")
44
+ out_folder = out_folder.replace("\\", "/")
45
+
46
+ # assign R script file to local variable
47
+ Beratools_R_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Beratools_r_script.r')
48
+ # Defining the R script and loading the instance in Python
49
+ r['source'](Beratools_R_script)
50
+ # Loading the function defined in R script.
51
+ r_ht_metrics_function = robjects.globalenv['ht_metrics_lite']
52
+ # r_pd2cellsize =robjects.globalenv['pd2cellsize']
53
+ # Invoking the R function
54
+ # cell_size=r_pd2cellsize(in_las_folder)
55
+ r_ht_metrics_function(in_las_folder, cell_size, out_folder, rprocesses)
56
+
57
+
58
+ if __name__ == '__main__':
59
+ start_time = time.time()
60
+ print('Height Metrics from LiDAR process.\n'
61
+ '@ {}'.format(time.strftime("%d %b %Y %H:%M:%S", time.localtime())))
62
+
63
+ r = robjects.r
64
+ utils = importr('utils')
65
+ base = importr('base')
66
+ utils.chooseCRANmirror(ind=12) # select the 12th mirror in the list: Canada
67
+ print("Checking R packages ...")
68
+ CRANpacknames = ['lidR', 'rgrass', 'rlas', 'future',
69
+ 'terra'] # ,'comprehenr','na.tools','sf','sp']#,'devtools','gdal']#,'fasterRaster']
70
+ CRANnames_to_install = [x for x in CRANpacknames if not robjects.packages.isinstalled(x)]
71
+
72
+ if len(CRANnames_to_install) > 0:
73
+ utils.install_packages(StrVector(CRANnames_to_install))
74
+ packages_found = True
75
+ else:
76
+ packages_found = True
77
+
78
+ # if packages_found:
79
+ # utils.update_packages(checkBuilt = True, ask=False)
80
+
81
+ del CRANpacknames, CRANnames_to_install
82
+
83
+ print("Checking input parameters ...")
84
+ in_args, in_verbose = check_arguments()
85
+ in_las_folder = in_args.input["in_las_folder"]
86
+ try:
87
+ cell_size = float(in_args.input["cell_size"])
88
+ in_args.input["cell_size"] = cell_size
89
+ except ValueError:
90
+ print("Invalid input of cell_size, default value will be used")
91
+ in_args.input["cell_size"] = 1.0
92
+ out_folder = in_args.input["out_folder"]
93
+
94
+ if not os.path.exists(in_las_folder):
95
+ print("Error! Cannot locate Las folder, please check.")
96
+ exit()
97
+ else:
98
+ found = False
99
+ for files in os.listdir(in_las_folder):
100
+ if files.endswith(".las") or files.endswith(".laz"):
101
+ found = True
102
+ break
103
+ if not found:
104
+ print("Error! Cannot locate input LAS file(s), please check!")
105
+ exit()
106
+
107
+ if not os.path.exists(out_folder):
108
+ print("Warning! Cannot locate output folder, It will be created.")
109
+ os.makedirs(out_folder)
110
+
111
+ print("Checking input parameters ... Done")
112
+
113
+ ht_metrics(print, **in_args.input, processes=int(in_args.processes), verbose=in_verbose)
114
+
115
+ print('Height Metrics from LiDAR process is done in {} seconds)'
116
+ .format(round(time.time() - start_time, 5)))
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import os
4
+ import sys
5
+ sys.path.append(os.path.dirname(os.path.realpath(__file__)))
6
+
7
+ name = 'tools'
@@ -0,0 +1,132 @@
1
+ import os
2
+ import csv
3
+
4
+ from PyQt5.QtWidgets import QDialog
5
+ from beratools.gui.batch_processing_dlg import *
6
+ from beratools.gui.bt_data import *
7
+
8
+ bt = BTData()
9
+
10
+
11
+ # TODO: Check input file existence
12
+ def create_tool_batch_csv(project, tool_name, tasks):
13
+ tool_api = tool_name
14
+ proj_path = Path(project)
15
+ script_path = str(proj_path.with_name(proj_path.stem + '_' + tool_api.replace(' ', '_') + '.csv'))
16
+
17
+ param_list = bt.get_bera_tool_parameters_list(tool_name)
18
+
19
+ all_tasks = []
20
+ for item in tasks:
21
+ task = param_list
22
+ in_line = Path(item['in_line'])
23
+ in_chm = Path(item['in_chm'])
24
+ path_line = in_line.with_name(in_line.stem + '_output_line.shp')
25
+ path_footprint = in_line.with_name(in_line.stem + '_footprint.shp')
26
+ path_canopy = in_chm.with_name(in_chm.stem + '_canopy.tif')
27
+ path_cost = in_chm.with_name(in_chm.stem + '_cost.tif')
28
+
29
+ # TODO: change to tool api
30
+ if tool_name == 'Canopy Cost Raster':
31
+ task['in_chm'] = in_chm.as_posix()
32
+ task['out_canopy'] = path_canopy.as_posix()
33
+ task['out_cost'] = path_cost.as_posix()
34
+ elif tool_name == 'Center Line':
35
+ task['in_line'] = in_line.as_posix()
36
+ task['in_cost'] = path_cost.as_posix()
37
+ task['out_line'] = path_line.as_posix()
38
+ elif tool_name == 'Line Footprint by Static Threshold':
39
+ task['in_line'] = path_line.as_posix()
40
+ task['in_canopy'] = path_canopy.as_posix()
41
+ task['in_cost'] = path_cost.as_posix()
42
+ task['out_footprint'] = path_footprint.as_posix()
43
+ elif tool_name == 'Line Footprint by Dynamic Threshold':
44
+ task['in_line'] = path_line.as_posix()
45
+ task['in_chm'] = in_chm.as_posix()
46
+ task['out_footprint'] = in_line.with_name(in_line.stem + '_dyn_footprint.shp')
47
+ elif tool_name == 'Forest Line Attributes':
48
+ task['in_line'] = path_line.as_posix()
49
+ task['in_chm'] = in_chm.as_posix()
50
+ task['in_footprint'] = path_footprint.as_posix()
51
+ task['out_line'] = in_line.with_name(in_line.stem + '_line_attributes.shp').as_posix()
52
+ elif tool_name == 'Raster Line Attributes':
53
+ task['in_line'] = path_line.as_posix()
54
+ task['in_chm'] = in_chm.as_posix()
55
+ task['out_line'] = in_line.with_name(in_line.stem + '_raster_attributes.shp').as_posix()
56
+
57
+ all_tasks.append(task.copy())
58
+
59
+ header = list(task.keys())
60
+
61
+ with open(script_path, 'w', newline='') as csv_file:
62
+ writer = csv.DictWriter(csv_file, fieldnames=header)
63
+ writer.writeheader()
64
+ writer.writerows(all_tasks)
65
+
66
+ return script_path
67
+
68
+
69
+ def batch_processing(callback, batch_tool_name, in_project, processes, verbose):
70
+ proj_data = None
71
+ proj_tool_name = None
72
+ proj_tasks = None
73
+ if in_project and os.path.exists(in_project):
74
+ with open(in_project, 'r') as project_file:
75
+ proj_data = json.load(project_file)
76
+
77
+ csv_file = create_tool_batch_csv(in_project, batch_tool_name, proj_data['tasks'])
78
+ dialog = BPDialog(batch_tool_name)
79
+ dialog.open_csv(csv_file)
80
+
81
+ flag = dialog.exec()
82
+
83
+ # import tasks data
84
+ data = pd.read_csv(csv_file)
85
+ task_data = data.to_dict(orient='records')
86
+
87
+ if flag == QDialog.Accepted and task_data:
88
+ steps = len(task_data)
89
+ step = 0
90
+
91
+ print('{} tasks are prepared'.format(steps))
92
+ print('-----------------------------------')
93
+
94
+ step = 0
95
+ total_steps = len(task_data)
96
+ for task in task_data:
97
+ print('Starting task #{} ...'.format(step))
98
+ print(' "PROGRESS_LABEL {} task {} of {}" '.format(batch_tool_name, step, total_steps), flush=True)
99
+ print(' %{} '.format(step / steps * 100), flush=True)
100
+ task = generate_task_params(task)
101
+ code = execute_task(bt.get_bera_tool_api(batch_tool_name), task)
102
+ step += 1
103
+ # task is cancelled
104
+ if code == 2:
105
+ break
106
+
107
+ print(' "PROGRESS_LABEL {} {} tasks finished" '.format(batch_tool_name, total_steps), flush=True)
108
+ print(' %{} '.format(0), flush=True)
109
+
110
+ print('Tasks finished.', flush=True)
111
+
112
+
113
+ def execute_task(tool_api, task):
114
+ # return 2 if task is cancelled
115
+ return bt.run_tool(tool_api, task, None, False)
116
+
117
+
118
+ def generate_task_params(task):
119
+ """
120
+ When project tool_api is tiler, tool parameters are missing.
121
+ This function will retrieve default arguments
122
+ and generate new file names such as cost/canopy/centerline file names
123
+ """
124
+ updated_task = task
125
+ return updated_task
126
+
127
+
128
+ if __name__ == '__main__':
129
+ in_args, in_verbose = check_arguments()
130
+ app = QApplication(sys.argv)
131
+ batch_processing(print, **in_args.input, processes=int(in_args.processes), verbose=in_verbose)
132
+ sys.exit(app.exec_())