Select to view content in your preferred language

Python Tool Not Yielding Anything

501
5
Jump to solution
03-06-2025 02:39 PM
Ron_S1
by
Emerging Contributor

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
0 Kudos
1 Solution

Accepted Solutions
DavidSolari
MVP Regular Contributor

I ported your code to a standard toolbox tool (see attached) and everything ran smoothly. Some notes:

  1. There's no Tool class anymore, just a standard script tool layout. Wrapping your code in that class was almost certainly the issue as the script tool simply defined the class and then immediately finished. If you want to use that style, you'll either have to switch to a Python Toolbox or add something like this to the bottom: Tool().execute().
  2. Your original script didn't check if the topology already had the feature class added, this has been fixed.
  3. Your tool had no return value which could cause issues if it was combined with other tools in Modelbuilder. I chose to return the feature dataset but the choice is up to you.
  4. Avoid tab indents for Python scripts, that ship sailed a decade ago and most Python tools will create a mixed whitespace nightmare during edits. Luckily it's very easy to bulk convert tabs to the standard 4 space indents and then change the defaults going forward.

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

where are you doing the GetMessages.

What does you toolbox parameters look like?


... sort of retired...
0 Kudos
Ron_S1
by
Emerging Contributor

 

Hi Dan

This is the setup of my toolbox parameter. As for the GetMessages, I didn't have it initially, I just saw somewhere to add get messages to at least show if the lines are running, but it's still not doing anything. My guess is that the entire tool is not executing at all. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...
0 Kudos
DavidSolari
MVP Regular Contributor

I ported your code to a standard toolbox tool (see attached) and everything ran smoothly. Some notes:

  1. There's no Tool class anymore, just a standard script tool layout. Wrapping your code in that class was almost certainly the issue as the script tool simply defined the class and then immediately finished. If you want to use that style, you'll either have to switch to a Python Toolbox or add something like this to the bottom: Tool().execute().
  2. Your original script didn't check if the topology already had the feature class added, this has been fixed.
  3. Your tool had no return value which could cause issues if it was combined with other tools in Modelbuilder. I chose to return the feature dataset but the choice is up to you.
  4. Avoid tab indents for Python scripts, that ship sailed a decade ago and most Python tools will create a mixed whitespace nightmare during edits. Luckily it's very easy to bulk convert tabs to the standard 4 space indents and then change the defaults going forward.
Ron_S1
by
Emerging Contributor

Hi David,

Really appreciate your help and advice on this one. This works perfectly!

Thank you!