custom_tools.general_tools package

Submodules

custom_tools.general_tools.custom_arcpy module

class custom_tools.general_tools.custom_arcpy.SelectionType(*values)[source]

Bases: Enum

Summary:

Enum class that holds the strings for the selection types.

NEW_SELECTION = 'NEW_SELECTION'
ADD_TO_SELECTION = 'ADD_TO_SELECTION'
REMOVE_FROM_SELECTION = 'REMOVE_FROM_SELECTION'
SUBSET_SELECTION = 'SUBSET_SELECTION'
SWITCH_SELECTION = 'SWITCH_SELECTION'
CLEAR_SELECTION = 'CLEAR_SELECTION'
class custom_tools.general_tools.custom_arcpy.OverlapType(*values)[source]

Bases: Enum

Summary:

Enum class that holds the strings for overlap types for select by location.

INTERSECT = 'INTERSECT'
INTERSECT_3D = 'INTERSECT_3D'
INTERSECT_DBMS = 'INTERSECT_DBMS'
WITHIN_A_DISTANCE = 'WITHIN_A_DISTANCE'
WITHIN_A_DISTANCE_3D = 'WITHIN_A_DISTANCE_3D'
WITHIN_A_DISTANCE_GEODESIC = 'WITHIN_A_DISTANCE_GEODESIC'
CONTAINS = 'CONTAINS'
COMPLETELY_CONTAINS = 'COMPLETELY_CONTAINS'
CONTAINS_CLEMENTINI = 'CONTAINS_CLEMENTINI'
WITHIN = 'WITHIN'
COMPLETELY_WITHIN = 'COMPLETELY_WITHIN'
WITHIN_CLEMENTINI = 'WITHIN_CLEMENTINI'
ARE_IDENTICAL_TO = 'ARE_IDENTICAL_TO'
BOUNDARY_TOUCHES = 'BOUNDARY_TOUCHES'
SHARE_A_LINE_SEGMENT_WITH = 'SHARE_A_LINE_SEGMENT_WITH'
CROSSED_BY_THE_OUTLINE_OF = 'CROSSED_BY_THE_OUTLINE_OF'
HAVE_THEIR_CENTER_IN = 'HAVE_THEIR_CENTER_IN'
custom_tools.general_tools.custom_arcpy.resolve_enum(enum_class, value)[source]

Resolves various types of inputs to their corresponding enum member within a specified enumeration class.

This function is designed to enhance flexibility in function arguments, allowing the use of enum members, their string names, or their values interchangeably. This is particularly useful in scenarios where function parameters might be specified in different formats, ensuring compatibility and ease of use.

Parameters:
  • enum_class (Enum) – The enumeration class to which the value is supposed to belong.

  • value (str, Enum, or any) – The input value to resolve. This can be the enum member itself, its string name,

  • gracefully. (or its associated value. The function is designed to handle these various formats)

custom_tools.general_tools.custom_arcpy.select_attribute_and_make_feature_layer(input_layer, expression, output_name, selection_type='NEW_SELECTION', inverted=False)[source]
Summary:

Selects features based on an attribute query and creates a new feature layer with the selected features.

Details:
  • A temporary feature layer is created from the input_layer.

  • The selection type, defined by selection_type, is applied to this layer.

  • The selected features are stored in a new temporary feature layer

Parameters:
  • input_layer (str) – The path or name of the input feature layer.

  • expression (str) – The SQL expression for selecting features.

  • output_name (str) – The name of the output feature layer.

  • selection_type (str, optional) – Type of selection (e.g., “NEW_SELECTION”). Defaults to “NEW_SELECTION”.

  • inverted (bool, optional) – If True, inverts the selection. Defaults to False.

Example

>>> custom_arcpy.select_attribute_and_make_feature_layer(
...     input_layer=input_n100.ArealdekkeFlate,
...     expression=urban_areas_sql_expr,
...     output_name=Building_N100.data_preparation___urban_area_selection_n100___n100_building.value,
... )
'Building_N100.adding_matrikkel_as_points__urban_area_selection_n100__n100' created temporarily.
custom_tools.general_tools.custom_arcpy.select_attribute_and_make_permanent_feature(input_layer, expression, output_name, selection_type='NEW_SELECTION', inverted=False)[source]
Summary:

Selects features based on an attribute query and creates a new feature layer, then stores the selected features permanently in the specified output feature class.

Details:
  • A temporary feature layer is created from the input_layer.

  • The selection_type determines how the selection is applied to this layer. If inverted is True, the selection is inverted.

  • The selection is done on the feature layer using the expression.

  • The selected features are stored permanently in a new feature class specified by output_name using copy features.

Parameters:
  • input_layer (str) – The path or name of the input feature layer for selection.

  • expression (str) – The SQL expression used for selecting features.

  • output_name (str) – The name for the new, permanent output feature class.

  • selection_type (str, optional) – Specifies the type of selection. Defaults to “NEW_SELECTION”.

  • inverted (bool, optional) – If set to True, inverts the selection. Defaults to False.

Example

>>> custom_arcpy.select_attribute_and_make_permanent_feature(
...     input_layer=input_n100.ArealdekkeFlate,
...     expression=urban_areas_sql_expr,
...     output_name=Building_N100.adding_matrikkel_as_points__urban_area_selection_n100__permanent,
... )
'Building_N100.adding_matrikkel_as_points__urban_area_selection_n100__permanent' created permanently.
custom_tools.general_tools.custom_arcpy.select_location_and_make_feature_layer(input_layer, overlap_type, select_features, output_name, selection_type=SelectionType.NEW_SELECTION, inverted=False, search_distance=None)[source]
Summary:

Selects features based from the input layer based on their spatial relationship to the selection features and creates a new, temporary feature layer as an output.

Details:
  • Creates a feature layer from the input_layer.

  • Depending on the overlap_type, features in input_layer that spatially relate to select_features are selected.

  • If overlap_type requires a search_distance and it is provided, the distance is used in the selection.

  • The selection can be inverted if inverted is set to True.

  • The selected features are stored in a new temporary feature layer named output_name.

Parameters:
  • input_layer (str) – The path or name of the input feature layer.

  • overlap_type (str or OverlapType) – The spatial relationship type to use for selecting features.

  • select_features (str) – The path or name of the feature layer used to select features from the input_layer.

  • output_name (str) – The name of the output feature layer.

  • selection_type (SelectionType, optional) – Specifies the type of selection. Defaults to SelectionType.NEW_SELECTION.

  • inverted (bool, optional) – If True, inverts the selection. Defaults to False.

  • search_distance (str, optional) – A distance value that defines the proximity for selecting features. Required for certain overlap_type values.

Example

>>> custom_arcpy.select_location_and_make_feature_layer(
...     input_layer=Building_N100.data_preparation___polygons_that_are_large_enough___n100_building.value,
...     overlap_type=custom_arcpy.OverlapType.INTERSECT.value,
...     select_features=Building_N100.grunnriss_to_point__aggregated_polygon__n100.value,
...      output_name=Building_N100.simplify_polygons___not_intersect_aggregated_and_original_polygon___n100_building.value,
...     inverted=True,
... )
'grunnriss_to_point__intersect_aggregated_and_original__n100' created temporarily.
custom_tools.general_tools.custom_arcpy.select_location_and_make_permanent_feature(input_layer, overlap_type, select_features, output_name, selection_type=SelectionType.NEW_SELECTION, inverted=False, search_distance=None)[source]
Summary:

Selects features based from the input layer based on their spatial relationship to the selection features and creates a new, permanent feature class as an output.

Details:
  • Initiates by creating a temporary feature layer from input_layer.

  • Applies a spatial selection based on overlap_type between the input_layer and select_features.

  • Utilizes search_distance if required by the overlap_type and provided, to define the proximity for selection.

  • The selection can be inverted if inverted is set to True, meaning it will select all features not meeting the spatial relationship criteria.

  • The selected features are stored permanently in a new feature class specified by output_name.

  • Cleans up by deleting the temporary feature layer to maintain a tidy workspace.

Parameters:
  • input_layer (str) – The path or name of the input feature layer.

  • overlap_type (str or OverlapType) – The type of spatial relationship to use for feature selection.

  • select_features (str) – The feature layer used as a reference for spatial selection.

  • output_name (str) – The name for the new, permanent feature class to store selected features.

  • selection_type (SelectionType, optional) – The method of selection to apply. Defaults to SelectionType.NEW_SELECTION.

  • inverted (bool, optional) – If set to True, the selection will be inverted. Defaults to False.

  • search_distance (str, optional) – The distance within which to select features, necessary for certain types of spatial selections like WITHIN_A_DISTANCE.

Example

>>> custom_arcpy.select_location_and_make_permanent_feature(
...     input_layer=Building_N100.data_selection___begrensningskurve_n100_input_data___n100_building.value,
...     overlap_type=OverlapType.WITHIN_A_DISTANCE.value,
...     select_features=Building_N100.polygon_propogate_displacement___building_polygons_after_displacement___n100_building.value,
...     output_name=Building_N100.polygon_resolve_building_conflicts___begrensningskurve_500m_from_displaced_polygon___n100_building.value,
...     search_distance="500 Meters",
... )
'polygon_propogate_displacement___begrensningskurve_500_m_from_displaced_polygon___n100_building' created permanently.
custom_tools.general_tools.custom_arcpy.apply_symbology(input_layer, in_symbology_layer, output_name, grouped_lyrx=False, target_layer_name=None)[source]
Summary:

Applies symbology from a specified lyrx file to an input feature layer and saves the result as a new lyrx file.

Details:
  • Creates a temporary feature layer from the input_layer.

  • Applies symbology to the temporary layer using the symbology defined in in_symbology_layer.

  • The symbology settings are maintained as they are in the symbology layer file.

  • Saves the temporary layer with the applied symbology to a new layer file specified by output_name.

  • Deletes the temporary layer to clean up the workspace.

  • A confirmation message is printed indicating the successful creation of the output layer file.

Parameters:
  • input_layer (str) – The path or name of the input feature layer to which symbology will be applied.

  • in_symbology_layer (str) – The path to the layer file (.lyrx) containing the desired symbology settings.

  • output_name (str) – The name (including path) for the output layer file (.lyrx) with the applied symbology.

  • grouped_lyrx (bool, optional) – If True, the symbology is applied to the entire layer group. Defaults to False.

  • target_layer_name (str, optional) – The name of the target layer within the layer group to which symbology is applied. Defaults to None.

Example

>>> custom_arcpy.apply_symbology_to_the_layers(
...     input_layer=Building_N100.point_displacement_with_buffer___merged_buffer_displaced_points___n100_building.value,
...     in_symbology_layer=config.symbology_n100_grunnriss,
...     output_name=Building_N100.polygon_resolve_building_conflicts___building_polygon___n100_building_lyrx.value,
... )
'apply_symbology_to_layers__building_polygon__n100__lyrx.lyrx file created.'

custom_tools.general_tools.file_utilities module

class custom_tools.general_tools.file_utilities.FeatureClassCreator(template_fc, input_fc, output_fc, object_type='POLYGON', delete_existing=False)[source]

Bases: object

run()[source]

Executes the process of creating a new feature class and appending geometry from the input feature class.

class custom_tools.general_tools.file_utilities.WorkFileManager(unique_id: int, root_file: str = None, write_to_memory: bool = True, keep_files: bool = False)[source]

Bases: object

What:

This class handles the creation and deletion of work files used in other classes or processes. It is designed to make it easy to switch between writing to disk and in-memory, and to delete/stop deleting work files to better troubleshoot issues. This class is not intended to be used to create final outputs of logics or processes.

How:

The same instance of WorkFileManager can create and manage different structures containing files. for each call of the setup_work_file_paths is designed to take a single structure. Each file path generated by the WorkFileManager is tracked by the created_paths attribute so if you do not need stage the deletion of the files you can simply call the delete_created_files method without any parameters.

Parameters:
  • unique_id (int) – Used to generate unique file names, can be self value or an iterated number.

  • root_file (str) – The core file name used to generate unique file names.

  • write_to_memory (bool) – Defaults to True, write to memory if True, write to disk if False.

  • keep_files (bool) – Defaults to False, delete work files if True, keep work files if False.

general_files_directory_name = 'general_files'
lyrx_directory_name = 'lyrx_outputs'
generate_output(instance: object, name: str, iteration_index: int) str[source]
What:

Generates a unique file path for a given base name and iteration index. Designed to allow users of WorkFileManager to generate indexed outputs in a loop.

Parameters:
  • instance (object) – The caller instance to update attributes on if needed.

  • name (str) – The base name of the work file.

  • iteration_index (int) – The current iteration index for uniqueness.

  • instance – The caller instance to update attributes on if needed.

Returns:

The generated file path.

Return type:

str

setup_work_file_paths(instance: object, file_structure: Any, keys_to_update: str = None, add_key: str = None, file_type: str = 'gdb', index: int = None) Any[source]
What:

Generates file paths for supported structures and sets them as attributes on the instance. Currently tested and supported structures include: - str - list[str] - dict[str, str] - list[dict[str, str]]

Parameters:
  • instance (object) – The instance to set the file paths as attributes on.

  • file_structure (Any) – The input structure to process and return.

  • keys_to_update (str, optional) – Keys to update if file_structure is a dictionary.

  • add_key (str, optional) – An additional key to add to the dictionary.

  • file_type (str, optional) – The type of file path to generate. Defaults to “gdb”.

  • index (int, optional) – Index to ensure uniqueness in file names when processing lists of dicts.

Returns:

The same structure as file_structure, updated with generated file paths.

Return type:

Any

setup_dynamic_file_paths(base_name: str, count: int, file_type: str = 'gdb') list[str][source]

Generates a list of file paths for a dynamic number of files based on a base name.

Parameters:
  • base_name (str) – The base name to use for generating file paths.

  • count (int) – The number of file paths to generate.

  • file_type (str, optional) – The file type for the generated paths. Defaults to “gdb”.

Returns:

A list of generated file paths.

Return type:

list[str]

delete_created_files(delete_targets: list[str] = None, exceptions: list[str] = None, delete_files: list[str] = None)[source]
What:

Deletes the created paths, defaults to deleting all created paths, but can target or exclude specific paths.

Parameters:
  • delete_targets (list[str], optional) – List of paths to delete. Defaults to None.

  • exceptions (list[str], optional) – List of paths to exclude from deletion. Defaults to None.

  • delete_files (bool, optional) – Whether to delete files. Defaults to None, which uses self.keep_files.

static list_contents(data: Any, title: str = 'Contents')[source]

Pretty prints the contents of a data structure (list, dict, or other serializable objects).

Parameters:
  • data (Any) – The data structure to print (list, dict, or other serializable objects).

  • title (str, optional) – A title to display before printing. Defaults to “Contents”.

static apply_to_structure(data, func, **key_map)[source]
What:

Applies a function to elements within a supported data structure. Designed to work with dictionaries, lists of dictionaries, and extensible for other structures.

How:

Maps specified keys in the data structure to the function’s parameters and applies the function to each valid element.

Parameters:
  • data (Union[dict, list[dict]]) – The data structure to process.

  • func (callable) – The function to apply. The keys in key_map should match the function parameters.

  • **key_map (str) – Mapping of function parameter names to keys in the data structure.

Raises:
  • TypeError – If the data type is unsupported.

  • KeyError – If a required key is missing in a dictionary.

static extract_key_by_alias(data: list[dict], unique_alias: str, key: str) str[source]

Extracts the value of a specific key from the dictionary with the specified alias.

Parameters:
  • data (list[dict]) – The input list of dictionaries.

  • unique_alias (str) – The alias identifying the target dictionary.

  • key (str) – The key to extract the value from.

Returns:

The value associated with the key in the specified dictionary.

Return type:

str

Raises:
  • ValueError – If no dictionary with the specified alias is found.

  • KeyError – If the key does not exist in the identified dictionary.

static extract_key_all(data: list[dict], key: str) list[str][source]

Extracts all values for a specific key across all dictionaries.

static update_key_by_alias(data: list[dict], unique_alias: str, key: str, new_value: str) None[source]

Updates the value of a specific key in the dictionary with the specified alias.

Parameters:
  • data (list[dict]) – The input list of dictionaries.

  • unique_alias (str) – The alias identifying the target dictionary.

  • key (str) – The key to update the value for.

  • new_value (str) – The new value to set for the key.

Raises:
  • ValueError – If no dictionary with the specified alias is found.

  • KeyError – If the key does not exist in the identified dictionary.

custom_tools.general_tools.file_utilities.compare_feature_classes(feature_class_1, feature_class_2)[source]
custom_tools.general_tools.file_utilities.reclassify_value(input_table: str, target_field: str, target_value: str, replace_value: str, reference_field: str = None, logic_format: str = 'PYTHON3') None[source]
custom_tools.general_tools.file_utilities.deleting_added_field_from_feature_to_x(input_file_feature: str = None, field_name_feature: str = None) None[source]
custom_tools.general_tools.file_utilities.get_all_fields(input_fields, *added_field_sets)[source]

Combines an input fields list with any number of additional field sets. Assumes each added field set is a list of [field_name, field_type] pairs.

custom_tools.general_tools.file_utilities.count_objects(input_layer)[source]

custom_tools.general_tools.geometry_tools module

custom_tools.general_tools.graph module

custom_tools.general_tools.line_to_buffer_symbology module

class custom_tools.general_tools.line_to_buffer_symbology.LineToBufferSymbology(input_road_lines: str, sql_selection_query: dict, output_road_buffer: str, buffer_factor: int | float = 1, fixed_buffer_addition: int | float = 0, write_work_files_to_memory: bool = True, keep_work_files: bool = False, root_file: str = None)[source]

Bases: object

selecting_different_road_lines(sql_query: str, selection_output_name: str)[source]
What:

Selects road lines based on the provided SQL query and creates a feature layer or a permanent feature class depending on the write_work_files_to_memory flag.

Parameters:
  • sql_query (str) – The SQL query string used to select the road lines.

  • selection_output_name (str) – The name for the output feature layer or file.

creating_buffer_from_selected_lines(selection_output_name, buffer_width, buffer_output_name)[source]

Creates a buffer around the selected road lines.

static merge_buffers(buffer_output_names, merged_output_name)[source]

Merges multiple buffer outputs into a single feature class.

process_each_query(sql_query, original_width, counter)[source]

Processes each SQL query to select road lines and create buffers.

delete_working_files(*file_paths)[source]

Deletes multiple feature classes or files.

delete_feature_class(feature_class_path)[source]

Deletes a feature class if it exists.

process_queries()[source]

Processes all SQL queries to create buffers and handle merging.

run()[source]

custom_tools.general_tools.partition_iterator module

custom_tools.general_tools.polygon_processor module

custom_tools.general_tools.print_logger module

class custom_tools.general_tools.print_logger.LogPath[source]

Bases: object

N100Building = 'C:\\path\\to\\folder\\you\\want\\your\\outputs\\in\\ag_outputs\\n100\\building\\general_files\\app.log'
N100River = 'C:\\path\\to\\folder\\you\\want\\your\\outputs\\in\\ag_outputs\\n100\\river\\general_files\\app.log'
custom_tools.general_tools.print_logger.setup_logger(scale, object_type, log_directory='logs', filename='app.log')[source]

WORK IN PROGRESS NOT DONE Creates a logger for the specified scale and object type. Log files will be stored in a directory structure matching the scale and object type.

Parameters:
  • scale – The scale for the log (e.g., ‘n100’).

  • object_type – The object type for the log (e.g., ‘bygning’).

  • log_directory – Base directory for logs. Defaults to ‘logs’.

  • filename – Default filename for the log. Defaults to ‘app.log’.

Returns:

Logger instance for the specified scale and object type.

custom_tools.general_tools.study_area_selector module

class custom_tools.general_tools.study_area_selector.StudyAreaSelector(input_output_file_dict: dict, selecting_file: str, select_local: bool = False, selecting_sql_expression: str = None)[source]

Bases: object

select_study_area()[source]
use_global_files()[source]
delete_working_files(*file_paths)[source]

Deletes multiple feature classes or files.

delete_feature_class(feature_class_path)[source]

Deletes a feature class if it exists.

run()[source]

Module contents