Created
June 12, 2025 05:36
-
-
Save bohdanszymanik/1faa9d149fac0fdf477bf65d10a8acb0 to your computer and use it in GitHub Desktop.
Dissolving together Territorial Authority Local Boards TALB to Territorial Authority areas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
# Input and output feature classes | |
in_fc = r"C:\Users\BhodanSzymanilk\wd\SimplifiedVictimisationByStationAtTAGrain\SimplifiedVictimisationByStationAtTAGrain.gdb\census2023TALBpopulations" | |
output_fc = r"C:\Users\BhodanSzymanilk\wd\SimplifiedVictimisationByStationAtTAGrain\SimplifiedVictimisationByStationAtTAGrain.gdb\Census2023TApopulations" | |
# create a new field that uniquely identifies each TA - we grab the first 3 numbers from TALB2023_V - check if it exists firts | |
# all the Auckland local boards are identified with an initial '076' followed by numbers specific to the local board | |
TAID = "TAID" | |
for fieldname in [field.name for field in arcpy.ListFields(in_fc)]: | |
print(fieldname) | |
if TAID in [field.name for field in arcpy.ListFields(in_fc)]: | |
print(f"Field '{TAID}' already exists - deleting...") | |
arcpy.management.DeleteField(in_fc, TAID) | |
else: | |
print(f"Field '{TAID}' does not exist.") | |
arcpy.AddField_management( | |
in_table=in_fc, | |
field_name=TAID, | |
field_type="TEXT", | |
field_length=3, # field_length (optional, for text fields) | |
field_alias=TAID, # field_alias (optional) | |
field_is_nullable="NULLABLE", # Make the field nullable | |
field_is_required="NON_REQUIRED" # Field is not required - if set as required then you won't be able to delete - by design https://support.esri.com/en-us/knowledge-base/error-error-001334-cannot-delete-required-field-field-000014937 | |
# field_domain | |
) | |
# now populate | |
old_field_name = "TALB2023_V" | |
arcpy.CalculateField_management(in_fc, TAID, f"!{old_field_name}![:3]", "PYTHON3") | |
# Field to dissolve on (e.g., "LandUseType") | |
dissolve_field = TAID | |
# Perform the dissolve | |
arcpy.management.Dissolve( | |
in_features=in_fc, | |
out_feature_class=output_fc, | |
dissolve_field=dissolve_field, | |
statistics_fields=[ | |
["TALB2023_V","FIRST"], | |
["TALB2023_1","FIRST"], | |
["TALB2023_2","FIRST"], | |
["VAR_1_2","SUM"], | |
["VAR_1_3","SUM"], | |
["VAR_1_4","SUM"], | |
["VAR_1_5","SUM"], | |
["VAR_1_6","SUM"], | |
["VAR_1_7","SUM"], | |
["VAR_1_8","SUM"], | |
["VAR_1_9","SUM"], | |
["VAR_1_10","SUM"], | |
["VAR_1_11","SUM"], | |
["VAR_1_12","SUM"], | |
["VAR_1_13","SUM"], | |
["VAR_1_14","SUM"], | |
["VAR_1_15","SUM"], | |
["VAR_1_16","SUM"], | |
["VAR_1_17","SUM"], | |
["VAR_1_18","SUM"], | |
["VAR_1_19","SUM"], | |
["VAR_1_20","SUM"], | |
["VAR_1_21","SUM"], | |
["VAR_1_22","SUM"], | |
["VAR_1_23","SUM"], | |
["VAR_1_24","SUM"], | |
["VAR_1_25","SUM"] | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment