Skip to content

Commit

Permalink
Merge pull request #81 from kartverket/waterpolygon_centerline
Browse files Browse the repository at this point in the history
Waterpolygon centerline
  • Loading branch information
EllingOftedalKV authored Feb 20, 2024
2 parents c915cc1 + 5a7b5f5 commit 412fd01
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 9 deletions.
93 changes: 93 additions & 0 deletions custom_tools/file_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import arcpy
import os


class FeatureClassCreator:
def __init__(
self,
template_fc,
input_fc,
output_fc,
object_type="POLYGON",
delete_existing=False,
):
"""
Initializes the FeatureClassCreator with required parameters.
Parameters:
- template_fc (str): The path to the template feature class used to define the schema.
- input_fc (str): The path to the input feature class from which to append the geometry.
- output_fc (str): The path to the output feature class to create and append to.
- object_type (str): The type of geometry for the new feature class ("POINT", "MULTIPOINT", "LINE", "POLYLINE", "POLYGON", or "MULTIPATCH").
- delete_existing (bool): Whether to delete the existing output feature class if it exists. If set to False the geometry will be appended to the existing output feature class.
Example Usage:
--------------
>>> feature_creator = FeatureClassCreator(
... template_fc='path/to/template_feature_class',
... input_fc='path/to/input_feature_class',
... output_fc='path/to/output_feature_class',
... object_type='POLYGON',
... delete_existing=True
... )
>>> feature_creator.run()
This initializes the creator with all parameters and creates a new feature class based on the provided template,
optionally deleting any existing feature class at the output path, and appends the geometry from the input feature class.
"""
self.template_fc = template_fc
self.input_fc = input_fc
self.output_fc = output_fc
self.object_type = object_type
self.delete_existing = delete_existing

def run(self):
"""
Executes the process of creating a new feature class and appending geometry from the input feature class.
"""
if arcpy.Exists(self.output_fc):
if self.delete_existing:
# Deletes the output file if it exists and delete_existing boolean is set to True
arcpy.Delete_management(self.output_fc)
print(f"Deleted existing feature class: {self.output_fc}")
# Creates a new feature class after deletion
self._create_feature_class()
else:
# If output exists and delete_existing is set to False, just append data.
print(
f"Output feature class {self.output_fc} already exists. Appending data."
)
else:
if self.delete_existing:
print("Output feature class does not exist, so it was not deleted.")
# If output does not exist, create a new feature class
self._create_feature_class()

# Append geometry as the final step, occurring in all scenarios.
self._append_geometry()

def _create_feature_class(self):
"""
Creates a new feature class using the specified template and object type.
"""
output_workspace, output_class_name = os.path.split(self.output_fc)
arcpy.CreateFeatureclass_management(
output_workspace,
output_class_name,
self.object_type,
self.template_fc,
spatial_reference=arcpy.Describe(self.template_fc).spatialReference,
)
print(f"Created new feature class: {self.output_fc}")

def _append_geometry(self):
"""
Appends geometry from the input feature class to the output feature class.
This method assumes the output feature class already exists or was just created.
"""
with arcpy.da.SearchCursor(
self.input_fc, ["SHAPE@"]
) as s_cursor, arcpy.da.InsertCursor(self.output_fc, ["SHAPE@"]) as i_cursor:
for row in s_cursor:
i_cursor.insertRow(row)
print("Appended geometry to the feature class.")
40 changes: 40 additions & 0 deletions file_manager/n100/file_manager_rivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ class River_N100(Enum):
)
)

centerline_pruning_loop__water_features_processed__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="water_features_processed",
scale=scale,
)

centerline_pruning_loop__polygon_to_line__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="polygon_to_line",
Expand Down Expand Up @@ -418,6 +424,22 @@ class River_N100(Enum):
scale=scale,
)

centerline_pruning_loop__collapsed_hydropolygon_points__n100 = (
generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="collapsed_hydropolygon_points",
scale=scale,
)
)

centerline_pruning_loop__collapsed_hydropolygon_points_selected__n100 = (
generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="collapsed_hydropolygon_points_selected",
scale=scale,
)
)

centerline_pruning_loop__closed_centerline_lines__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="closed_centerline_lines",
Expand Down Expand Up @@ -473,3 +495,21 @@ class River_N100(Enum):
description="complex_water_features",
scale=scale,
)

centerline_pruning_loop__simple_centerlines__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="simple_centerlines",
scale=scale,
)

centerline_pruning_loop__complex_centerlines__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="complex_centerlines",
scale=scale,
)

centerline_pruning_loop__finnished_centerlines__n100 = generate_file_name_gdb(
function_name=centerline_pruning_loop,
description="finnished_centerlines",
scale=scale,
)
Loading

0 comments on commit 412fd01

Please sign in to comment.