Skip to content

Commit

Permalink
Merge pull request #98 from kartverket/environment_settings_refactor
Browse files Browse the repository at this point in the history
Environment settings refactor
  • Loading branch information
EllingOftedalKV authored Feb 29, 2024
2 parents e03db28 + d342ab7 commit dd52b4f
Show file tree
Hide file tree
Showing 30 changed files with 228 additions and 333 deletions.
20 changes: 8 additions & 12 deletions custom_tools/partition_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PartitionIterator:

def __init__(
self,
inputs,
outputs,
alias_path_inputs,
alias_path_outputs,
root_file_partition_iterator,
scale,
output_feature_class,
Expand All @@ -30,15 +30,15 @@ def __init__(
"""
Initialize the PartitionIterator with input datasets for partitioning and processing.
:param inputs: A dictionary of input feature class paths with their aliases.
:param alias_path_inputs: A dictionary of input feature class paths with their aliases.
:param root_file_partition_iterator: Base path for in progress outputs.
:param scale: Scale for the partitions.
:param output_feature_class: The output feature class for final results.
:param feature_count: Feature count for cartographic partitioning.
:param partition_method: Method used for creating cartographic partitions.
"""
self.inputs = inputs
self.outputs = outputs
self.inputs = alias_path_inputs
self.outputs = alias_path_outputs
self.root_file_partition_iterator = root_file_partition_iterator
self.scale = scale
self.output_feature_class = output_feature_class
Expand All @@ -56,10 +56,6 @@ def __init__(
self.search_distance = search_distance
self.object_id_field = object_id_field

def setup_arcpy_environment(self):
# Set up the ArcPy environment
environment_setup.general_setup()

def create_cartographic_partitions(self):
"""
Creates cartographic partitions based on the input feature classes.
Expand Down Expand Up @@ -396,7 +392,7 @@ def partition_iteration(
print(f"Finished iteration {object_id}")

def run(self):
self.setup_arcpy_environment()
environment_setup.main()
self.create_cartographic_partitions()

max_object_id = self.pre_iteration()
Expand Down Expand Up @@ -442,8 +438,8 @@ def run(self):

# Instantiate PartitionIterator with necessary parameters
partition_iterator = PartitionIterator(
inputs=inputs,
outputs=outputs,
alias_path_inputs=inputs,
alias_path_outputs=outputs,
root_file_partition_iterator=Building_N100.iteration__partition_iterator__n100.value,
scale=env_setup.global_config.scale_n100,
output_feature_class=Building_N100.iteration__partition_iterator_final_output__n100.value,
Expand Down
1 change: 0 additions & 1 deletion custom_tools/polygon_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ def run(self):
)

self.create_output_feature_class_if_not_exists()
# Initialize attributes that depend on external resources here

# Preparing data for processing
input_data_array = arcpy.da.FeatureClassToNumPyArray(
Expand Down
2 changes: 1 addition & 1 deletion custom_tools/stress_testing_tools/stress_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from custom_tools import custom_arcpy

environment_setup.general_setup()
environment_setup.main()


def stress_test_select_location_and_make_permanent_feature():
Expand Down
197 changes: 178 additions & 19 deletions env_setup/environment_setup.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,190 @@
# Importing custom files relative to the root path
import config

# Importing general packages
import arcpy
import os

import config
from env_setup.global_config import (
scale_n50,
scale_n100,
scale_n250,
scale_n500,
object_admin,
object_arealdekke_flate,
object_bygning,
object_elv_bekk,
object_veg_sti,
main_directory_name,
lyrx_directory_name,
general_files_name,
)

project_spatial_reference = 25833


def general_setup():
"""Set up the ArcGIS Pro environment.
def main():
"""
Initializes and executes the setup for ArcGIS Pro environment and project directory structure.
Summary:
This function performs the essential initialization tasks for setting up the ArcGIS Pro
environment and creating a predefined directory structure for the project.
Details:
- Initializes the ArcGIS environment setup by configuring workspace, output coordinate system (EPSG:25833),
xy tolerance (0.02 meters), and xy resolution (0.01 meters), parallel processing factor and set the overwrite output flag to True.
- Sets up the project directory structure, including creating geodatabases and layer files directories.
"""
arc_gis_environment_setup = ArcGisEnvironmentSetup()
arc_gis_environment_setup.setup()

directory_setup_instance = ProjectDirectorySetup()
directory_setup_instance.setup()


class ArcGisEnvironmentSetup:
"""
Configures and initializes the ArcGIS Pro environment settings.
Summary:
Sets up the ArcGIS Pro environment with specified workspace and spatial reference. Ensures setup is performed only once globally.
Details:
- Checks if setup has already been done globally to avoid duplication.
- Sets `arcpy.env.overwriteOutput` to True, ensuring existing files can be overwritten.
- Configures `arcpy.env.workspace` with the specified workspace path.
- Sets the output coordinate system to the specified spatial reference (EPSG code).
- Establishes `arcpy.env.XYTolerance` and `arcpy.env.XYResolution` for geometric precision.
- Adjusts `arcpy.env.parallelProcessingFactor` according to CPU percentage configured, optimizing performance.
Attributes:
workspace (str): The directory path for the ArcGIS workspace.
spatial_reference (int): The EPSG code for the spatial reference, defaulting to 25833 (ETRS89 / UTM zone 33N).
"""

_setup_done_globally = False

def __init__(
self,
workspace=config.default_project_workspace,
spatial_reference=25833,
):
self.workspace = workspace
self.spatial_reference = spatial_reference

def setup(self):
if ArcGisEnvironmentSetup._setup_done_globally:
print("ArcGIS Pro environment setup has already been completed. Skipping.")
return

arcpy.env.overwriteOutput = True
arcpy.env.workspace = self.workspace
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(
self.spatial_reference
)
arcpy.env.XYTolerance = "0.02 Meters"
arcpy.env.XYResolution = "0.01 Meters"
arcpy.env.parallelProcessingFactor = config.cpu_percentage

ArcGisEnvironmentSetup._setup_done_globally = True

print("ArcGIS Pro environment setup completed with the following settings:")
print("- Overwrite Output: True")
print(f"- Workspace: {arcpy.env.workspace}")
print(f"- Output Coordinate System: EPSG:{self.spatial_reference}")
print(f"- XY Tolerance: {arcpy.env.XYTolerance}")
print(f"- XY Resolution: {arcpy.env.XYResolution}")
print(f"- Parallel Processing Factor: {arcpy.env.parallelProcessingFactor}")
print("ArcGIS Pro environment setup completed.\n")

Parameters:
- workspace (str): The workspace path. Defaults to what's set in the config.
- Spatial Reference EPSG 3045 = ETRS89 / UTM zone 33N

class ProjectDirectorySetup:
"""
Creates and configures the project directory structure and geodatabases.
Summary:
Establishes a predefined directory structure for project files and geodatabases, ensuring it's done only once globally.
Details:
- Checks if the global setup has already been completed to prevent redundancy.
- Creates a main directory and specified subdirectories for organizing project files.
- Generates geodatabases in the designated subdirectories for data storage.
- Sets up directories for layer files (`lyrx`) to organize map layer configurations.
- Creates a directory for general files, facilitating organized storage of miscellaneous project files.
Attributes:
base_directory (str): The root directory for the project structure.
sub_directories (list): A list of names for subdirectories to be created within the project structure.
gdb_names (list): Names of the geodatabases to be created in each subdirectory.
"""

_setup_done_globally = False

def __init__(self, base_directory=config.output_folder):
self.base_directory = base_directory
self.sub_directories = [
scale_n50,
scale_n100,
scale_n250,
scale_n500,
]
self.gdb_names = [
object_admin,
object_arealdekke_flate,
object_bygning,
object_elv_bekk,
object_veg_sti,
]

def setup(self):
if ProjectDirectorySetup._setup_done_globally:
print("Global setup has already been completed. Skipping.")
return

self.create_directory_structure()
self.create_gdbs_in_subdirs()
self.create_lyrx_directory_structure()
self.create_general_files_structure()

ProjectDirectorySetup._setup_done_globally = True

def create_directory_structure(self):
main_directory = os.path.join(self.base_directory, main_directory_name)
os.makedirs(main_directory, exist_ok=True)
for subdir in self.sub_directories:
path = os.path.join(main_directory, subdir)
os.makedirs(path, exist_ok=True)
print(f"Created directory: {path}")

def create_gdbs_in_subdirs(self):
for subdir in self.sub_directories:
subdir_path = os.path.join(
self.base_directory, "automatic_generalization_outputs", subdir
)
for gdb_name in self.gdb_names:
gdb_path = os.path.join(subdir_path, f"{gdb_name}.gdb")
if not arcpy.Exists(gdb_path):
arcpy.CreateFileGDB_management(subdir_path, f"{gdb_name}.gdb")
print(f"Created GDB: {gdb_path}")
else:
print(f"GDB already exists: {gdb_path}")

arcpy.env.overwriteOutput = True
arcpy.env.workspace = config.default_project_workspace
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(project_spatial_reference)
arcpy.env.XYTolerance = "0.02 Meters"
arcpy.env.XYResolution = "0.01 Meters"
arcpy.env.parallelProcessingFactor = config.cpu_percentage
def create_lyrx_directory_structure(self):
for subdir in self.sub_directories:
lyrx_directory_path = os.path.join(
self.base_directory, main_directory_name, subdir, lyrx_directory_name
)
os.makedirs(lyrx_directory_path, exist_ok=True)
print(f"Created directory: {lyrx_directory_path}")

print(
f"Workspace environment set up with workspace: {config.default_project_workspace}"
)
def create_general_files_structure(self):
main_directory = os.path.join(self.base_directory, main_directory_name)
os.makedirs(main_directory, exist_ok=True)
for subdir in self.sub_directories:
general_files_path = os.path.join(
main_directory, subdir, general_files_name
)
os.makedirs(general_files_path, exist_ok=True)
print(f"Created 'general_files' folder: {general_files_path}")


if __name__ == "__main__":
general_setup()
main()
Loading

0 comments on commit dd52b4f

Please sign in to comment.