From fe28401d73987e2197d530c95e8aae2f17f998ce Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Thu, 1 Aug 2024 21:58:48 +0200 Subject: [PATCH 1/9] added a work file manager. Added the WorkFileManager class to handle temp files used in classes to initiate unqie paths for iterations and writing it to disk or memory based on params. --- custom_tools/general_tools/file_utilities.py | 51 ++++++++++++ .../begrensningskurve_land_waterbodies.py | 80 +++++++++++++++---- 2 files changed, 114 insertions(+), 17 deletions(-) diff --git a/custom_tools/general_tools/file_utilities.py b/custom_tools/general_tools/file_utilities.py index 43b986fd..7d1269a5 100644 --- a/custom_tools/general_tools/file_utilities.py +++ b/custom_tools/general_tools/file_utilities.py @@ -93,6 +93,57 @@ def _append_geometry(self): print("Appended geometry to the feature class.") +class WorkFileManager: + def __init__( + self, + unique_id: str, + root_file: str = None, + write_to_memory: bool = True, + keep_files: bool = False, + ): + self.unique_id = unique_id + self.root_file = root_file + self.write_to_memory = write_to_memory + self.keep_files = keep_files + + if not self.write_to_memory and not self.root_file: + raise ValueError( + "Need to specify root_file path to write to disk for work files." + ) + + if self.keep_files and not self.root_file: + raise ValueError( + "Need to specify root_file path and write to disk to keep work files." + ) + + self.file_location = "memory/" if self.write_to_memory else f"{self.root_file}_" + + def _build_file_path(self, file_name: str) -> str: + return f"{self.file_location}{file_name}_{self.unique_id}" + + def setup_work_file_paths(self, instance, file_names: list[str]): + """Generates file paths and sets them as attributes on the instance.""" + generated_paths = [self._build_file_path(name) for name in file_names] + for name, path in zip(file_names, generated_paths): + setattr(instance, name, path) + return generated_paths + + def cleanup_files(self, file_paths: list[str]): + for path in file_paths: + self._delete_file(path) + + @staticmethod + def _delete_file(file_path: str): + try: + if arcpy.Exists(file_path): + arcpy.management.Delete(file_path) + print(f"Deleted: {file_path}") + else: + print(f"File did not exist: {file_path}") + except arcpy.ExecuteError as e: + print(f"Error deleting file {file_path}: {e}") + + def compare_feature_classes(feature_class_1, feature_class_2): # Get count of features in the first feature class count_fc1 = int(arcpy.GetCount_management(feature_class_1)[0]) diff --git a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py index 50fb9848..b0fd531f 100644 --- a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py +++ b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py @@ -8,7 +8,8 @@ from env_setup import environment_setup from custom_tools.decorators.partition_io_decorator import partition_io_decorator from custom_tools.general_tools import custom_arcpy -from constants.n100_constants import N100_Symbology, N100_SQLResources, N100_Values +from custom_tools.general_tools.file_utilities import WorkFileManager +from constants.n100_constants import N100_Values class BegrensningskurveLandWaterbodies: @@ -39,20 +40,58 @@ def __init__( self.area_length_ratio_field_name = "area_length_ratio" - self.waterfeatures_from_begrensningskurve = None - self.waterfeatures_from_begrensningskurve_selection = None - self.land_features_area = None - self.water_features_area = None - self.water_features_area_narrow = None - self.water_features_area_wide = None - self.selected_land_features = None - self.land_features_buffer = None - self.begrensningskurve_waterfeatures_buffer = None - self.erase_feature_1 = None - self.erase_feature_2 = None + self.work_file_manager = WorkFileManager( + unique_id=id(self), + root_file=root_file, + write_to_memory=write_work_files_to_memory, + keep_files=keep_work_files, + ) + + self.waterfeatures_from_begrensningskurve = ( + "waterfeatures_from_begrensningskurve" + ) + self.waterfeatures_from_begrensningskurve_selection = ( + "waterfeatures_from_begrensningskurve_selection" + ) + self.land_features_area = "land_features_area" + self.water_features_area = "water_features_area" + self.water_features_area_narrow = "water_features_area_narrow" + self.water_features_area_wide = "water_features_area_wide" + self.selected_land_features = "selected_land_features" + self.land_features_buffer = "land_features_buffer" + self.begrensningskurve_waterfeatures_buffer = ( + "begrensningskurve_waterfeatures_buffer" + ) + self.erase_feature_1 = "erase_feature_1" + self.erase_feature_2 = "erase_feature_2" + + self.work_file_list = [ + self.waterfeatures_from_begrensningskurve, + self.waterfeatures_from_begrensningskurve_selection, + self.land_features_area, + self.water_features_area, + self.water_features_area_narrow, + self.water_features_area_wide, + self.selected_land_features, + self.land_features_buffer, + self.begrensningskurve_waterfeatures_buffer, + self.erase_feature_1, + self.erase_feature_2, + ] self.working_files_list = [] + def setup_work_files(self): + # Generate full file paths and assign them to instance variables + self.working_files_list = self.work_file_manager.setup_work_file_paths( + instance=self, + file_names=self.work_file_list, + ) + + def cleanup_work_files(self): + # Clean up the work files when done + self.work_file_manager.cleanup_files(self.working_files_list) + def reset_temp_files(self): """Reset temporary file attributes.""" unique_id = id(self) @@ -276,15 +315,22 @@ def delete_feature_class(feature_class_path): output_param_names=["output_begrensningskurve"], ) def run(self): - self.reset_temp_files() + self.working_files_list = self.work_file_manager.setup_work_file_paths( + instance=self, + file_names=self.work_file_list, + ) + + # self.reset_temp_files() self.selections() self.field_management() self.finding_narrow_or_not() self.creating_buffers() self.erase_buffers() self.merge_water_features() - if not self.keep_work_files: - self.delete_working_files(*self.working_files_list) + # if not self.keep_work_files: + # self.delete_working_files(*self.working_files_list) + + self.work_file_manager.cleanup_files(self.working_files_list) if __name__ == "__main__": @@ -294,8 +340,8 @@ def run(self): input_land_cover_features=Building_N100.data_selection___land_cover_n100_input_data___n100_building.value, water_feature_buffer_width=N100_Values.building_water_intrusion_distance_m.value, output_begrensningskurve=Building_N100.begrensingskurve_land_water___begrensingskurve_buffer_in_water___n100_building.value, - write_work_files_to_memory=False, - keep_work_files=True, + write_work_files_to_memory=True, + keep_work_files=False, root_file=Building_N100.begrensingskurve_land_water___root_file___n100_building.value, ) begrensningskurve_land_waterbodies.run() From ed58f04dabca6a09816705af463cbdce600cf1a3 Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Fri, 2 Aug 2024 14:29:00 +0200 Subject: [PATCH 2/9] Removed redundant boilerplate code after WorkFIleManager implementation in begrensningskurve_land_waterbodies.py. Removed redundant boilerplate code after WorkFIleManager implementation in begrensningskurve_land_waterbodies.py. Deleted setup_work_files, cleanup_work_files, reset_temp_files, and associated methods for managing temporary files within Begrensningskurve class. Streamlined the class by removing unnecessary methods and updated the configuration to write work files to disk instead of memory. --- .../begrensningskurve_land_waterbodies.py | 86 +------------------ 1 file changed, 1 insertion(+), 85 deletions(-) diff --git a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py index b0fd531f..6f3af1e9 100644 --- a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py +++ b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py @@ -81,75 +81,6 @@ def __init__( self.working_files_list = [] - def setup_work_files(self): - # Generate full file paths and assign them to instance variables - self.working_files_list = self.work_file_manager.setup_work_file_paths( - instance=self, - file_names=self.work_file_list, - ) - - def cleanup_work_files(self): - # Clean up the work files when done - self.work_file_manager.cleanup_files(self.working_files_list) - - def reset_temp_files(self): - """Reset temporary file attributes.""" - unique_id = id(self) - temporary_file = "in_memory\\" - permanent_file = f"{self.root_file}_" - if self.root_file is None: - if not self.write_work_files_to_memory: - raise ValueError( - "Need to specify root_file path to write to disk for work files." - ) - if self.keep_work_files: - raise ValueError( - "Need to specify root_file path and write to disk to keep_work_files." - ) - - if self.write_work_files_to_memory: - file_location = temporary_file - else: - file_location = permanent_file - - self.waterfeatures_from_begrensningskurve = ( - f"{file_location}waterfeatures_from_begrensningskurve_{unique_id}" - ) - self.waterfeatures_from_begrensningskurve_selection = ( - f"{file_location}waterfeatures_from_begrensningskurve_selection_{unique_id}" - ) - self.land_features_area = f"{file_location}land_features_area_{unique_id}" - self.water_features_area = f"{file_location}water_features_area_{unique_id}" - self.selected_land_features = ( - f"{file_location}selected_land_features_{unique_id}" - ) - self.water_features_area_narrow = ( - f"{file_location}water_features_area_narrow_{unique_id}" - ) - self.water_features_area_wide = ( - f"{file_location}water_features_area_wide_{unique_id}" - ) - self.land_features_buffer = f"{file_location}land_features_buffer_{unique_id}" - self.begrensningskurve_waterfeatures_buffer = ( - f"{file_location}begrensningskurve_waterfeatures_buffer_{unique_id}" - ) - self.erase_feature_1 = f"{file_location}erase_feature_1{unique_id}" - self.erase_feature_2 = f"{file_location}erase_feature_2{unique_id}" - - self.working_files_list = [ - self.waterfeatures_from_begrensningskurve, - self.waterfeatures_from_begrensningskurve_selection, - self.land_features_area, - self.water_features_area, - self.water_features_area_narrow, - self.water_features_area_wide, - self.selected_land_features, - self.land_features_buffer, - self.begrensningskurve_waterfeatures_buffer, - self.erase_feature_1, - self.erase_feature_2, - ] - def selections(self): if self.write_work_files_to_memory: custom_arcpy.select_attribute_and_make_feature_layer( @@ -298,18 +229,6 @@ def merge_water_features(self): output=self.output_begrensningskurve, ) - def delete_working_files(self, *file_paths): - """Deletes multiple feature classes or files. Detailed alias and output_type logging is not available here.""" - for file_path in file_paths: - self.delete_feature_class(file_path) - print(f"Deleted file: {file_path}") - - @staticmethod - def delete_feature_class(feature_class_path): - """Deletes a feature class if it exists.""" - if arcpy.Exists(feature_class_path): - arcpy.management.Delete(feature_class_path) - @partition_io_decorator( input_param_names=["input_begrensningskurve", "input_land_cover_features"], output_param_names=["output_begrensningskurve"], @@ -320,15 +239,12 @@ def run(self): file_names=self.work_file_list, ) - # self.reset_temp_files() self.selections() self.field_management() self.finding_narrow_or_not() self.creating_buffers() self.erase_buffers() self.merge_water_features() - # if not self.keep_work_files: - # self.delete_working_files(*self.working_files_list) self.work_file_manager.cleanup_files(self.working_files_list) @@ -340,7 +256,7 @@ def run(self): input_land_cover_features=Building_N100.data_selection___land_cover_n100_input_data___n100_building.value, water_feature_buffer_width=N100_Values.building_water_intrusion_distance_m.value, output_begrensningskurve=Building_N100.begrensingskurve_land_water___begrensingskurve_buffer_in_water___n100_building.value, - write_work_files_to_memory=True, + write_work_files_to_memory=False, keep_work_files=False, root_file=Building_N100.begrensingskurve_land_water___root_file___n100_building.value, ) From c37c562890f4839123fe3111768d42346b57b9de Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Fri, 2 Aug 2024 15:42:40 +0200 Subject: [PATCH 3/9] Added ability to handle lyrx files for WorkFileManager class in file_utilities.py. --- custom_tools/general_tools/file_utilities.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/custom_tools/general_tools/file_utilities.py b/custom_tools/general_tools/file_utilities.py index 7d1269a5..8968bde2 100644 --- a/custom_tools/general_tools/file_utilities.py +++ b/custom_tools/general_tools/file_utilities.py @@ -96,7 +96,7 @@ def _append_geometry(self): class WorkFileManager: def __init__( self, - unique_id: str, + unique_id: int, root_file: str = None, write_to_memory: bool = True, keep_files: bool = False, @@ -128,6 +128,16 @@ def setup_work_file_paths(self, instance, file_names: list[str]): setattr(instance, name, path) return generated_paths + def _build_file_path_lyrx(self, file_name: str) -> str: + return f"{self.file_location}{file_name}_{self.unique_id}.lyrx" + + def setup_work_file_paths_lyrx(self, instance, file_names: list[str]): + """Generates file paths and sets them as attributes on the instance.""" + generated_paths = [self._build_file_path_lyrx(name) for name in file_names] + for name, path in zip(file_names, generated_paths): + setattr(instance, name, path) + return generated_paths + def cleanup_files(self, file_paths: list[str]): for path in file_paths: self._delete_file(path) From ae924108e9951917cb30a1d69cccc6607e6a6d5c Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Fri, 2 Aug 2024 16:09:57 +0200 Subject: [PATCH 4/9] Made the syntax to use WorkFileManager more easy to use. --- custom_tools/general_tools/file_utilities.py | 60 ++++++++++++++----- .../begrensningskurve_land_waterbodies.py | 6 +- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/custom_tools/general_tools/file_utilities.py b/custom_tools/general_tools/file_utilities.py index 8968bde2..07ee4d44 100644 --- a/custom_tools/general_tools/file_utilities.py +++ b/custom_tools/general_tools/file_utilities.py @@ -121,26 +121,56 @@ def __init__( def _build_file_path(self, file_name: str) -> str: return f"{self.file_location}{file_name}_{self.unique_id}" - def setup_work_file_paths(self, instance, file_names: list[str]): - """Generates file paths and sets them as attributes on the instance.""" - generated_paths = [self._build_file_path(name) for name in file_names] - for name, path in zip(file_names, generated_paths): - setattr(instance, name, path) - return generated_paths + def setup_work_file_paths(self, instance, file_names): + """ + Generates file paths and sets them as attributes on the instance. + Updates the file_names list with the new paths. + Can handle a list of file names or a list of lists of file names. + """ + if isinstance(file_names[0], list): + for sublist in file_names: + for i, name in enumerate(sublist): + path = self._build_file_path(name) + setattr(instance, name, path) + sublist[i] = path + else: + for i, name in enumerate(file_names): + path = self._build_file_path(name) + setattr(instance, name, path) + file_names[i] = path def _build_file_path_lyrx(self, file_name: str) -> str: return f"{self.file_location}{file_name}_{self.unique_id}.lyrx" - def setup_work_file_paths_lyrx(self, instance, file_names: list[str]): - """Generates file paths and sets them as attributes on the instance.""" - generated_paths = [self._build_file_path_lyrx(name) for name in file_names] - for name, path in zip(file_names, generated_paths): - setattr(instance, name, path) - return generated_paths + def setup_work_file_paths_lyrx(self, instance, file_names): + """ + Generates file paths and sets them as attributes on the instance. + Updates the file_names list with the new paths. + Can handle a list of file names or a list of lists of file names. + """ + if isinstance(file_names[0], list): + for sublist in file_names: + for i, name in enumerate(sublist): + path = self._build_file_path_lyrx(name) + setattr(instance, name, path) + sublist[i] = path + else: + for i, name in enumerate(file_names): + path = self._build_file_path_lyrx(name) + setattr(instance, name, path) + file_names[i] = path - def cleanup_files(self, file_paths: list[str]): - for path in file_paths: - self._delete_file(path) + def cleanup_files(self, file_paths): + """ + Deletes files. Can handle a list of file paths or a list of lists of file paths. + """ + if isinstance(file_paths[0], list): + for sublist in file_paths: + for path in sublist: + self._delete_file(path) + else: + for path in file_paths: + self._delete_file(path) @staticmethod def _delete_file(file_path: str): diff --git a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py index 6f3af1e9..834e9bf0 100644 --- a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py +++ b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py @@ -234,7 +234,9 @@ def merge_water_features(self): output_param_names=["output_begrensningskurve"], ) def run(self): - self.working_files_list = self.work_file_manager.setup_work_file_paths( + environment_setup.main() + + self.work_file_manager.setup_work_file_paths( instance=self, file_names=self.work_file_list, ) @@ -246,7 +248,7 @@ def run(self): self.erase_buffers() self.merge_water_features() - self.work_file_manager.cleanup_files(self.working_files_list) + self.work_file_manager.cleanup_files(self.work_file_list) if __name__ == "__main__": From e23e13863dc511fad5b65295029f4635ba277e91 Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Mon, 5 Aug 2024 09:21:48 +0200 Subject: [PATCH 5/9] Start of refactor work file management in building conflict resolution Started to integrate WorkFileManager for dynamic handling of work files with added parameters for in-memory operations and file preservation. --- .../building/resolve_building_conflicts.py | 93 ++++++++++++++++++- .../point_resolve_building_conflicts.py | 14 +-- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/custom_tools/generalization_tools/building/resolve_building_conflicts.py b/custom_tools/generalization_tools/building/resolve_building_conflicts.py index 6bc6949f..310e7d47 100644 --- a/custom_tools/generalization_tools/building/resolve_building_conflicts.py +++ b/custom_tools/generalization_tools/building/resolve_building_conflicts.py @@ -13,6 +13,7 @@ from input_data.input_symbology import SymbologyN100 from file_manager.n100.file_manager_buildings import Building_N100 from file_manager.base_file_manager import BaseFileManager +from custom_tools.general_tools.file_utilities import WorkFileManager from constants.n100_constants import N100_Symbology, N100_Values from custom_tools.general_tools.polygon_processor import PolygonProcessor from env_setup import environment_setup @@ -35,6 +36,8 @@ def __init__( base_path_for_lyrx: str, base_path_for_features: str, output_files: Dict[str, str], + write_work_files_to_memory: bool = False, + keep_work_files: bool = False, ): # ======================================== # INITIALIZING VARIABLES @@ -74,7 +77,74 @@ def __init__( self.output_points = output_files["building_points"] self.output_polygons = output_files["building_polygons"] - # Working files (to be deleted after script has run) + self.work_file_manager_gdb = WorkFileManager( + unique_id=id(self), + root_file=base_path_for_features, + write_to_memory=write_work_files_to_memory, + keep_files=keep_work_files, + ) + + self.work_file_manager_lyrx = WorkFileManager( + unique_id=id(self), + root_file=base_path_for_lyrx, + write_to_memory=write_work_files_to_memory, + keep_files=keep_work_files, + ) + + # GDB Work Files + self.points_to_squares = "points_to_squares" + self.results_rbc_1_squares = "results_rbc_1_squares" + self.results_rbc_1_polygons = "results_rbc_1_polygons" + self.invisible_polygons_after_rbc_1 = "invisible_polygons_after_rbc_1" + self.invisible_polygons_to_points_after_rbc_1 = ( + "invisible_polygons_to_points_after_rbc_1" + ) + self.building_polygons_to_points_and_then_squares_rbc_1 = ( + "building_polygons_to_points_and_then_squares_rbc_1" + ) + self.merged_squares_rbc1 = "merged_squares_rbc1" + self.squares_after_rbc2 = "squares_after_rbc2" + self.polygons_after_rbc2 = "polygons_after_rbc2" + self.squares_back_to_points_after_rbc2 = "squares_back_to_points_after_rbc2" + + self.working_files_list_gdb = [ + self.points_to_squares, + self.results_rbc_1_squares, + self.results_rbc_1_polygons, + self.invisible_polygons_after_rbc_1, + self.invisible_polygons_to_points_after_rbc_1, + self.building_polygons_to_points_and_then_squares_rbc_1, + self.merged_squares_rbc1, + self.squares_after_rbc2, + self.polygons_after_rbc2, + self.squares_back_to_points_after_rbc2, + ] + + # Lyrx Work FIles + self.building_squares_with_lyrx = "building_squares_with_lyrx" + self.polygons_with_lyrx = "polygons_with_lyrx" + self.roads_with_lyrx = "roads_with_lyrx" + self.begrensningskurve_with_lyrx = "begrensningskurve_with_lyrx" + self.railway_with_lyrx = "railway_with_lyrx" + self.railway_stations_with_lyrx = "railway_stations_with_lyrx" + self.adding_symbology_to_squares_going_into_rbc2 = ( + "adding_symbology_to_squares_going_into_rbc2" + ) + self.adding_symbology_to_polygons_going_into_rbc2 = ( + "adding_symbology_to_polygons_going_into_rbc2" + ) + + self.working_files_list_lyrx = [ + self.building_squares_with_lyrx, + self.polygons_with_lyrx, + self.roads_with_lyrx, + self.begrensningskurve_with_lyrx, + self.railway_with_lyrx, + self.railway_stations_with_lyrx, + self.adding_symbology_to_squares_going_into_rbc2, + self.adding_symbology_to_polygons_going_into_rbc2, + ] + self.working_files_list = [] # Feature base path @@ -87,8 +157,6 @@ def __init__( # LOGICS # ======================================== - self.points_to_squares = None - def constructing_work_files(self): unique_id = id(self) @@ -423,6 +491,23 @@ def delete_feature_class(feature_class_path): ) def run(self): environment_setup.main() + + self.work_file_manager_gdb.setup_work_file_paths( + instance=self, + file_names=self.working_files_list_gdb, + ) + + self.work_file_manager_lyrx.setup_work_file_paths_lyrx( + instance=self, + file_names=self.working_files_list_lyrx, + ) + + for file in self.working_files_list_gdb: + print(f"Working file: {file}") + + for file in self.working_files_list_lyrx: + print(f"Working file: {file}") + self.constructing_work_files() self.building_points_to_squares() self.apply_symbology_to_the_layers() @@ -438,6 +523,8 @@ def run(self): self.adding_files_to_working_list() self.delete_working_files(self.working_files_list) + self.work_file_manager.cleanup_files(self.working_files_list) + if __name__ == "__main__": resolve_building_conflicts = ResolveBuildingConflicts( diff --git a/generalization/n100/building/point_resolve_building_conflicts.py b/generalization/n100/building/point_resolve_building_conflicts.py index 4c9fdf2d..b6d123a6 100644 --- a/generalization/n100/building/point_resolve_building_conflicts.py +++ b/generalization/n100/building/point_resolve_building_conflicts.py @@ -8,7 +8,7 @@ from input_data.input_symbology import SymbologyN100 from file_manager.n100.file_manager_buildings import Building_N100 from constants.n100_constants import N100_Symbology, N100_Values -from custom_tools.general_tools.polygon_processor import PolygonProcessor + from env_setup import environment_setup from input_data import input_symbology from custom_tools.general_tools.partition_iterator import PartitionIterator @@ -34,18 +34,6 @@ def main(): """ environment_setup.main() resolve_building_conflicts() - # building_points_to_squares() - # selecting_data_with_area() - # apply_symbology_to_the_layers() - # resolve_building_conflicts_1() - # building_polygons_to_keep_after_rbc_1() - # transforming_invisible_polygons_to_points_and_then_to_squares() - # adding_symbology_to_layers_being_used_for_rbc_2() - # resolve_building_conflicts_2() - # selecting_features_to_be_kept_after_rbc_2() - # transforming_squares_back_to_points() - # merging_building_points() - # assigning_final_names() def resolve_building_conflicts(): From 2e8c0a3192669238d0fa40c9d7d588cf6c45c994 Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Mon, 5 Aug 2024 10:29:23 +0200 Subject: [PATCH 6/9] Removed unused self list --- .../building/begrensningskurve_land_waterbodies.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py index 834e9bf0..dd14f696 100644 --- a/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py +++ b/custom_tools/generalization_tools/building/begrensningskurve_land_waterbodies.py @@ -79,8 +79,6 @@ def __init__( self.erase_feature_2, ] - self.working_files_list = [] - def selections(self): if self.write_work_files_to_memory: custom_arcpy.select_attribute_and_make_feature_layer( From 70e2410f3d81ba1e319024c86582b8a941e80623 Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Mon, 5 Aug 2024 10:29:58 +0200 Subject: [PATCH 7/9] Finished implementation of WorkFileManager in resolve_building_conflicts.py --- .../building/resolve_building_conflicts.py | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/custom_tools/generalization_tools/building/resolve_building_conflicts.py b/custom_tools/generalization_tools/building/resolve_building_conflicts.py index 310e7d47..3015d7aa 100644 --- a/custom_tools/generalization_tools/building/resolve_building_conflicts.py +++ b/custom_tools/generalization_tools/building/resolve_building_conflicts.py @@ -157,13 +157,6 @@ def __init__( # LOGICS # ======================================== - def constructing_work_files(self): - unique_id = id(self) - - self.points_to_squares = ( - f"{self.base_path_for_features}_points_to_squares_{unique_id}" - ) - @timing_decorator def building_points_to_squares(self): # Transforms all the building points to squares @@ -185,32 +178,32 @@ def apply_symbology_to_the_layers(self): { "input_layer": self.points_to_squares, "in_symbology_layer": self.building_squares_lyrx, - "output_name": f"{self.lyrx_base_path}building_squares_with_lyrx.lyrx", + "output_name": self.building_squares_with_lyrx, }, { "input_layer": self.input_building_polygons, "in_symbology_layer": self.building_polygons_lyrx, - "output_name": f"{self.lyrx_base_path}polygons_with_lyrx.lyrx", + "output_name": self.polygons_with_lyrx, }, { "input_layer": self.input_road_barrier, "in_symbology_layer": self.road_barrier_lyrx, - "output_name": f"{self.lyrx_base_path}roads_with_lyrx.lyrx", + "output_name": self.roads_with_lyrx, }, { "input_layer": self.input_begrensningskurve_barrier, "in_symbology_layer": self.begrensningskurve_barrier_lyrx, - "output_name": f"{self.lyrx_base_path}begrensningskurve_with_lyrx.lyrx", + "output_name": self.begrensningskurve_with_lyrx, }, { "input_layer": self.input_railway_barrier, "in_symbology_layer": self.railway_barrier_lyrx, - "output_name": f"{self.lyrx_base_path}railway_with_lyrx.lyrx", + "output_name": self.railway_with_lyrx, }, { "input_layer": self.input_railway_station_barrier, "in_symbology_layer": self.railway_station_barrier_lyrx, - "output_name": f"{self.lyrx_base_path}railway_stations_with_lyrx.lyrx", + "output_name": self.railway_stations_with_lyrx, }, ] @@ -227,17 +220,17 @@ def apply_symbology_to_the_layers(self): def barriers_for_rbc(self): input_barriers_for_rbc = [ [ - f"{self.lyrx_base_path}begrensningskurve_with_lyrx.lyrx", + self.begrensningskurve_with_lyrx, "false", f"{self.begrensningskurve_barrier_gap} Meters", ], [ - f"{self.lyrx_base_path}railway_stations_with_lyrx.lyrx", + self.railway_stations_with_lyrx, "false", f"{self.railway_station_barrier_gap} Meters", ], [ - f"{self.lyrx_base_path}railway_with_lyrx.lyrx", + self.railway_with_lyrx, "false", f"{self.railway_barrier_gap} Meters", ], @@ -250,8 +243,8 @@ def resolve_building_conflicts_1(self): try: arcpy.env.referenceScale = "100000" input_buildings_rbc_1 = [ - f"{self.lyrx_base_path}polygons_with_lyrx.lyrx", - f"{self.lyrx_base_path}building_squares_with_lyrx.lyrx", + self.polygons_with_lyrx, + self.building_squares_with_lyrx, ] arcpy.cartography.ResolveBuildingConflicts( in_buildings=input_buildings_rbc_1, @@ -275,7 +268,7 @@ def building_squares_and_polygons_to_keep_after_rbc_1(self): custom_arcpy.select_attribute_and_make_permanent_feature( input_layer=self.points_to_squares, expression=sql_expression_resolve_building_conflicts_squares, - output_name=f"{self.base_path_for_features}results_rbc_1_squares", + output_name=self.results_rbc_1_squares, ) # Sql expression to keep only building polygons that are visible (0) after the tool has run @@ -285,7 +278,7 @@ def building_squares_and_polygons_to_keep_after_rbc_1(self): custom_arcpy.select_attribute_and_make_permanent_feature( input_layer=self.input_building_polygons, expression=sql_expression_resolve_building_conflicts_polygon, - output_name=f"{self.base_path_for_features}results_rbc_1_polygons", + output_name=self.results_rbc_1_polygons, ) @timing_decorator @@ -297,20 +290,20 @@ def transforming_invisible_polygons_to_points_and_then_to_squares(self): custom_arcpy.select_attribute_and_make_permanent_feature( input_layer=self.input_building_polygons, expression=sql_expression_find_invisible_polygons, - output_name=f"{self.base_path_for_features}invisible_polygons_after_rbc_1", + output_name=self.invisible_polygons_after_rbc_1, ) # Invisible building polygons are then transformed to points arcpy.management.FeatureToPoint( - in_features=f"{self.base_path_for_features}invisible_polygons_after_rbc_1", - out_feature_class=f"{self.base_path_for_features}invisible_polygons_to_points_after_rbc_1", + in_features=self.invisible_polygons_after_rbc_1, + out_feature_class=self.invisible_polygons_to_points_after_rbc_1, point_location="INSIDE", ) # Transforms all the building points to squares polygon_processor = PolygonProcessor( - input_building_points=f"{self.base_path_for_features}invisible_polygons_to_points_after_rbc_1", - output_polygon_feature_class=f"{self.base_path_for_features}building_polygons_to_points_and_then_squares_rbc_1", + input_building_points=self.invisible_polygons_to_points_after_rbc_1, + output_polygon_feature_class=self.building_polygons_to_points_and_then_squares_rbc_1, building_symbol_dimensions=self.building_symbol_dimension, symbol_field_name="symbol_val", index_field_name="OBJECTID", @@ -320,10 +313,10 @@ def transforming_invisible_polygons_to_points_and_then_to_squares(self): # Merging squares and polygons made to points, and then squares arcpy.management.Merge( inputs=[ - f"{self.base_path_for_features}results_rbc_1_squares", - f"{self.base_path_for_features}building_polygons_to_points_and_then_squares_rbc_1", + self.results_rbc_1_squares, + self.building_polygons_to_points_and_then_squares_rbc_1, ], - output=f"{self.base_path_for_features}merged_squares_rbc1", + output=self.merged_squares_rbc1, ) @timing_decorator @@ -346,7 +339,7 @@ def calculating_symbol_val_and_nbr_for_squares(self): # Applying the symbol_val_to_nbr logic arcpy.CalculateField_management( - in_table=f"{self.base_path_for_features}merged_squares_rbc1", + in_table=self.merged_squares_rbc1, field="byggtyp_nbr", expression="symbol_val_to_nbr(!symbol_val!, !byggtyp_nbr!)", expression_type="PYTHON3", @@ -355,7 +348,7 @@ def calculating_symbol_val_and_nbr_for_squares(self): # Applying the update_symbol_val logic arcpy.CalculateField_management( - in_table=f"{self.base_path_for_features}merged_squares_rbc1", + in_table=self.merged_squares_rbc1, field="symbol_val", expression="update_symbol_val(!symbol_val!)", expression_type="PYTHON3", @@ -366,16 +359,16 @@ def calculating_symbol_val_and_nbr_for_squares(self): def adding_symbology_to_layers_being_used_for_rbc_2(self): # Building squares (from points, transformed to squares in the first function) that are kept after rbc 1 custom_arcpy.apply_symbology( - input_layer=f"{self.base_path_for_features}merged_squares_rbc1", + input_layer=self.merged_squares_rbc1, in_symbology_layer=self.building_squares_lyrx, - output_name=f"{self.lyrx_base_path}adding_symbology_to_squares_going_into_rbc2.lyrx", + output_name=self.adding_symbology_to_squares_going_into_rbc2, ) # Building polygons kept after rbc 1 custom_arcpy.apply_symbology( - input_layer=f"{self.base_path_for_features}results_rbc_1_polygons", + input_layer=self.results_rbc_1_polygons, in_symbology_layer=self.building_polygons_lyrx, - output_name=f"{self.lyrx_base_path}adding_symbology_to_polygons_going_into_rbc2.lyrx", + output_name=self.adding_symbology_to_polygons_going_into_rbc2, ) @timing_decorator @@ -384,8 +377,8 @@ def resolve_building_conflicts_2(self): arcpy.env.referenceScale = "100000" input_buildings_rbc_2 = [ - f"{self.lyrx_base_path}adding_symbology_to_squares_going_into_rbc2.lyrx", - f"{self.lyrx_base_path}adding_symbology_to_polygons_going_into_rbc2.lyrx", + self.adding_symbology_to_squares_going_into_rbc2, + self.adding_symbology_to_polygons_going_into_rbc2, ] arcpy.cartography.ResolveBuildingConflicts( @@ -405,24 +398,24 @@ def selecting_features_to_be_kept_after_rbc_2(self): # Selecting squares that should be kept after rbc 2 custom_arcpy.select_attribute_and_make_permanent_feature( - input_layer=f"{self.base_path_for_features}merged_squares_rbc1", + input_layer=self.merged_squares_rbc1, expression=sql_expression_squares, - output_name=f"{self.base_path_for_features}squares_after_rbc2", + output_name=self.squares_after_rbc2, ) # Selecting polygons that should be kept after rbc 2 custom_arcpy.select_attribute_and_make_permanent_feature( - input_layer=f"{self.base_path_for_features}results_rbc_1_polygons", + input_layer=self.results_rbc_1_polygons, expression=sql_expression_polygons, - output_name=f"{self.base_path_for_features}polygons_after_rbc2", + output_name=self.polygons_after_rbc2, ) @timing_decorator def transforming_squares_back_to_points(self): # Squares from points are transformed back to points arcpy.management.FeatureToPoint( - in_features=f"{self.base_path_for_features}squares_after_rbc2", - out_feature_class=f"{self.base_path_for_features}squares_back_to_points_after_rbc2", + in_features=self.squares_after_rbc2, + out_feature_class=self.squares_back_to_points_after_rbc2, point_location="INSIDE", ) @@ -430,12 +423,13 @@ def transforming_squares_back_to_points(self): def assigning_final_names(self): # Squares arcpy.management.CopyFeatures( - f"{self.base_path_for_features}squares_back_to_points_after_rbc2", + self.squares_back_to_points_after_rbc2, self.output_points, ) # Polygons arcpy.management.CopyFeatures( - f"{self.base_path_for_features}polygons_after_rbc2", self.output_polygons + self.polygons_after_rbc2, + self.output_polygons, ) @timing_decorator @@ -508,7 +502,6 @@ def run(self): for file in self.working_files_list_lyrx: print(f"Working file: {file}") - self.constructing_work_files() self.building_points_to_squares() self.apply_symbology_to_the_layers() self.resolve_building_conflicts_1() @@ -520,10 +513,12 @@ def run(self): self.selecting_features_to_be_kept_after_rbc_2() self.transforming_squares_back_to_points() self.assigning_final_names() - self.adding_files_to_working_list() - self.delete_working_files(self.working_files_list) + # self.adding_files_to_working_list() + # self.delete_working_files(self.working_files_list) - self.work_file_manager.cleanup_files(self.working_files_list) + self.work_file_manager_gdb.cleanup_files( + [self.working_files_list_gdb, self.working_files_list_lyrx] + ) if __name__ == "__main__": From 6b607771a06b6d42d0192515da7c787fb413a96a Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Mon, 5 Aug 2024 11:24:52 +0200 Subject: [PATCH 8/9] Removed obsolete code after WorkFileManager integration in resolve_building_conflicts.py --- .../building/resolve_building_conflicts.py | 51 ------------------- 1 file changed, 51 deletions(-) diff --git a/custom_tools/generalization_tools/building/resolve_building_conflicts.py b/custom_tools/generalization_tools/building/resolve_building_conflicts.py index 3015d7aa..b0bf3755 100644 --- a/custom_tools/generalization_tools/building/resolve_building_conflicts.py +++ b/custom_tools/generalization_tools/building/resolve_building_conflicts.py @@ -432,49 +432,6 @@ def assigning_final_names(self): self.output_polygons, ) - @timing_decorator - def adding_files_to_working_list(self): - self.working_files_list.extend( - [ - f"{self.base_path_for_features}points_to_squares", - f"{self.lyrx_base_path}building_squares_with_lyrx.lyrx", - f"{self.lyrx_base_path}polygons_with_lyrx.lyrx", - f"{self.lyrx_base_path}roads_with_lyrx.lyrx", - f"{self.lyrx_base_path}begrensningskurve_with_lyrx.lyrx", - f"{self.lyrx_base_path}railway_with_lyrx.lyrx", - f"{self.lyrx_base_path}railway_stations_with_lyrx.lyrx", - f"{self.base_path_for_features}results_rbc_1_squares", - f"{self.base_path_for_features}results_rbc_1_polygons", - f"{self.base_path_for_features}invisible_polygons_after_rbc_1", - f"{self.base_path_for_features}invisible_polygons_to_points_after_rbc_1", - f"{self.base_path_for_features}building_polygons_to_points_and_then_squares_rbc_1", - f"{self.base_path_for_features}merged_squares_rbc1", - f"{self.lyrx_base_path}adding_symbology_to_squares_going_into_rbc2.lyrx", - f"{self.lyrx_base_path}adding_symbology_to_polygons_going_into_rbc2.lyrx", - f"{self.base_path_for_features}squares_back_to_points_after_rbc2" - f"{self.base_path_for_features}squares_after_rbc2", - f"{self.base_path_for_features}polygons_after_rbc2", - self.points_to_squares, - ] - ) - - @timing_decorator - def delete_working_files(self, *file_paths): - """ - Deletes multiple feature classes or files. - """ - for file_path in file_paths: - self.delete_feature_class(file_path) - print(f"Deleted file: {file_path}") - - @staticmethod - def delete_feature_class(feature_class_path): - """ - Deletes a feature class if it exists. - """ - if arcpy.Exists(feature_class_path): - arcpy.management.Delete(feature_class_path) - @partition_io_decorator( input_param_names=[ "building_inputs", @@ -496,12 +453,6 @@ def run(self): file_names=self.working_files_list_lyrx, ) - for file in self.working_files_list_gdb: - print(f"Working file: {file}") - - for file in self.working_files_list_lyrx: - print(f"Working file: {file}") - self.building_points_to_squares() self.apply_symbology_to_the_layers() self.resolve_building_conflicts_1() @@ -513,8 +464,6 @@ def run(self): self.selecting_features_to_be_kept_after_rbc_2() self.transforming_squares_back_to_points() self.assigning_final_names() - # self.adding_files_to_working_list() - # self.delete_working_files(self.working_files_list) self.work_file_manager_gdb.cleanup_files( [self.working_files_list_gdb, self.working_files_list_lyrx] From b53e729eccf060816ee37a9852c50eefa262fd78 Mon Sep 17 00:00:00 2001 From: EllingOftedalKV Date: Mon, 5 Aug 2024 11:25:52 +0200 Subject: [PATCH 9/9] Removed old obsolete code in point_resolve_building_conflicts.py --- .../point_resolve_building_conflicts.py | 339 ------------------ 1 file changed, 339 deletions(-) diff --git a/generalization/n100/building/point_resolve_building_conflicts.py b/generalization/n100/building/point_resolve_building_conflicts.py index b6d123a6..d9d8355d 100644 --- a/generalization/n100/building/point_resolve_building_conflicts.py +++ b/generalization/n100/building/point_resolve_building_conflicts.py @@ -170,344 +170,5 @@ def resolve_building_conflicts(): resolve_building_conflicts_partition_iteration.run() -# @timing_decorator -# def building_points_to_squares(): -# # Transforms all the building points to squares -# polygon_processor = PolygonProcessor( -# input_building_points=Building_N100.building_point_buffer_displacement__displaced_building_points__n100.value, -# output_polygon_feature_class=Building_N100.point_resolve_building_conflicts___transform_points_to_square_polygons___n100_building.value, -# building_symbol_dimensions=N100_Symbology.building_symbol_dimensions.value, -# symbol_field_name="symbol_val", -# index_field_name="OBJECTID", -# ) -# polygon_processor.run() -# -# -# @timing_decorator -# def selecting_data_with_area(): -# # Selects data in Asker and Oslo only -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=input_n100.AdminFlate, -# expression="navn IN ('Asker', 'Oslo', 'Trondheim', 'Ringerike')", -# output_name=Building_N100.point_resolve_building_conflicts___selection_area_resolve_building_conflicts___n100_building.value, -# ) -# -# # List of dictionaries containing parameters for each selection -# selections = [ -# { -# "input_layer": Building_N100.polygon_resolve_building_conflicts___building_polygons_final___n100_building.value, -# "output_name": Building_N100.point_resolve_building_conflicts___building_polygon_selection_rbc___n100_building.value, -# }, -# { -# "input_layer": Building_N100.data_preparation___unsplit_roads___n100_building.value, -# "output_name": Building_N100.point_resolve_building_conflicts___road_selection_rbc___n100_building.value, -# }, -# { -# "input_layer": Building_N100.point_propagate_displacement___points_after_propagate_displacement___n100_building.value, -# "output_name": Building_N100.point_resolve_building_conflicts___building_point_selection_rbc___n100_building.value, -# }, -# { -# "input_layer": Building_N100.data_preparation___begrensningskurve_buffer_erase_2___n100_building.value, -# "output_name": Building_N100.point_resolve_building_conflicts___begrensningskurve_selection_rbc___n100_building.value, -# }, -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___transform_points_to_square_polygons___n100_building.value, -# "output_name": Building_N100.point_resolve_building_conflicts___squares_selection_rbc___n100_building.value, -# }, -# { -# "input_layer": input_n100.Bane, -# "output_name": Building_N100.polygon_resolve_building_conflicts___railway___n100_building_lyrx, -# }, -# ] -# -# # Loop over list and make selections -# for selection in selections: -# custom_arcpy.select_location_and_make_permanent_feature( -# input_layer=selection["input_layer"], -# overlap_type=custom_arcpy.OverlapType.INTERSECT, -# select_features=Building_N100.point_resolve_building_conflicts___selection_area_resolve_building_conflicts___n100_building.value, -# output_name=selection["output_name"], -# ) -# -# -# @timing_decorator -# def apply_symbology_to_the_layers(): -# # List of dictionaries containing parameters for each symbology application -# symbology_configs = [ -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___building_point_selection_rbc___n100_building.value, -# "in_symbology_layer": SymbologyN100.building_point.value, -# "output_name": Building_N100.point_resolve_building_conflicts___bygningspunkt_selection___n100_building_lyrx.value, -# }, -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___building_polygon_selection_rbc___n100_building.value, -# "in_symbology_layer": SymbologyN100.building_polygon.value, -# "output_name": Building_N100.point_resolve_building_conflicts___grunnriss_selection___n100_building_lyrx.value, -# }, -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___road_selection_rbc___n100_building.value, -# "in_symbology_layer": SymbologyN100.road.value, -# "output_name": Building_N100.point_resolve_building_conflicts___veg_sti_selection___n100_building_lyrx.value, -# }, -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___begrensningskurve_selection_rbc___n100_building.value, -# "in_symbology_layer": SymbologyN100.begrensnings_kurve_buffer.value, -# "output_name": Building_N100.point_resolve_building_conflicts___begrensningskurve_selection___n100_building_lyrx.value, -# }, -# { -# "input_layer": Building_N100.point_resolve_building_conflicts___squares_selection_rbc___n100_building.value, -# "in_symbology_layer": SymbologyN100.squares.value, -# "output_name": Building_N100.point_resolve_building_conflicts___squares_selection___n100_building_lyrx.value, -# }, -# ] -# -# # Loop over the symbology configurations and apply the function -# for symbology_config in symbology_configs: -# custom_arcpy.apply_symbology( -# input_layer=symbology_config["input_layer"], -# in_symbology_layer=symbology_config["in_symbology_layer"], -# output_name=symbology_config["output_name"], -# ) -# -# -# def barriers_for_rbc(): -# input_barriers_for_rbc = [ -# [ -# Building_N100.point_resolve_building_conflicts___veg_sti_selection___n100_building_lyrx.value, -# "false", -# f"{N100_Values.rbc_barrier_clearance_distance_m.value} Meters", # 30 Meters for all barriers -# ], -# [ -# Building_N100.point_resolve_building_conflicts___begrensningskurve_selection___n100_building_lyrx.value, -# "false", -# f"{N100_Values.rbc_barrier_clearance_distance_m.value} Meters", -# ], -# [ -# Building_N100.data_preparation___railway_stations_to_polygons_symbology___n100_building_lyrx.value, -# "false", -# f"{N100_Values.rbc_barrier_clearance_distance_m.value} Meters", -# ], -# [ -# Building_N100.polygon_resolve_building_conflicts___railway___n100_building_lyrx.value, -# "false", -# f"{N100_Values.rbc_barrier_clearance_distance_m.value} Meters", -# ], -# ] -# -# return input_barriers_for_rbc -# -# -# @timing_decorator -# def resolve_building_conflicts_1(): -# arcpy.env.referenceScale = "100000" -# -# print("Starting Resolve Building Conflicts 1 for drawn polygons") -# -# # Input point squares and building polygons -# input_buildings_rbc_1 = [ -# Building_N100.point_resolve_building_conflicts___grunnriss_selection___n100_building_lyrx.value, -# Building_N100.point_resolve_building_conflicts___squares_selection___n100_building_lyrx.value, -# ] -# -# arcpy.cartography.ResolveBuildingConflicts( -# in_buildings=input_buildings_rbc_1, -# invisibility_field="invisibility", -# in_barriers=barriers_for_rbc(), -# building_gap=f"{N100_Values.rbc_building_clearance_distance_m.value} Meters", -# minimum_size="1 meters", -# hierarchy_field="hierarchy", -# ) -# -# # Sql expression to select buildingspoints that are visible + church and hospital points -# sql_expression_resolve_building_conflicts = ( -# "(invisibility = 0) OR (symbol_val IN (1, 2, 3))" -# ) -# -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___squares_selection_rbc___n100_building.value, -# expression=sql_expression_resolve_building_conflicts, -# output_name=Building_N100.point_resolve_building_conflicts___squares_to_keep_after_rbc1___n100_building.value, -# ) -# -# -# def building_polygons_to_keep_after_rbc_1(): -# # Sql expression to keep only building polygons that are visible (0) after the tool has run -# sql_expression_resolve_building_conflicts_polygon = "invisibility = 0" -# -# # Selecting building polygons that are visible -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygon_selection_rbc___n100_building.value, -# expression=sql_expression_resolve_building_conflicts_polygon, -# output_name=Building_N100.point_resolve_building_conflicts___building_polygons_to_keep_after_rbc1___n100_building.value, -# ) -# -# -# def transforming_invisible_polygons_to_points_and_then_to_squares(): -# # Sql expression to keep only building polygons that have invisbility value 1 after the tool has run -# sql_expression_resolve_building_conflicts_polygon = "invisibility = 1" -# -# # Selecting building polygons that are invisible -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygon_selection_rbc___n100_building.value, -# expression=sql_expression_resolve_building_conflicts_polygon, -# output_name=Building_N100.point_resolve_building_conflicts___building_polygons_invisible_result_1___n100_building.value, -# ) -# -# # Building polygons that are made invisible are transformed to points -# arcpy.management.FeatureToPoint( -# in_features=Building_N100.point_resolve_building_conflicts___building_polygons_invisible_result_1___n100_building.value, -# out_feature_class=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_result_1___n100_building.value, -# point_location="INSIDE", -# ) -# -# code_block_symbol_val_to_nbr = ( -# "def symbol_val_to_nbr(symbol_val, byggtyp_nbr):\n" -# " if symbol_val == -99:\n" -# " return 729\n" -# " return byggtyp_nbr" -# ) -# -# # Code block to update the symbol_val to reflect the new byggtyp_nbr -# code_block_update_symbol_val = ( -# "def update_symbol_val(symbol_val):\n" -# " if symbol_val == -99:\n" -# " return 8\n" -# " return symbol_val" -# ) -# -# # Applying the symbol_val_to_nbr logic -# arcpy.CalculateField_management( -# in_table=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_result_1___n100_building.value, -# field="byggtyp_nbr", -# expression="symbol_val_to_nbr(!symbol_val!, !byggtyp_nbr!)", -# expression_type="PYTHON3", -# code_block=code_block_symbol_val_to_nbr, -# ) -# -# # Applying the update_symbol_val logic -# arcpy.CalculateField_management( -# in_table=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_result_1___n100_building.value, -# field="symbol_val", -# expression="update_symbol_val(!symbol_val!)", -# expression_type="PYTHON3", -# code_block=code_block_update_symbol_val, -# ) -# -# # Transforms all the building points to squares -# polygon_processor = PolygonProcessor( -# input_building_points=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_result_1___n100_building.value, -# output_polygon_feature_class=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_and_then_squares___n100_building.value, -# building_symbol_dimensions=N100_Symbology.building_symbol_dimensions.value, -# symbol_field_name="symbol_val", -# index_field_name="OBJECTID", -# ) -# polygon_processor.run() -# -# -# def adding_symbology_to_layers_being_used_for_rbc_2(): -# # Building squares (from points, transformed to squares in the first function) that are kept after rbc 1 -# custom_arcpy.apply_symbology( -# input_layer=Building_N100.point_resolve_building_conflicts___squares_to_keep_after_rbc1___n100_building.value, -# in_symbology_layer=SymbologyN100.squares.value, -# output_name=Building_N100.point_resolve_building_conflicts___squares_to_keep_after_rbc1___n100_building_lyrx.value, -# ) -# # Building polygons kept after rbc 1 -# custom_arcpy.apply_symbology( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygons_to_keep_after_rbc1___n100_building.value, -# in_symbology_layer=SymbologyN100.building_polygon.value, -# output_name=Building_N100.point_resolve_building_conflicts___building_polygons_to_keep_after_rbc1___n100_building_lyrx.value, -# ) -# # Squares made from points, which again comes from invisible building polygons after rbc 1 -# custom_arcpy.apply_symbology( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_and_then_squares___n100_building.value, -# in_symbology_layer=SymbologyN100.squares.value, -# output_name=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_and_then_squares___n100_building_lyrx.value, -# ) -# -# -# def resolve_building_conflicts_2(): -# print("Starting resolve building conflicts 2") -# arcpy.env.referenceScale = "100000" -# -# input_buildings_rbc_2 = [ -# Building_N100.point_resolve_building_conflicts___squares_to_keep_after_rbc1___n100_building_lyrx.value, -# Building_N100.point_resolve_building_conflicts___building_polygons_to_keep_after_rbc1___n100_building_lyrx.value, -# Building_N100.point_resolve_building_conflicts___building_polygons_to_points_and_then_squares___n100_building_lyrx.value, -# ] -# -# arcpy.cartography.ResolveBuildingConflicts( -# in_buildings=input_buildings_rbc_2, -# invisibility_field="invisibility", -# in_barriers=barriers_for_rbc(), -# building_gap=f"{N100_Values.rbc_building_clearance_distance_m.value} Meters", -# minimum_size="1 meters", -# hierarchy_field="hierarchy", -# ) -# -# -# def selecting_features_to_be_kept_after_rbc_2(): -# sql_expression_resolve_building_conflicts = ( -# "(invisibility = 0) OR (symbol_val IN (1, 2, 3))" -# ) -# -# # Selecting polygons that are to be kept after rbc 2 -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygons_to_keep_after_rbc1___n100_building.value, -# expression=sql_expression_resolve_building_conflicts, -# output_name=Building_N100.point_resolve_building_conflicts___building_polygons_rbc2___n100_building.value, -# ) -# -# # Selecting squares from points -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___squares_to_keep_after_rbc1___n100_building.value, -# expression=sql_expression_resolve_building_conflicts, -# output_name=Building_N100.point_resolve_building_conflicts___squares_from_points_rbc2___n100_building.value, -# ) -# # Selecting squares from polygons (polygons that were transformed to points and then squares) -# custom_arcpy.select_attribute_and_make_permanent_feature( -# input_layer=Building_N100.point_resolve_building_conflicts___building_polygons_to_points_and_then_squares___n100_building.value, -# expression=sql_expression_resolve_building_conflicts, -# output_name=Building_N100.point_resolve_building_conflicts___squares_from_polygons_rbc2___n100_building.value, -# ) -# -# -# def transforming_squares_back_to_points(): -# # Squares from points are transformed back to points -# arcpy.management.FeatureToPoint( -# in_features=Building_N100.point_resolve_building_conflicts___squares_from_points_rbc2___n100_building.value, -# out_feature_class=Building_N100.point_resolve_building_conflicts___squares_from_points_transformed_back_to_points___n100_building.value, -# point_location="INSIDE", -# ) -# -# # Squares from polygons are transformed to points -# arcpy.management.FeatureToPoint( -# in_features=Building_N100.point_resolve_building_conflicts___squares_from_polygons_rbc2___n100_building.value, -# out_feature_class=Building_N100.point_resolve_building_conflicts___squares_from_polygons_transformed_to_points___n100_building.value, -# point_location="INSIDE", -# ) -# -# -# def merging_building_points(): -# arcpy.management.Merge( -# inputs=[ -# Building_N100.point_resolve_building_conflicts___squares_from_points_transformed_back_to_points___n100_building.value, -# Building_N100.point_resolve_building_conflicts___squares_from_polygons_transformed_to_points___n100_building.value, -# ], -# output=Building_N100.point_resolve_building_conflicts___final_points_merged___n100_building.value, -# ) -# -# -# def assigning_final_names(): -# arcpy.management.CopyFeatures( -# Building_N100.point_resolve_building_conflicts___building_polygons_rbc2___n100_building.value, -# Building_N100.point_resolve_building_conflicts___building_polygons_final___n100_building.value, -# ) -# -# arcpy.management.CopyFeatures( -# Building_N100.point_resolve_building_conflicts___final_points_merged___n100_building.value, -# Building_N100.point_resolve_building_conflicts___building_points_final___n100_building.value, -# ) - - if __name__ == "__main__": main()