Hi,
I have created a script to create topology dataset in ArcGIS Pro, so what it does is it creates the dataset, the topology, import the layer I want to participate in the topology and add topology rules. I have 2 versions of the script, first one is just being ran in Pycharm, the other one has some tweaks to make it an ArcGIS python script tool. The first version runs perfectly, however the tool version is not yielding anything or even producing errors. Not sure what's wrong and I'm confident that I have set the parameters correctly in arcgis pro.
Here's the running version script:
import arcpy
import os
# Define input and output paths
gdb_path = r"P:\s123\GIS\R\Rgdb" # Path to your geodatabase
feature_class_path = r"P:\s123\GIS\R\R.gdb\MPads" # Path to the input feature class (could be a shapefile or feature class)
feature_dataset_name = "MyFeatureDataset" # Name of the new feature dataset
spatial_reference = arcpy.SpatialReference(4269) # Define the spatial reference (e.g., WGS 1984)
# Define the topology name
topology_name = "MyTopology"
# Step 1: Create a Feature Dataset
feature_dataset_path = os.path.join(gdb_path, feature_dataset_name)
# Check if feature dataset exists, if not, create it
if not arcpy.Exists(feature_dataset_path):
arcpy.CreateFeatureDataset_management(gdb_path, feature_dataset_name, spatial_reference)
print(f"Feature dataset '{feature_dataset_name}' created.")
else:
print(f"Feature dataset '{feature_dataset_name}' already exists.")
# Step 2: Import Feature Class into the Feature Dataset
# If the feature class is not already in the geodatabase, import it
feature_class_name = os.path.basename(feature_class_path + "_topo")
output_feature_class = os.path.join(feature_dataset_path, feature_class_name)
if not arcpy.Exists(output_feature_class):
arcpy.FeatureClassToFeatureClass_conversion(feature_class_path, feature_dataset_path, feature_class_name)
print(f"Feature class '{feature_class_name}' imported into feature dataset '{feature_dataset_name}'.")
else:
print(f"Feature class '{feature_class_name}' already exists in the feature dataset.")
# Step 3: Create Topology in the Feature Dataset
topology_path = os.path.join(feature_dataset_path, topology_name)
# Check if topology exists, if not, create it
if not arcpy.Exists(topology_path):
arcpy.CreateTopology_management(feature_dataset_path, topology_name)
print(f"Topology '{topology_name}' created in feature dataset '{feature_dataset_name}'.")
else:
print(f"Topology '{topology_name}' already exists.")
# Step 4: Add Feature Class to the Topology
arcpy.AddFeatureClassToTopology_management(topology_path, output_feature_class)
print(f"Feature class '{feature_class_name}' added to the topology.")
# Step 5: Define Topology Rules using the correct method
# Example rules: "Must Not Overlap (Area)", "Must Not Have Gaps (Area)"
arcpy.management.AddRuleToTopology(topology_path, "Must Not Have Gaps (Area)", output_feature_class)
arcpy.management.AddRuleToTopology(topology_path, "Must Not Overlap (Area)", output_feature_class)
print(f"Topology rules applied to the topology '{topology_name}'.")
# Step 6: Validate the Topology
arcpy.ValidateTopology_management(topology_path)
print(f"Topology '{topology_name}' validated.")
print("Topology creation and validation complete.")
Here's the tool version of the script
import arcpy
import os
class Tool(object):
def __init__(self):
"""Define the tool (the name of the tool and its label)"""
self.label = "Create Topology"
self.description = "Creates a topology dataset and adds feature classes to it."
def execute(self):
"""Main tool execution logic"""
try:
# Get input parameters
gdb_path = arcpy.GetParameterAsText(0) # Path to the geodatabase
feature_class_path = arcpy.GetParameterAsText(1) # Feature class path to import
feature_dataset_name = arcpy.GetParameterAsText(2) # Feature dataset name
spatial_reference = arcpy.GetParameterAsText(3) # Spatial reference (e.g., EPSG code)
topology_name = arcpy.GetParameterAsText(4) # Topology name
rule = arcpy.GetParameterAsText(5) # Topology rule to apply
# Step 1: Create a Feature Dataset
feature_dataset_path = os.path.join(gdb_path, feature_dataset_name)
if not arcpy.Exists(feature_dataset_path):
arcpy.CreateFeatureDataset_management(gdb_path, feature_dataset_name, spatial_reference)
arcpy.AddMessage(f"Feature dataset '{feature_dataset_name}' created.")
else:
arcpy.AddMessage(f"Feature dataset '{feature_dataset_name}' already exists.")
# Step 2: Import Feature Class into Feature Dataset
feature_class_name = os.path.basename(feature_class_path) + "_topo"
output_feature_class = os.path.join(feature_dataset_path, feature_class_name)
if not arcpy.Exists(output_feature_class):
arcpy.FeatureClassToFeatureClass_conversion(feature_class_path, feature_dataset_path, feature_class_name)
arcpy.AddMessage(f"Feature class '{feature_class_name}' imported into feature dataset.")
else:
arcpy.AddMessage(f"Feature class '{feature_class_name}' already exists.")
# Step 3: Create Topology in Feature Dataset
topology_path = os.path.join(feature_dataset_path, topology_name)
if not arcpy.Exists(topology_path):
arcpy.CreateTopology_management(feature_dataset_path, topology_name)
arcpy.AddMessage(f"Topology '{topology_name}' created.")
else:
arcpy.AddMessage(f"Topology '{topology_name}' already exists.")
# Step 4: Add Feature Class to Topology
arcpy.AddFeatureClassToTopology_management(topology_path, output_feature_class)
arcpy.AddMessage(f"Feature class '{feature_class_name}' added to the topology.")
# Step 5: Define Topology Rule
arcpy.management.AddRuleToTopology(topology_path, rule, output_feature_class)
arcpy.AddMessage(f"Topology rule '{rule}' applied.")
# Step 6: Validate the Topology
arcpy.ValidateTopology_management(topology_path)
arcpy.AddMessage(f"Topology '{topology_name}' validated.")
arcpy.AddMessage("Process complete.")
except Exception as e:
arcpy.AddError(f"Error: {e}")
raise e
Solved! Go to Solution.
I ported your code to a standard toolbox tool (see attached) and everything ran smoothly. Some notes:
where are you doing the GetMessages.
What does you toolbox parameters look like?
GetAllMessages—ArcGIS Pro | Documentation
if it isn't executing then GetMessages at least, but have a look at the above. Your toolbox tool should be reporting something when it is run. (another reason I hate Try Except blocks 😉 , it takes longer to deal with them than dealing with an error in the first place)
I ported your code to a standard toolbox tool (see attached) and everything ran smoothly. Some notes: