def cut_discriminator(self, trees, value): if value < 0 or value > 100: Tools.print_log_line('Value must debe ser un valor entre 0 y 100', logging.ERROR) else: return Tree.sum_tree_list(trees, 'expan') * ((100 - value) / 100.0) return 0
def apply_model(self, plot: Plot, years: int, value: float): Tools.print_log_line('Aplicando cut down sistemática', logging.INFO) new_plot = Plot() new_plot.clone(plot) order_criteria = OrderCriteria() order_criteria.add_criteria('dbh') search_criteria = SearchCriteria() search_criteria.add_criteria('status', None, EQUAL) trees = Tree.get_sord_and_order_tree_list(plot.trees, search_criteria=search_criteria, order_criteria=order_criteria) for tree in trees: new_tree = Tree() new_tree.clone(tree) new_tree.add_value('expan', tree.expan * ((100 - value) / 100)) new_tree.add_value('status', None) new_plot.add_tree(new_tree) cut_tree = Tree() cut_tree.clone(tree) cut_tree.add_value('status', 'C') cut_tree.add_value('expan', tree.expan - new_tree.expan) if cut_tree.expan > 0: new_plot.add_tree(cut_tree) return new_plot
def apply_model(self, plot: Plot, years: int, value: float): Tools.print_log_line('Aplicando corta por el menor', logging.INFO) accumulator = 0 cut_all_the_rest = False new_plot = Plot() new_plot.clone(plot) order_criteria = OrderCriteria(ASC) order_criteria.add_criteria('dbh') search_criteria = SearchCriteria() search_criteria.add_criteria('status', None, EQUAL) trees = Tree.get_sord_and_order_tree_list(plot.trees, search_criteria=search_criteria, order_criteria=order_criteria) cut_discriminator = self.type.cut_discriminator(trees, value) for tree in trees: accumulator += self.type.accumulator(tree) if not cut_all_the_rest: new_tree = Tree() new_tree.clone(tree) if accumulator >= cut_discriminator: cut_all_the_rest = True new_expan = self.type.compute_expan(tree, accumulator, cut_discriminator) if new_expan <= 0: new_tree.add_value('expan', new_expan) new_tree.add_value('status', 'C') else: cut_tree = Tree() cut_tree.clone(tree) cut_tree.add_value('status', 'C') cut_tree.add_value('expan', new_expan) if cut_tree.expan > 0: new_plot.add_tree(cut_tree) new_tree.sub_value('expan', new_expan) new_tree.add_value('status', None) new_plot.add_tree(new_tree) else: new_tree = Tree() new_tree.clone(tree) new_tree.add_value('status', 'C') new_plot.add_tree(new_tree) return new_plot
def __next__(self): if self.__sheet is None: Tools.print_log_line("No sheet have been chosen.", logging.ERROR) raise StopIteration if len(self.__sheet['results']['bindings']) <= self.__cursor: raise StopIteration else: result = {} data = self.__sheet['results']['bindings'][self.__cursor] if self.__headers is None: for i in range(len(data)): result[i] = data[i].value else: for i in range(len(data)): result[self.__headers[i]] = data[ self.__headers[i]]['value'] self.__cursor = self.__cursor + 1 return result raise StopIteration
def get_tree(self, plot_id: str, tree_id: str): if plot_id in self.__plots.keys(): return self.__plots[plot_id].get_tree(tree_id) else: Tools.print_log_line( "There is not any plot with id " + str(plot_id), logging.ERROR) return None
def __init__(self, data=None): self.__values = dict() self.__trees = dict() self.__dead_trees = dict() self.__cut_trees = dict() self.__added_trees = dict() if data is None: Tools.print_log_line("No data info. The Plot has been created empty.", logging.WARNING) for variable in PLOT_VARIABLE_NAMES: self.__values[variable] = 0.0 else: for variable in PLOT_VARIABLE_NAMES: if variable not in data.keys(): Tools.print_log_line(str(variable) + ' is not in data document', logging.WARNING) self.__values[variable] = 0.0 else: self.__values[variable] = data[variable] self.__values[ID] = int(self.__values[ID]) if 'plot' in data: # json input self.map_json_to_xl(data)
def get_sord_and_order_tree_list(input, search_criteria=None, order_criteria=None): def sort_key(element: Tree): if element.get_value(order_criteria.get_first()) is None: return 0.0 return element.get_value(order_criteria.get_first()) if isinstance(input, dict): data = input.values() elif isinstance(input, list) or isinstance(input, {}.values().__class__): data = input else: Tools.print_log_line('Input list must be list and dict', logging.WARNING) return None tmp = list() if search_criteria is not None: for tree in data: valid = 0 for criteria in search_criteria.criterion: if criteria.is_valid(tree.get_value(criteria.variable)): valid += 1 if valid == len(search_criteria.criterion): tmp.append(tree) else: for tree in data: tmp.append(tree) if order_criteria is not None: tmp = sorted(tmp, key=sort_key, reverse=False if order_criteria.type == DESC else True) return tmp
def calculate_plot_from_tree(self): for tree in self.__trees.values(): if tree is not None: self.__values['BASAL_AREA'] = tree.basal_area * tree.expan / 10000; self.__values['DOMINANT_H'] = tree.height self.__values['DENSITY'] = tree.expan self.__values['AGE'] = tree.tree_age self.__values['MEAN_DBH'] = tree.var_1 self.__values['QM_DBH'] = tree.dbh self.__values['DOMINANT_DBH'] = tree.var_2 self.__values['DBH_MAX'] = tree.var_4 self.__values['DBH_MIN'] = tree.var_5 self.__values['MEAN_H'] = tree.var_3 self.__values['CROWN_MEAN_D'] = tree.var_6 self.__values['CROWN_DOM_D'] = tree.lcw if self.qm_dbh != 0: self.__values['REINEKE'] = tree.expan * math.pow(25 / self.qm_dbh, -1.605) if self.dominant_h != 0 and tree.expan != 0: self.__values['HART'] = 10000 / (self.dominant_h * math.sqrt(tree.expan)) if self.__values['DOMINANT_H'] != 0: self.__values['CANOPY_COVER'] = math.pi * (tree.lcw * tree.lcw / 4) * tree.expan / 10000 self.__values['VOL'] = tree.vol * tree.expan self.__values['BOLE_VOL'] = tree.bole_vol * tree.expan self.__values['BARK_VOL'] = self.__values['VOL'] - self.__values['BOLE_VOL'] else: Tools.print_log_line('Tree is None', logging.ERROR)
def apply_model(self, input_files: list, years: int): if not isinstance(input_files, dict) or len(input_files) < 2: return None Tools.print_log_line('Generating initial inventory', logging.INFO) reader: JSONReader = JSONReader(input_files) return Inventory(reader)
def sub_value(self, var, value): try: if var in INT_VALUES: self.__values[var] += int(value) else: self.__values[var] -= float(value) except Exception as e: Tools.print_log_line(str(e) + ' when it tried to update variable ' + var + ' with value ' + str(value), logging.ERROR)
def __init__(self, name: str, version: int): self.__tree = None self.__name = name self.__version = version Tools.print_log_line( "Loading model " + str(self.name) + "(" + str(self.version) + ")", logging.INFO)
def apply_model(self, file_path: str, years: int): if file_path is None: return None Tools.print_log_line('Generating initial inventory', logging.INFO) reader: ExcelReader = ExcelReader(file_path, DEFAULT_EXCEL_FILE_STRUCTURE) return Inventory(reader)
def get_value(self, var, json = False): try: if self.__values[var] is None: return self.__values[var] elif var in STR_VALUES or json == True: return str(self.__values[var]) if var in INT_VALUES: return int(self.__values[var]) else: return self.__values[var] except Exception as e: Tools.print_log_line(str(e) + ' when it tried to access variable ' + var, logging.ERROR)
def add_value(self, var, value): try: if value is None: self.__values[var] = value elif var in STR_VALUES: self.__values[var] = str(value) elif var in INT_VALUES: self.__values[var] = int(value) else: self.__values[var] = float(value) except Exception as e: Tools.print_log_line(str(e) + ' when it tried to update variable ' + var + ' with value ' + value, logging.ERROR)
def __init__(self, filename, sheets): if not os.path.exists(filename): Tools.print_log_line('filename ' + filename + " does not exists.", logging.ERROR) exit(-1) self.__document = xlrd.open_workbook(filename) self.__cursor = 0 self.__sheet = None self.__sheets = {j: i for i, j in enumerate(sheets)} self.__headers = None Tools.print_log_line('Inventory file ' + filename + ' loaded', logging.INFO)
def __init__(self, reader=None, date=datetime.now()): self.__date = date self.__plots = dict() self.__plots_to_print = dict() if reader is None: Tools.print_log_line( "No reader information, generated empty plots list", logging.WARNING) elif isinstance(reader, ExcelReader): reader.choose_sheet(PARCEL_CODE, True) for plot in reader: p = Plot(plot) self.__plots[p.id] = p self.__plots_to_print[p.id] = True reader.choose_sheet(TREE_CODE, True) for data in reader: tree = Tree(data) plot_id = tree.get_value('PLOT_ID') self.__plots[plot_id].add_tree(tree) elif isinstance(reader, JSONReader): reader.choose_sheet('plots', True) for plot in reader: p = Plot(plot) self.__plots[p.id] = p self.__plots_to_print[p.id] = True reader.choose_sheet('trees', True) for data in reader: tree = Tree(data) plot_id = tree.get_value('PLOT_ID', True) # True, it's in json format self.__plots[plot_id].add_tree(tree)
def __init__(self, input_files: list): self.__documents = dict() self.__cursor = 0 self.__sheet = None self.__headers = None for filetype, filename in input_files.items(): if not os.path.exists(filename): Tools.print_log_line( 'filename ' + filename + " does not exists.", logging.ERROR) exit(-1) with open(filename, 'r', encoding='utf-8', errors='ignore') as f: self.__documents[filetype] = json.loads(f.read()) Tools.print_log_line('Inventory file ' + filename + ' loaded', logging.INFO)
def __init__(self, data=None): self.__values = dict() if data is None: Tools.print_log_line("No data info when tree is generated", logging.WARNING) for var_name in VARIABLE_NAMES: self.__values[var_name] = 0 else: for var_name in VARIABLE_NAMES: if var_name not in data.keys(): Tools.print_log_line(var_name + ' is not in data document', logging.WARNING) self.__values[var_name] = 0 else: self.__values[var_name] = data[var_name] if 'tree' in data: # json input self.map_json_to_xl(data) self.__values['status'] = None
def __init__(self, configuration_file=None): self.__operations = list() if configuration_file is not None: with open(configuration_file, 'r', encoding='utf-8', errors='ignore') as f: configuration = json.loads(f.read()) self.__name = configuration['name'] self.__overwrite_output_file = True if configuration['operations'] == "YES" else False self.__output_path = configuration['output_path'] self.__zip_compression = True if configuration['zip_compression'] == "YES" else False #TODO: THE VALUE 0 MUST A GLOBAL VALUE self.__ext = 0 if 'output_type' not in configuration.keys() else configuration['output_type'] self.__decimal_numbers = configuration['decimal_numbers'] for item in configuration['operations'].values(): self.__operations.append(Operation(item)) if item['operation'] == 'INIT': self.__modelo = item['model_path'] else: Tools.print_log_line('No configuration file provided.', logging.WARNING)
def load_engine(type_engine, configuration=None): engine = -1 for key, value in ENGINES: if key == type_engine: engine = key if engine == 0: return BasicEngine(configuration) if engine == 1: return BasicEngine(configuration) if engine == 2: if platform.system() == 'Windows': Tools.print_log_line( 'Windows os system does not support DASK engine, using default', logging.WARNING) return BasicEngine(configuration) else: return DaskEngine(configuration) return BasicEngine(configuration)
def __next__(self): if self.__sheet is None: Tools.print_log_line("No sheet have been chosen.", logging.ERROR) raise StopIteration self.__cursor = self.__cursor + 1 if self.__sheet.nrows <= self.__cursor: raise StopIteration else: result = {} data = self.__sheet.row(self.__cursor) if self.__headers is None: for i in range(len(data)): result[i] = data[i].value else: for i in range(len(data)): result[self.__headers[i]] = data[i].value return result raise StopIteration
def generate_xslt_file(self, name: str, labels: dict, file_path: str, plot, modelo: str, zip_compression: bool = False, type: int = JSON, decimals: int = 2): Tools.print_log_line('Generating xslf file for plot ' + str(plot.id), logging.INFO) workbook = Workbook() worksheet = workbook.active worksheet.title = labels['simanfor.general.Summary'] try: logo_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), "..", "files", "logo.png") logo = drawing.image.Image(logo_path) logo.anchor = 'A1' logo.width = 190 logo.height = 60 worksheet.add_image(logo) except: Tools.print_log_line("Couldn't find logo image.", logging.INFO) worksheet.column_dimensions["D"].width = 20 worksheet.cell(row=1, column=4).value = labels['simanfor.general.study_area'] worksheet.cell(row=2, column=4).value = labels['simanfor.general.forest'] worksheet.cell(row=3, column=4).value = labels['simanfor.general.main_specie'] worksheet.cell( row=4, column=4).value = labels['simanfor.general.specie_ifn_id'] worksheet.cell(row=1, column=5).value = plot.study_area worksheet.cell(row=2, column=5).value = plot.forest worksheet.cell(row=3, column=5).value = plot.main_specie worksheet.cell(row=4, column=5).value = plot.specie_ifn_id worksheet.column_dimensions["J"].width = 16 worksheet.column_dimensions["K"].width = 20 worksheet.cell(row=1, column=10).value = labels['simanfor.general.inventory'] worksheet.cell(row=2, column=10).value = labels['simanfor.general.plot'] worksheet.cell(row=3, column=10).value = labels['simanfor.general.model'] worksheet.cell(row=4, column=10).value = labels['simanfor.general.scenario'] worksheet.cell(row=1, column=11).value = str(int(plot.inventory_id)) worksheet.cell(row=2, column=11).value = str(plot.id) worksheet.cell(row=3, column=11).value = modelo worksheet.cell(row=4, column=11).value = name self.resumen_merged_header(worksheet, 'C6:F6', 6, 3, labels['simanfor.general.merged_header_1']) self.resumen_merged_header(worksheet, 'G6:I6', 6, 7, labels['simanfor.general.merged_header_2']) self.resumen_merged_header(worksheet, 'J6:M6', 6, 10, labels['simanfor.general.merged_header_3']) self.resumen_merged_header(worksheet, 'N6:P6', 6, 14, labels['simanfor.general.merged_header_4']) cell = worksheet.cell(row=6, column=17) cell.value = labels['simanfor.general.merged_header_5'] cell.font = cell.font.copy(bold=True) worksheet.cell(row=7, column=1).value = labels['simanfor.plot.AGE'] worksheet.cell(row=7, column=2).value = labels['simanfor.general.measure_1'] worksheet.cell(row=7, column=3).value = labels['simanfor.general.measure_2'] worksheet.cell(row=7, column=4).value = labels['simanfor.general.measure_3'] worksheet.cell(row=7, column=5).value = labels['simanfor.general.measure_4'] worksheet.cell(row=7, column=6).value = labels['simanfor.general.measure_5'] worksheet.cell(row=7, column=7).value = labels['simanfor.general.measure_6'] worksheet.cell(row=7, column=8).value = labels['simanfor.general.measure_7'] worksheet.cell(row=7, column=9).value = labels['simanfor.general.measure_8'] worksheet.cell(row=7, column=10).value = labels['simanfor.general.measure_9'] worksheet.cell(row=7, column=11).value = labels['simanfor.general.measure_10'] worksheet.cell(row=7, column=12).value = labels['simanfor.general.measure_11'] worksheet.cell(row=7, column=13).value = labels['simanfor.general.measure_12'] worksheet.cell(row=7, column=14).value = labels['simanfor.general.measure_13'] worksheet.cell(row=7, column=15).value = labels['simanfor.general.measure_14'] worksheet.cell(row=7, column=16).value = labels['simanfor.general.measure_15'] worksheet.cell(row=7, column=17).value = labels['simanfor.general.measure_16'] worksheet.cell(row=7, column=18).value = i18n.t('simanfor.general.measure_17') ws_parcelas = workbook.create_sheet(labels['simanfor.general.Plots']) for j in range(len(OUTPUT_NAMES)): ws_parcelas.cell(row=1, column=j + 1).value = labels['simanfor.plot.' + OUTPUT_NAMES[j]] for j in range(len(PLOT_VARIABLE_NAMES)): ws_parcelas.cell( row=1, column=j + 1 + len(OUTPUT_NAMES)).value = labels['simanfor.plot.' + PLOT_VARIABLE_NAMES[j]] step_count = 1 row = 8 summary_row = 8 for step in self.__steps: next_step = None if step_count >= len( self.__steps) else self.__steps[step_count] next_operation = None if next_step == None else next_step._Step__full_operation summary_row = step.to_xslt(labels, workbook, plot.id, row, next_step, next_operation, summary_row, decimals) row += 1 step_count += 1 workbook.close() workbook.save(file_path) return plot
def set_tree(self, tree: Tree): Tools.print_log_line( "Loading tree (" + tree.id + ") into model" + self.tree + "(" + self.version + ")", logging.INFO) self.__tree = tree
def __init__(self): super().__init__(type='PercentOfTrees') Tools.print_log_line('Creating percents of trees cut technique', logging.INFO)